-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperation.cs
More file actions
123 lines (122 loc) · 4.48 KB
/
FileOperation.cs
File metadata and controls
123 lines (122 loc) · 4.48 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System.IO;
using System.Text;
namespace CreatorModAPI
{
public static class FileOperation
{
/// <summary>
/// 判断文件是否被占用
/// </summary>
/// <param name="file">文件路径</param>
/// <returns></returns>
public static bool IsFileInUse(this string file)
{
bool isUse = true;
FileStream fs = null;
try
{
fs = new FileStream(file, FileMode.Open);
fs.Dispose();
isUse = false;
}
catch { }
return isUse;
}
public static Stream CreateStream(this Stream stream)
{
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
stream.Dispose();
return memoryStream;
}
public static bool Delete(this string directory)
{
if (File.Exists(directory))
{
if (IsFileInUse(directory)) return false;
File.Delete(directory);
return true;
}
foreach (string dire in Directory.GetDirectories(directory)) Delete(dire);
foreach (string file in Directory.GetFiles(directory))
{
if (!IsFileInUse(file)) File.Delete(file); else return false;
}
if (Directory.Exists(directory))
Directory.Delete(directory);
return true;
}
/*public static bool IsCopyWorld(this string file)
{
bool b = true;
FileStream stream = new FileStream(file, FileMode.Open);
using (BinaryReader binaryReader = new BinaryReader(stream, Encoding.UTF8, true))
{
byte[] array = new byte[7];
if (binaryReader.Read(array, 0, array.Length) != array.Length && array[0] == (byte)'W' && array[1] == (byte)'o' && array[2] == (byte)'r' && array[3] == (byte)'l' && array[4] == (byte)'d' && array[5] == (byte)'\0')
{
try
{
binaryReader.Dispose();
stream.Dispose();
CopyAndPaste.ConversionCopyFile(file);
return true;
}
catch
{
return false;
}
}
if (binaryReader.Read(array, 0, array.Length) != array.Length || array[0] != (byte)'S' || array[1] != (byte)'C' || array[2] != (byte)'C' || array[3] != (byte)'O' || array[4] != (byte)'P' || array[5] != (byte)'Y' || array[6] != (byte)'\0')
{
b = false;
}
binaryReader.Dispose();
stream.Dispose();
}
return b;
}
public static bool IsOneKeyWorld(this string file)
{
bool b = true;
FileStream stream = new FileStream(file, FileMode.Open);
using (BinaryReader binaryReader = new BinaryReader(stream, Encoding.UTF8, true))
{
byte[] array = new byte[7];
if (binaryReader.Read(array, 0, array.Length) != array.Length || array[0] != (byte)'O' || array[1] != (byte)'n' || array[2] != (byte)'e' || array[3] != (byte)'K' || array[4] != (byte)'e' || array[5] != (byte)'y' || array[6] != (byte)'\0')
{
b = false;
}
binaryReader.Dispose();
stream.Dispose();
}
return b;
}*/
public static FileStream CreateFile(this string file)
{
FileStream fileStream;
try
{
fileStream = new FileStream(file, FileMode.Create);
}
catch (DirectoryNotFoundException)
{
string[] directorys = file.Split('/');
string path = "";
for (int i = 0; i < directorys.Length - 1; i++)
{
if (i == directorys.Length - 1)
{
path += directorys[i];
break;
}
path += directorys[i] + "/";
}
Directory.CreateDirectory(path);
fileStream = new FileStream(file, FileMode.Create);
}
return fileStream;
}
}
}