public static void Copy(string source, string dest) { if (!FileEx.Exists(source)) { throw new IOException("Source not exist."); } if (FileEx.Exists(dest)) { throw new IOException("Dest already exist."); } IOTools.Copy(source, dest); }
/// <summary> /// Return file crc of the specified file /// </summary> public static string GetFileCRC(string filename) { if (!FileEx.Exists(filename)) { return(""); } Fesersoft.Hashing.crc32 crc32 = new Fesersoft.Hashing.crc32(); using (FileStreamEx stmcheck = FileEx.OpenRead(filename)) { uint crchash = (uint)crc32.CRC(stmcheck); stmcheck.Close(); return(ConvertToHex(crchash)); } }
public static string GetFileMD5(string filename) { if (!FileEx.Exists(filename)) { return(""); } MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider(); using (FileStreamEx stmcheck = FileEx.OpenRead(filename)) { byte[] hash = csp.ComputeHash(stmcheck); stmcheck.Close(); return(BitConverter.ToString(hash).Replace("-", "").ToLower()); } }
/// <summary> /// Open a file stream, used by FileStreamEx /// </summary> internal static void openStream(IStorage parentStorage, string filename, ref FileMode mode, ref FileAccess access, out IntPtr streamPtr, out IStream stream) { ShellAPI.STGM grfmode = ShellAPI.STGM.SHARE_DENY_WRITE; switch (access) { case FileAccess.ReadWrite: grfmode |= ShellAPI.STGM.READWRITE; break; case FileAccess.Write: grfmode |= ShellAPI.STGM.WRITE; break; } switch (mode) { case FileMode.Create: if (FileEx.Exists(filename)) { grfmode |= ShellAPI.STGM.CREATE; } break; case FileMode.CreateNew: grfmode |= ShellAPI.STGM.CREATE; break; } if (parentStorage != null) { if (parentStorage.OpenStream( filename, IntPtr.Zero, grfmode, 0, out streamPtr) == ShellAPI.S_OK) { stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream)); } else if (access != FileAccess.Read) { //Create file if not exists if (parentStorage.CreateStream( filename, ShellAPI.STGM.WRITE, 0, 0, out streamPtr) == ShellAPI.S_OK) { stream = (IStream)Marshal.GetTypedObjectForIUnknown(streamPtr, typeof(IStream)); } else { throw new IOException(String.Format("Can't open stream: {0}", filename)); } } else { throw new IOException(String.Format("Can't open stream: {0}", filename)); } } else { throw new IOException(String.Format("Can't open stream: {0}", filename)); } }