-
Notifications
You must be signed in to change notification settings - Fork 47
/
FileUtil.cs
97 lines (91 loc) · 3.54 KB
/
FileUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
01/27/2020 EPPlus Software AB Initial release EPPlus 5
*************************************************************************************************/
using System;
using System.IO;
namespace EPPlusSamples
{
public class FileUtil
{
static DirectoryInfo _outputDir = null;
public static DirectoryInfo OutputDir
{
get
{
return _outputDir;
}
set
{
_outputDir = value;
if (!_outputDir.Exists)
{
_outputDir.Create();
}
}
}
public static FileInfo GetCleanFileInfo(string file)
{
var fi = new FileInfo(OutputDir.FullName + Path.DirectorySeparatorChar + file);
if (fi.Exists)
{
fi.Delete(); // ensures we create a new workbook
}
return fi;
}
public static FileInfo GetFileInfo(string file)
{
return new FileInfo(OutputDir.FullName + Path.DirectorySeparatorChar + file);
}
public static FileInfo GetFileInfo(DirectoryInfo altOutputDir, string file, bool deleteIfExists = true)
{
var fi = new FileInfo(altOutputDir.FullName + Path.DirectorySeparatorChar + file);
if (deleteIfExists && fi.Exists)
{
fi.Delete(); // ensures we create a new workbook
}
return fi;
}
internal static DirectoryInfo GetDirectoryInfo(string directory)
{
var di = new DirectoryInfo(_outputDir.FullName + Path.DirectorySeparatorChar + directory);
if (!di.Exists)
{
di.Create();
}
return di;
}
/// <summary>
/// Returns a fileinfo with the full path of the requested file
/// </summary>
/// <param name="directory">A subdirectory</param>
/// <param name="file"></param>
/// <returns></returns>
public static FileInfo GetFileInfo(string directory, string file)
{
var rootDir = GetRootDirectory().FullName;
return new FileInfo(Path.Combine(rootDir, directory, file));
}
public static DirectoryInfo GetRootDirectory()
{
var currentDir = AppDomain.CurrentDomain.BaseDirectory;
while (!currentDir.EndsWith("bin"))
{
currentDir = Directory.GetParent(currentDir).FullName.TrimEnd('\\');
}
return new DirectoryInfo(currentDir).Parent;
}
public static DirectoryInfo GetSubDirectory(string directory, string subDirectory)
{
var currentDir = GetRootDirectory().FullName;
return new DirectoryInfo(Path.Combine(currentDir, directory, subDirectory));
}
}
}