|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace VirtualFileSystem |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Provides file system operations. Helps determining file and folder existence and creating file and folder items. |
| 11 | + /// </summary> |
| 12 | + public static class FsPath |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Returns true if the path points to a file. False - otherwise. |
| 16 | + /// </summary> |
| 17 | + /// <remarks>Throws exception if the file/folder does not exists.</remarks> |
| 18 | + /// <param name="path">Path to the file or folder.</param> |
| 19 | + /// <returns>True if the path is file. False - otherwise.</returns> |
| 20 | + public static bool IsFile(string path) |
| 21 | + { |
| 22 | + return !IsFolder(path); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Returns true if the path points to a folder. False - otherwise. |
| 27 | + /// </summary> |
| 28 | + /// <remarks>Throws exception if the file/folder does not exists.</remarks> |
| 29 | + /// <param name="path">Path to the file or folder.</param> |
| 30 | + /// <returns>True if the path is folder. False - otherwise.</returns> |
| 31 | + public static bool IsFolder(string path) |
| 32 | + { |
| 33 | + FileAttributes attributes = File.GetAttributes(path); |
| 34 | + return (attributes & FileAttributes.Directory) == FileAttributes.Directory; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Returns true if a file or folder exists under the specified path. False - otherwise. |
| 39 | + /// </summary> |
| 40 | + /// <remarks>Does not throw exceptions.</remarks> |
| 41 | + /// <param name="path">Path to the file or folder.</param> |
| 42 | + /// <returns>True if the file or folder exists under specified path. False - otherwise.</returns> |
| 43 | + public static bool Exists(string path) |
| 44 | + { |
| 45 | + return File.Exists(path) || Directory.Exists(path); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Returns true if the path point to a recycle bin folder. |
| 50 | + /// </summary> |
| 51 | + /// <param name="path">Path to the file or folder.</param> |
| 52 | + public static bool IsRecycleBin(string path) |
| 53 | + { |
| 54 | + return path.IndexOf("\\$Recycle.Bin", StringComparison.InvariantCultureIgnoreCase) != -1; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Returns storage item or null if item does not eists. |
| 59 | + /// </summary> |
| 60 | + /// <param name="path">Path to the file or folder.</param> |
| 61 | + /// <returns> |
| 62 | + /// Instance of <see cref="Windows.Storage.StorageFile"/> or <see cref="Windows.Storage.StorageFolder"/> |
| 63 | + /// that corresponds to path or null if item does not exists. |
| 64 | + /// </returns> |
| 65 | + public static async Task<Windows.Storage.IStorageItem> GetStorageItemAsync(string path) |
| 66 | + { |
| 67 | + if (File.Exists(path)) |
| 68 | + { |
| 69 | + return await Windows.Storage.StorageFile.GetFileFromPathAsync(path); |
| 70 | + } |
| 71 | + if (Directory.Exists(path)) |
| 72 | + { |
| 73 | + return await Windows.Storage.StorageFolder.GetFolderFromPathAsync(path); |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Returns folder or file item or null if the item does not exists. |
| 80 | + /// </summary> |
| 81 | + /// <param name="path">Path to the file or folder.</param> |
| 82 | + /// <returns> |
| 83 | + /// Instance of <see cref="FileInfo"/> or <see cref="DirectoryInfo"/> |
| 84 | + /// that corresponds to path or null if item does not exists. |
| 85 | + /// </returns> |
| 86 | + public static FileSystemInfo GetFileSystemItem(string path) |
| 87 | + { |
| 88 | + if (File.Exists(path)) |
| 89 | + { |
| 90 | + return new FileInfo(path); |
| 91 | + } |
| 92 | + if (Directory.Exists(path)) |
| 93 | + { |
| 94 | + return new DirectoryInfo(path); |
| 95 | + } |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Gets file or folder attributes in a human-readable form. |
| 101 | + /// </summary> |
| 102 | + /// <param name="path">Path to the file or folder.</param> |
| 103 | + /// <returns>String that represents file or folder attributes or null if the file/folder is not found.</returns> |
| 104 | + public static string GetAttString(string path) |
| 105 | + { |
| 106 | + try |
| 107 | + { |
| 108 | + return File.GetAttributes(path).ToString(); |
| 109 | + } |
| 110 | + catch |
| 111 | + { |
| 112 | + return null; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Returns true if the file hase a format ~XXXXX.tmp, false - otherwise. |
| 118 | + /// </summary> |
| 119 | + /// <param name="path">Path to a file or folder.</param> |
| 120 | + public static bool IsMsOfficeTemp(string path) |
| 121 | + { |
| 122 | + return Path.GetFileName(path).StartsWith('~') && Path.GetExtension(path).Equals(".tmp", StringComparison.InvariantCultureIgnoreCase); |
| 123 | + } |
| 124 | + |
| 125 | + public static bool IsMsOfficeLocked(string path) |
| 126 | + { |
| 127 | + //string lockFileName = $"~${Path.GetFileName(path)}"; |
| 128 | + //string lockPath = Path.Combine(Path.GetDirectoryName(path), lockFileName); |
| 129 | + |
| 130 | + int separatorIndex = path.LastIndexOf(Path.DirectorySeparatorChar); |
| 131 | + string lockPath = path.Insert(separatorIndex + 1, "~$"); |
| 132 | + return File.Exists(lockPath); |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Returns true if the file or folder is marked with Hidden or Temporaty attributes. |
| 137 | + /// </summary> |
| 138 | + /// <param name="path">Path to a file or folder.</param> |
| 139 | + /// <returns> |
| 140 | + /// True if the file or folder is marked with Hidden or Temporaty attributes. |
| 141 | + /// Returns false if no Hidden or Temporaty attributes found or file/folder does not exists. |
| 142 | + /// </returns> |
| 143 | + public static bool IsHiddenOrTemp(string path) |
| 144 | + { |
| 145 | + if(!FsPath.Exists(path)) |
| 146 | + { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + FileAttributes att = File.GetAttributes(path); |
| 151 | + return ( (att & System.IO.FileAttributes.Hidden) != 0) |
| 152 | + || ((att & System.IO.FileAttributes.Temporary) != 0); |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Returns true if the file or folder should not be syched between the user file |
| 157 | + /// system and the remote storage. False - otherwise. |
| 158 | + /// </summary> |
| 159 | + /// <param name="path">Path to a file or folder.</param> |
| 160 | + public static bool AvoidSync(string path) |
| 161 | + { |
| 162 | + return IsMsOfficeLocked(path) || IsMsOfficeTemp(path) || IsHiddenOrTemp(path); |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Gets formatted file size or null for folders or if the file is not found. |
| 167 | + /// </summary> |
| 168 | + /// <param name="path">Path to a file or folder.</param> |
| 169 | + public static string Size(string path) |
| 170 | + { |
| 171 | + if (!File.Exists(path)) |
| 172 | + { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + long length; |
| 177 | + try |
| 178 | + { |
| 179 | + length = new FileInfo(path).Length; |
| 180 | + } |
| 181 | + catch |
| 182 | + { |
| 183 | + return null; |
| 184 | + } |
| 185 | + |
| 186 | + string[] suf = { "b ", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB |
| 187 | + if (length == 0) |
| 188 | + { |
| 189 | + return "0" + suf[0]; |
| 190 | + } |
| 191 | + long bytes = Math.Abs(length); |
| 192 | + int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); |
| 193 | + double num = Math.Round(bytes / Math.Pow(1024, place), 1); |
| 194 | + return (Math.Sign(length) * num).ToString() + suf[place]; |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments