Example #1
0
        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);
        }
Example #2
0
        /// <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));
            }
        }
Example #3
0
        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());
            }
        }
Example #4
0
 /// <summary>
 /// Copy directory or file, take full path of source and dest as parameter.
 /// </summary>
 public static void CopyFile(string source, string dest, FileCancelDelegate cancel)
 {
     using (FileStreamEx srcStream = FileEx.OpenRead(source))
     {
         byte[] buffer = new byte[Math.Min(1024 * 1024 * 32, srcStream.Length)];  //32MB
         int    readCount;
         ushort completePercent = 0;
         long   completeCount   = 0;
         using (FileStreamEx destStream = FileEx.Create(dest))
         {
             while ((readCount = srcStream.Read(buffer, 0, buffer.Length)) > 0 && !IsCancelTriggered(cancel, completePercent))
             {
                 completeCount += readCount;
                 destStream.Write(buffer, 0, readCount);
                 completePercent = srcStream.Length == 0 ? (ushort)100 : (ushort)((float)completeCount / (float)srcStream.Length * 100.0);
             }
             destStream.Flush();
             destStream.Close();
         }
         srcStream.Close();
     }
 }
Example #5
0
        /// <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));
            }
        }