Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IsolatedStorageFile.cs
Go to the documentation of this file.
2using System.Linq;
3using System.Text;
4
6
8{
10 {
11 public object Current
12 {
13 get
14 {
15 throw new InvalidOperationException();
16 }
17 }
18
19 public bool MoveNext()
20 {
21 return false;
22 }
23
24 public void Reset()
25 {
26 }
27 }
28
29 private bool _disposed;
30
31 private bool _closed;
32
33 private readonly object _internalLock = new object();
34
35 private readonly string _rootDirectory;
36
37 private string RootDirectory => _rootDirectory;
38
39 internal bool Disposed => _disposed;
40
41 internal bool IsDeleted
42 {
43 get
44 {
45 try
46 {
48 }
49 catch (IOException)
50 {
51 return true;
52 }
54 {
55 return true;
56 }
57 }
58 }
59
60 public override long AvailableFreeSpace => Quota - UsedSize;
61
62 [CLSCompliant(false)]
63 [Obsolete("IsolatedStorage.MaximumSize has been deprecated because it is not CLS Compliant. To get the maximum size use IsolatedStorage.Quota instead.")]
64 public override ulong MaximumSize => 9223372036854775807uL;
65
66 public override long Quota => long.MaxValue;
67
68 public override long UsedSize => 0L;
69
70 [CLSCompliant(false)]
71 [Obsolete("IsolatedStorage.CurrentSize has been deprecated because it is not CLS Compliant. To get the current size use IsolatedStorage.UsedSize instead.")]
72 public override ulong CurrentSize => 0uL;
73
74 public static bool IsEnabled => true;
75
77 {
78 InitStore(scope, null, null);
79 StringBuilder stringBuilder = new StringBuilder(Helper.GetRootDirectory(scope));
80 stringBuilder.Append(SeparatorExternal);
81 stringBuilder.Append(base.IdentityHash);
82 stringBuilder.Append(SeparatorExternal);
83 if (Helper.IsApplication(scope))
84 {
85 stringBuilder.Append("AppFiles");
86 }
87 else if (Helper.IsDomain(scope))
88 {
89 stringBuilder.Append("Files");
90 }
91 else
92 {
93 stringBuilder.Append("AssemFiles");
94 }
95 stringBuilder.Append(SeparatorExternal);
96 _rootDirectory = stringBuilder.ToString();
98 }
99
100 public void Close()
101 {
102 if (Helper.IsRoaming(base.Scope))
103 {
104 return;
105 }
106 lock (_internalLock)
107 {
108 if (!_closed)
109 {
110 _closed = true;
111 GC.SuppressFinalize(this);
112 }
113 }
114 }
115
116 public void DeleteFile(string file)
117 {
118 if (file == null)
119 {
120 throw new ArgumentNullException("file");
121 }
123 try
124 {
125 string fullPath = GetFullPath(file);
126 File.Delete(fullPath);
127 }
128 catch (Exception rootCause)
129 {
131 }
132 }
133
134 public bool FileExists(string path)
135 {
136 if (path == null)
137 {
138 throw new ArgumentNullException("path");
139 }
141 return File.Exists(GetFullPath(path));
142 }
143
144 public bool DirectoryExists(string path)
145 {
146 if (path == null)
147 {
148 throw new ArgumentNullException("path");
149 }
151 return Directory.Exists(GetFullPath(path));
152 }
153
154 public void CreateDirectory(string dir)
155 {
156 if (dir == null)
157 {
158 throw new ArgumentNullException("dir");
159 }
161 string fullPath = GetFullPath(dir);
162 if (Directory.Exists(fullPath))
163 {
164 return;
165 }
166 try
167 {
168 Directory.CreateDirectory(fullPath);
169 }
170 catch (Exception rootCause)
171 {
173 }
174 }
175
176 public void DeleteDirectory(string dir)
177 {
178 if (dir == null)
179 {
180 throw new ArgumentNullException("dir");
181 }
183 try
184 {
185 string fullPath = GetFullPath(dir);
186 Directory.Delete(fullPath, recursive: false);
187 }
188 catch (Exception rootCause)
189 {
191 }
192 }
193
194 public string[] GetFileNames()
195 {
196 return GetFileNames("*");
197 }
198
199 public string[] GetFileNames(string searchPattern)
200 {
201 if (searchPattern == null)
202 {
203 throw new ArgumentNullException("searchPattern");
204 }
206 try
207 {
208 return (from f in Directory.EnumerateFiles(RootDirectory, searchPattern)
209 select Path.GetFileName(f)).ToArray();
210 }
211 catch (UnauthorizedAccessException rootCause)
212 {
214 }
215 }
216
217 public string[] GetDirectoryNames()
218 {
219 return GetDirectoryNames("*");
220 }
221
222 public string[] GetDirectoryNames(string searchPattern)
223 {
224 if (searchPattern == null)
225 {
226 throw new ArgumentNullException("searchPattern");
227 }
229 try
230 {
231 return (from m in Directory.EnumerateDirectories(RootDirectory, searchPattern)
232 select m.Substring(Path.GetDirectoryName(m).Length + 1)).ToArray();
233 }
234 catch (UnauthorizedAccessException rootCause)
235 {
237 }
238 }
239
241 {
243 return new IsolatedStorageFileStream(path, mode, this);
244 }
245
246 public IsolatedStorageFileStream OpenFile(string path, FileMode mode, FileAccess access)
247 {
249 return new IsolatedStorageFileStream(path, mode, access, this);
250 }
251
252 public IsolatedStorageFileStream OpenFile(string path, FileMode mode, FileAccess access, FileShare share)
253 {
255 return new IsolatedStorageFileStream(path, mode, access, share, this);
256 }
257
259 {
261 return new IsolatedStorageFileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, this);
262 }
263
264 public DateTimeOffset GetCreationTime(string path)
265 {
266 if (path == null)
267 {
268 throw new ArgumentNullException("path");
269 }
270 if (path.Length == 0)
271 {
273 }
275 try
276 {
278 }
280 {
281 return new DateTimeOffset(1601, 1, 1, 0, 0, 0, TimeSpan.Zero).ToLocalTime();
282 }
283 }
284
286 {
287 if (path == null)
288 {
289 throw new ArgumentNullException("path");
290 }
291 if (path.Length == 0)
292 {
294 }
296 try
297 {
299 }
301 {
302 return new DateTimeOffset(1601, 1, 1, 0, 0, 0, TimeSpan.Zero).ToLocalTime();
303 }
304 }
305
307 {
308 if (path == null)
309 {
310 throw new ArgumentNullException("path");
311 }
312 if (path.Length == 0)
313 {
315 }
317 try
318 {
320 }
322 {
323 return new DateTimeOffset(1601, 1, 1, 0, 0, 0, TimeSpan.Zero).ToLocalTime();
324 }
325 }
326
327 public void CopyFile(string sourceFileName, string destinationFileName)
328 {
329 if (sourceFileName == null)
330 {
331 throw new ArgumentNullException("sourceFileName");
332 }
333 if (destinationFileName == null)
334 {
335 throw new ArgumentNullException("destinationFileName");
336 }
337 if (sourceFileName.Length == 0)
338 {
339 throw new ArgumentException(System.SR.Argument_EmptyPath, "sourceFileName");
340 }
341 if (destinationFileName.Length == 0)
342 {
343 throw new ArgumentException(System.SR.Argument_EmptyPath, "destinationFileName");
344 }
345 CopyFile(sourceFileName, destinationFileName, overwrite: false);
346 }
347
348 public void CopyFile(string sourceFileName, string destinationFileName, bool overwrite)
349 {
350 if (sourceFileName == null)
351 {
352 throw new ArgumentNullException("sourceFileName");
353 }
354 if (destinationFileName == null)
355 {
356 throw new ArgumentNullException("destinationFileName");
357 }
358 if (sourceFileName.Length == 0)
359 {
360 throw new ArgumentException(System.SR.Argument_EmptyPath, "sourceFileName");
361 }
362 if (destinationFileName.Length == 0)
363 {
364 throw new ArgumentException(System.SR.Argument_EmptyPath, "destinationFileName");
365 }
367 string fullPath = GetFullPath(sourceFileName);
368 string fullPath2 = GetFullPath(destinationFileName);
369 try
370 {
371 File.Copy(fullPath, fullPath2, overwrite);
372 }
374 {
375 throw new FileNotFoundException(System.SR.Format(System.SR.PathNotFound_Path, sourceFileName));
376 }
378 {
379 throw;
380 }
381 catch (Exception rootCause)
382 {
384 }
385 }
386
387 public void MoveFile(string sourceFileName, string destinationFileName)
388 {
389 if (sourceFileName == null)
390 {
391 throw new ArgumentNullException("sourceFileName");
392 }
393 if (destinationFileName == null)
394 {
395 throw new ArgumentNullException("destinationFileName");
396 }
397 if (sourceFileName.Length == 0)
398 {
399 throw new ArgumentException(System.SR.Argument_EmptyPath, "sourceFileName");
400 }
401 if (destinationFileName.Length == 0)
402 {
403 throw new ArgumentException(System.SR.Argument_EmptyPath, "destinationFileName");
404 }
406 string fullPath = GetFullPath(sourceFileName);
407 string fullPath2 = GetFullPath(destinationFileName);
408 try
409 {
410 File.Move(fullPath, fullPath2);
411 }
413 {
414 throw new FileNotFoundException(System.SR.Format(System.SR.PathNotFound_Path, sourceFileName));
415 }
417 {
418 throw;
419 }
420 catch (Exception rootCause)
421 {
423 }
424 }
425
426 public void MoveDirectory(string sourceDirectoryName, string destinationDirectoryName)
427 {
428 if (sourceDirectoryName == null)
429 {
430 throw new ArgumentNullException("sourceDirectoryName");
431 }
432 if (destinationDirectoryName == null)
433 {
434 throw new ArgumentNullException("destinationDirectoryName");
435 }
436 if (sourceDirectoryName.Length == 0)
437 {
438 throw new ArgumentException(System.SR.Argument_EmptyPath, "sourceDirectoryName");
439 }
440 if (destinationDirectoryName.Length == 0)
441 {
442 throw new ArgumentException(System.SR.Argument_EmptyPath, "destinationDirectoryName");
443 }
445 string fullPath = GetFullPath(sourceDirectoryName);
446 string fullPath2 = GetFullPath(destinationDirectoryName);
447 try
448 {
449 Directory.Move(fullPath, fullPath2);
450 }
452 {
453 throw new DirectoryNotFoundException(System.SR.Format(System.SR.PathNotFound_Path, sourceDirectoryName));
454 }
456 {
457 throw;
458 }
459 catch (Exception rootCause)
460 {
462 }
463 }
464
466 {
468 }
469
471 {
472 return GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Application);
473 }
474
476 {
477 return GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly);
478 }
479
484
489
491 {
492 return GetStore(IsolatedStorageScope.Machine | IsolatedStorageScope.Application);
493 }
494
496 {
497 return GetStore(IsolatedStorageScope.Assembly | IsolatedStorageScope.Machine);
498 }
499
504
506 {
507 return new IsolatedStorageFile(scope);
508 }
509
510 public static IsolatedStorageFile GetStore(IsolatedStorageScope scope, Type? applicationEvidenceType)
511 {
512 if (!(applicationEvidenceType == null))
513 {
515 }
516 return GetStore(scope);
517 }
518
519 public static IsolatedStorageFile GetStore(IsolatedStorageScope scope, object? applicationIdentity)
520 {
521 if (applicationIdentity != null)
522 {
524 }
525 return GetStore(scope);
526 }
527
528 public static IsolatedStorageFile GetStore(IsolatedStorageScope scope, Type? domainEvidenceType, Type? assemblyEvidenceType)
529 {
530 if (!(domainEvidenceType == null) || !(assemblyEvidenceType == null))
531 {
533 }
534 return GetStore(scope);
535 }
536
537 public static IsolatedStorageFile GetStore(IsolatedStorageScope scope, object? domainIdentity, object? assemblyIdentity)
538 {
539 if (domainIdentity != null || assemblyIdentity != null)
540 {
542 }
543 return GetStore(scope);
544 }
545
546 internal string GetFullPath(string partialPath)
547 {
548 int i;
549 for (i = 0; i < partialPath.Length && (partialPath[i] == Path.DirectorySeparatorChar || partialPath[i] == Path.AltDirectorySeparatorChar); i++)
550 {
551 }
552 partialPath = partialPath.Substring(i);
553 return Path.Combine(RootDirectory, partialPath);
554 }
555
556 internal void EnsureStoreIsValid()
557 {
558 if (Disposed)
559 {
561 }
562 if (_closed || IsDeleted)
563 {
565 }
566 }
567
568 public void Dispose()
569 {
570 Close();
571 _disposed = true;
572 }
573
574 internal static Exception GetIsolatedStorageException(string exceptionMsg, Exception rootCause)
575 {
576 IsolatedStorageException ex = new IsolatedStorageException(exceptionMsg, rootCause);
577 ex._underlyingException = rootCause;
578 return ex;
579 }
580
581 public override bool IncreaseQuotaTo(long newQuotaSize)
582 {
583 return true;
584 }
585
586 public override void Remove()
587 {
588 try
589 {
590 Directory.Delete(RootDirectory, recursive: true);
591 }
592 catch
593 {
595 }
596 Close();
597 string directoryName = Path.GetDirectoryName(RootDirectory.TrimEnd(Path.DirectorySeparatorChar));
598 if (ContainsUnknownFiles(directoryName))
599 {
600 return;
601 }
602 try
603 {
604 Directory.Delete(directoryName, recursive: true);
605 }
606 catch
607 {
608 return;
609 }
610 if (!Helper.IsDomain(base.Scope))
611 {
612 return;
613 }
614 directoryName = Path.GetDirectoryName(directoryName);
615 if (ContainsUnknownFiles(directoryName))
616 {
617 return;
618 }
619 try
620 {
621 Directory.Delete(directoryName, recursive: true);
622 }
623 catch
624 {
625 }
626 }
627
628 public static void Remove(IsolatedStorageScope scope)
629 {
630 VerifyGlobalScope(scope);
631 string rootDirectory = Helper.GetRootDirectory(scope);
632 try
633 {
634 Directory.Delete(rootDirectory, recursive: true);
635 Directory.CreateDirectory(rootDirectory);
636 }
637 catch
638 {
640 }
641 }
642
643 private static void VerifyGlobalScope(IsolatedStorageScope scope)
644 {
645 if (scope != IsolatedStorageScope.User && scope != (IsolatedStorageScope.User | IsolatedStorageScope.Roaming) && scope != IsolatedStorageScope.Machine)
646 {
648 }
649 }
650
651 private bool ContainsUnknownFiles(string directory)
652 {
653 string[] files;
654 string[] directories;
655 try
656 {
657 files = Directory.GetFiles(directory);
658 directories = Directory.GetDirectories(directory);
659 }
660 catch
661 {
663 }
664 if (directories.Length > 1 || (directories.Length != 0 && !IsMatchingScopeDirectory(directories[0])))
665 {
666 return true;
667 }
668 if (files.Length == 0)
669 {
670 return false;
671 }
672 if (Helper.IsRoaming(base.Scope))
673 {
674 if (files.Length <= 1)
675 {
676 return !IsIdFile(files[0]);
677 }
678 return true;
679 }
680 if (files.Length <= 2 && (IsIdFile(files[0]) || IsInfoFile(files[0])))
681 {
682 if (files.Length == 2 && !IsIdFile(files[1]))
683 {
684 return !IsInfoFile(files[1]);
685 }
686 return false;
687 }
688 return true;
689 }
690
691 private bool IsMatchingScopeDirectory(string directory)
692 {
693 string fileName = Path.GetFileName(directory);
694 if ((!Helper.IsApplication(base.Scope) || !string.Equals(fileName, "AppFiles", StringComparison.Ordinal)) && (!Helper.IsAssembly(base.Scope) || !string.Equals(fileName, "AssemFiles", StringComparison.Ordinal)))
695 {
696 if (Helper.IsDomain(base.Scope))
697 {
698 return string.Equals(fileName, "Files", StringComparison.Ordinal);
699 }
700 return false;
701 }
702 return true;
703 }
704
705 private static bool IsIdFile(string file)
706 {
707 return string.Equals(Path.GetFileName(file), "identity.dat");
708 }
709
710 private static bool IsInfoFile(string file)
711 {
712 return string.Equals(Path.GetFileName(file), "info.dat");
713 }
714}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static string[] GetDirectories(string path)
Definition Directory.cs:156
static void Delete(string path)
Definition Directory.cs:355
static void Move(string sourceDirName, string destDirName)
Definition Directory.cs:304
static IEnumerable< string > EnumerateDirectories(string path)
Definition Directory.cs:216
static IEnumerable< string > EnumerateFiles(string path)
Definition Directory.cs:236
static DirectoryInfo CreateDirectory(string path)
Definition Directory.cs:28
static string[] GetFiles(string path)
Definition Directory.cs:136
static bool Exists([NotNullWhen(true)] string? path)
Definition Directory.cs:43
static DateTime GetLastAccessTime(string path)
Definition File.cs:188
static bool Exists([NotNullWhen(true)] string? path)
Definition File.cs:97
static DateTime GetCreationTime(string path)
Definition File.cs:164
static DateTime GetLastWriteTime(string path)
Definition File.cs:212
static void Copy(string sourceFileName, string destFileName)
Definition File.cs:47
static void Move(string sourceFileName, string destFileName)
Definition File.cs:581
static void Delete(string path)
Definition File.cs:88
static bool IsAssembly(IsolatedStorageScope scope)
Definition Helper.cs:48
static bool IsRoaming(IsolatedStorageScope scope)
Definition Helper.cs:58
static bool IsDomain(IsolatedStorageScope scope)
Definition Helper.cs:63
static string GetRootDirectory(IsolatedStorageScope scope)
Definition Helper.cs:18
static void CreateDirectory(string path, IsolatedStorageScope scope)
Definition Helper.cs:164
static bool IsApplication(IsolatedStorageScope scope)
Definition Helper.cs:53
static void Remove(IsolatedStorageScope scope)
static IsolatedStorageFile GetStore(IsolatedStorageScope scope, object? domainIdentity, object? assemblyIdentity)
static void VerifyGlobalScope(IsolatedStorageScope scope)
void CopyFile(string sourceFileName, string destinationFileName, bool overwrite)
IsolatedStorageFileStream CreateFile(string path)
static IsolatedStorageFile GetMachineStoreForDomain()
override bool IncreaseQuotaTo(long newQuotaSize)
IsolatedStorageFileStream OpenFile(string path, FileMode mode, FileAccess access)
void MoveDirectory(string sourceDirectoryName, string destinationDirectoryName)
void MoveFile(string sourceFileName, string destinationFileName)
static Exception GetIsolatedStorageException(string exceptionMsg, Exception rootCause)
void CopyFile(string sourceFileName, string destinationFileName)
static IsolatedStorageFile GetMachineStoreForApplication()
static IsolatedStorageFile GetStore(IsolatedStorageScope scope, Type? domainEvidenceType, Type? assemblyEvidenceType)
static IsolatedStorageFile GetUserStoreForApplication()
IsolatedStorageFileStream OpenFile(string path, FileMode mode)
static IEnumerator GetEnumerator(IsolatedStorageScope scope)
static IsolatedStorageFile GetMachineStoreForAssembly()
IsolatedStorageFileStream OpenFile(string path, FileMode mode, FileAccess access, FileShare share)
static IsolatedStorageFile GetStore(IsolatedStorageScope scope, object? applicationIdentity)
static IsolatedStorageFile GetStore(IsolatedStorageScope scope, Type? applicationEvidenceType)
static IsolatedStorageFile GetStore(IsolatedStorageScope scope)
void InitStore(IsolatedStorageScope scope, Type appEvidenceType)
static string Combine(string path1, string path2)
Definition Path.cs:304
static ? string GetFileName(string? path)
Definition Path.cs:200
static readonly char AltDirectorySeparatorChar
Definition Path.cs:73
static readonly char DirectorySeparatorChar
Definition Path.cs:71
static ? string GetDirectoryName(string? path)
Definition Path.cs:121
static string IsolatedStorage_Scope_U_R_M
Definition SR.cs:52
static string IsolatedStorage_DeleteDirectory
Definition SR.cs:28
static string IsolatedStorage_NotValidOnDesktop
Definition SR.cs:36
static string IsolatedStorage_DeleteFile
Definition SR.cs:24
static string PlatformNotSupported_CAS
Definition SR.cs:54
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string IsolatedStorage_CreateDirectory
Definition SR.cs:26
static string IsolatedStorage_StoreNotOpen
Definition SR.cs:14
static string Argument_EmptyPath
Definition SR.cs:38
static string IsolatedStorage_DeleteDirectories
Definition SR.cs:48
static string IsolatedStorage_Operation
Definition SR.cs:22
static string PathNotFound_Path
Definition SR.cs:40
Definition SR.cs:7
override string ToString()
StringBuilder Append(char value, int repeatCount)
DateTimeOffset ToLocalTime()
static readonly TimeSpan Zero
Definition TimeSpan.cs:21