-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathFileContext.cs
More file actions
225 lines (192 loc) · 6.99 KB
/
Copy pathFileContext.cs
File metadata and controls
225 lines (192 loc) · 6.99 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.IO;
using OneScript.Contexts;
using ScriptEngine.Machine.Contexts;
namespace OneScript.StandardLibrary
{
[ContextClass("Файл","File")]
public class FileContext : AutoContext<FileContext>
{
private readonly string _givenName;
private string _name;
private string _baseName;
private string _fullName;
private string _path;
private string _extension;
public FileContext(string name)
{
// Strip null characters that can be added by Windows WebDAV client
// to maintain compatibility with 1.x behavior
name = PathHelper.StripNullCharacters(name);
if (String.IsNullOrWhiteSpace(name))
{
_name = "";
_baseName = "";
_fullName = "";
_path = "";
_extension = "";
}
_givenName = name;
}
private string LazyField(ref string value, Func<string, string> algo)
{
if (value == null)
value = algo(_givenName);
return value;
}
[ContextProperty("Имя","Name")]
public string Name
{
get
{
return LazyField(ref _name, GetFileNameV8Compatible);
}
}
private string GetFileNameV8Compatible(string arg)
{
return System.IO.Path.GetFileName(arg.TrimEnd(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar));
}
[ContextProperty("ИмяБезРасширения", "BaseName")]
public string BaseName
{
get
{
return LazyField(ref _baseName, System.IO.Path.GetFileNameWithoutExtension);
}
}
[ContextProperty("ПолноеИмя", "FullName")]
public string FullName
{
get
{
return LazyField(ref _fullName, System.IO.Path.GetFullPath);
}
}
[ContextProperty("Путь", "Path")]
public string Path
{
get
{
return LazyField(ref _path, GetPathWithEndingDelimiter);
}
}
private string GetPathWithEndingDelimiter(string src)
{
src = src.Trim ();
if (src.Length == 1 && src[0] == System.IO.Path.DirectorySeparatorChar)
return src; // корневой каталог
var path = System.IO.Path.GetDirectoryName(src.TrimEnd(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar));
if (path == null)
return src; // корневой каталог
if (path.Length > 0 && path[path.Length - 1] != System.IO.Path.DirectorySeparatorChar)
path += System.IO.Path.DirectorySeparatorChar;
return path;
}
[ContextProperty("Расширение", "Extension")]
public string Extension
{
get
{
return LazyField(ref _extension, System.IO.Path.GetExtension);
}
}
[DeprecatedName("Exist")]
[ContextMethod("Существует","Exists")]
public bool Exists()
{
if (_givenName == String.Empty)
return false;
try
{
File.GetAttributes(FullName);
return true;
}
catch (FileNotFoundException) { }
catch (DirectoryNotFoundException) { }
catch (ArgumentException) { }
catch (NotSupportedException) { }
catch (PathTooLongException) { }
catch (UnauthorizedAccessException) { }
catch (IOException) { }
return false;
}
[ContextMethod("Размер", "Size")]
public long Size()
{
return new FileInfo(FullName).Length;
}
[ContextMethod("ПолучитьНевидимость", "GetHidden")]
public bool GetHidden()
{
var attr = File.GetAttributes(FullName);
return attr.HasFlag(System.IO.FileAttributes.Hidden);
}
[ContextMethod("ПолучитьТолькоЧтение", "GetReadOnly")]
public bool GetReadOnly()
{
var attr = File.GetAttributes(FullName);
return attr.HasFlag(System.IO.FileAttributes.ReadOnly);
}
[ContextMethod("ПолучитьВремяИзменения", "GetModificationTime")]
public DateTime GetModificationTime()
{
return File.GetLastWriteTime(FullName);
}
[ContextMethod("ПолучитьВремяСоздания", "GetCreationTime")]
public DateTime GetCreationTime()
{
return File.GetCreationTime(FullName);
}
[ContextMethod("УстановитьНевидимость", "SetHidden")]
public void SetHidden(bool value)
{
FileSystemInfo entry = new FileInfo(FullName);
if(value)
entry.Attributes |= System.IO.FileAttributes.Hidden;
else
entry.Attributes &= ~System.IO.FileAttributes.Hidden;
}
[ContextMethod("УстановитьТолькоЧтение", "SetReadOnly")]
public void SetReadOnly(bool value)
{
FileSystemInfo entry = new FileInfo(FullName);
if (value)
entry.Attributes |= System.IO.FileAttributes.ReadOnly;
else
entry.Attributes &= ~System.IO.FileAttributes.ReadOnly;
}
[ContextMethod("УстановитьВремяИзменения", "SetModificationTime")]
public void SetModificationTime(DateTime dateTime)
{
FileSystemInfo entry = new FileInfo(FullName);
entry.LastWriteTime = dateTime;
}
[ContextMethod("ЭтоКаталог", "IsDirectory")]
public bool IsDirectory()
{
var attr = File.GetAttributes(FullName);
return attr.HasFlag(FileAttributes.Directory);
}
[ContextMethod("ЭтоФайл", "IsFile")]
public bool IsFile()
{
var attr = File.GetAttributes(FullName);
return !attr.HasFlag(FileAttributes.Directory);
}
public FileAttributes GetAttributes()
{
return File.GetAttributes(FullName);
}
[ScriptConstructor(Name = "По имени файла")]
public static FileContext Constructor(string name)
{
return new FileContext(name);
}
}
}