-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cs
More file actions
133 lines (110 loc) · 3.38 KB
/
FileHandler.cs
File metadata and controls
133 lines (110 loc) · 3.38 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
124
125
126
127
128
129
130
131
132
133
using System.IO;
using System.Security.Permissions;
namespace fileHanding
{
public class FileHandler
{
private string path;
private string mode;
private FileInteractor interactor;
private bool createIfNotFound;
// Used to store the information about the file being handled
private Dictionary<string, string> properties;
private FileInfo fileInfo;
public FileHandler(string path, string mode, string logpath, bool createIfNotFound = true)
{
this.path = path;
this.mode = mode;
this.createIfNotFound = createIfNotFound;
this.interactor = new FileInteractor(logpath, path, mode, createIfNotFound);
this.fileInfo = new FileInfo(path);
this.properties = new Dictionary<string, string>();
updateInfo();
}
// Updates the stored information of the file
private void updateInfo()
{
properties.Clear();
properties.Add("Size", Convert.ToString(fileInfo.Length));
properties.Add("Creation Time", Convert.ToString(fileInfo.CreationTime));
properties.Add("Last Access Time", Convert.ToString(fileInfo.LastAccessTime));
properties.Add("Last Edit Time", Convert.ToString(fileInfo.LastWriteTime));
}
public Dictionary<string, string> getFileInfo()
{
return properties;
}
public bool isReadable()
{
try
{
FileIOPermission readCheck = new FileIOPermission(FileIOPermissionAccess.Read, path);
readCheck.Demand();
}
catch (UnauthorizedAccessException)
{
return false;
}
return true;
}
public bool isWriteable()
{
try
{
FileIOPermission readCheck = new FileIOPermission(FileIOPermissionAccess.Write, path);
readCheck.Demand();
}
catch (UnauthorizedAccessException)
{
return false;
}
return true;
}
public bool isAppendable()
{
try
{
FileIOPermission readCheck = new FileIOPermission(FileIOPermissionAccess.Append, path);
readCheck.Demand();
}
catch (UnauthorizedAccessException)
{
return false;
}
return true;
}
public void append(List<string> lines, bool writeLines = true)
{
interactor.append(lines, writeLines);
}
public void write(List<string> lines, bool writeLines = true)
{
interactor.write(lines, writeLines);
}
public string[,] readCSV()
{
return interactor.readCSV();
}
public string[] read()
{
return interactor.read();
}
public void createFile()
{
File.Create(path).Close();
}
public void deleteFile()
{
File.Delete(path);
}
public void moveFile(string newPath)
{
File.Move(path, newPath);
path = newPath;
}
public void copyFile(string newPath)
{
File.Copy(path, newPath);
}
}
}