![]() |
Sylloge
A C# helper library
|
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 using System; 00018 using System.Collections; 00019 using System.Collections.Generic; 00020 using System.Text; 00021 using System.Windows.Forms; 00022 using System.Reflection; 00023 00024 namespace Sylloge 00025 { 00029 public static partial class FileSystem 00030 { 00037 public static void CreateDirectoryTree(string fullPath) 00038 { 00039 string top = (new System.IO.DirectoryInfo(fullPath)).Parent.FullName; 00040 if (!System.IO.Directory.Exists(top)) { Sylloge.FileSystem.CreateDirectoryTree(top); } 00041 try { 00042 System.IO.Directory.CreateDirectory(fullPath); 00043 } catch (Exception ex) { 00044 Sylloge.App.Out("Could not create directory (" + fullPath + ") - " + ex.ToString()); 00045 } 00046 } 00047 00057 public static bool CreateFile(string file, bool createTree = true) 00058 { 00059 if (System.IO.File.Exists(file)) { return true; } 00060 try { 00061 string dir = (new System.IO.FileInfo(file)).DirectoryName; 00062 if (!System.IO.Directory.Exists(dir)) { 00063 if (!createTree) { return false; } 00064 Sylloge.FileSystem.CreateDirectoryTree(dir); 00065 } 00066 System.IO.FileStream fh = System.IO.File.Create(file); 00067 fh.Close(); 00068 System.Threading.Thread.Sleep(500); 00069 } catch (Exception ex) { 00070 Sylloge.App.Out("Could not create file (" + file + ") - " + ex.ToString()); 00071 } 00072 return System.IO.File.Exists(file); 00073 } 00074 00075 public static bool UnsafeCreateFile(string file, bool createTree = true) 00076 { 00077 if (System.IO.File.Exists(file)) { return true; } 00078 string dir = (new System.IO.FileInfo(file)).DirectoryName; 00079 if (!System.IO.Directory.Exists(dir)) { 00080 if (!createTree) { return false; } 00081 Sylloge.FileSystem.CreateDirectoryTree(dir); 00082 } 00083 System.IO.FileStream fh = System.IO.File.Create(file); 00084 fh.Close(); 00085 System.Threading.Thread.Sleep(500); 00086 return System.IO.File.Exists(file); 00087 } 00088 00089 public static void ZeroFile(string file) 00090 { 00091 if (System.IO.File.Exists(file)) { 00092 try { 00093 System.IO.FileStream fh = new System.IO.FileStream(file, System.IO.FileMode.Truncate); 00094 fh.SetLength(0); 00095 fh.Close(); 00096 } catch (Exception ex) { 00097 Console.WriteLine("Could not zerio file ({0}) - {1}", file, ex); 00098 } 00099 } 00100 } 00101 00107 public static string DiskSpaceToString(ulong diskSpace) 00108 { 00109 string ByteType = "B"; 00110 string Space = Convert.ToString(diskSpace); 00111 if (diskSpace > Sylloge.Memory.GiB) { 00112 ByteType = "GiB"; 00113 Space = Math.Round((decimal)((double)diskSpace / (double)Sylloge.Memory.GiB), 3).ToString(); 00114 } else if (diskSpace > Sylloge.Memory.MiB) { 00115 ByteType = "MiB"; 00116 Space = Math.Round((decimal)((double)diskSpace / (double)Sylloge.Memory.MiB), 3).ToString(); 00117 } else if (diskSpace > Sylloge.Memory.KiB) { 00118 ByteType = "KiB"; 00119 Space = Math.Round((decimal)((double)diskSpace / (double)Sylloge.Memory.KiB), 3).ToString(); 00120 } 00121 return Space + " " + ByteType; 00122 } 00123 00129 private static string GetNewFileName(string oldFileName) 00130 { 00131 string val = string.Empty; 00132 if (!System.IO.File.Exists(oldFileName)) { return oldFileName; } 00133 string dir = (new System.IO.FileInfo(oldFileName)).DirectoryName; 00134 string ext = (new System.IO.FileInfo(oldFileName)).Extension; 00135 string fname = (new System.IO.FileInfo(oldFileName)).Name; 00136 fname = fname.Remove(fname.LastIndexOf(ext)); 00137 string fmt = "{0} - {1}{2}"; 00138 string tmpf = String.Format(fmt, fname, "00", ext); 00139 for (long i = 1; i < long.MaxValue; i++) { 00140 //val = dir + "\\" + fname + " - " + Index.ToString("00") + ext; 00141 val = System.IO.Path.Combine(dir, String.Format(fmt, fname, i.ToString("00"), ext)); 00142 if (!System.IO.File.Exists(val)) { return val; } 00143 } 00144 throw new System.IO.IOException("Could not generate new file name"); 00145 } 00146 } 00147 }
1.7.4