Example #1
0
        public static void CopyFiles(string srcPath, string destPath, bool isRecursive = true)
        {
            srcPath  = PathTool.NormalizePath(srcPath);
            destPath = PathTool.NormalizePathAndCreate(destPath);
            var srcDir       = Path.GetDirectoryName(srcPath);
            var srcPattern   = Path.GetFileName(srcPath);
            var destFileName = Path.GetFileName(destPath);

            var srcFiles = Directory.GetFiles(srcDir, srcPattern);

            foreach (var srcFile in srcFiles)
            {
                var srcFileInfo = new FileInfo(srcFile);
                if (string.IsNullOrEmpty(destFileName))
                {
                    srcFileInfo.CopyTo(Path.Combine(destPath, Path.GetFileName(srcFile)), true);
                }
                else
                {
                    // if dest path is a file just copy first one
                    srcFileInfo.CopyTo(destPath, true);
                    return;
                }
            }

            if (isRecursive)
            {
                var subDirs = Directory.GetDirectories(Path.GetDirectoryName(srcPath));
                foreach (var subDir in subDirs)
                {
                    var dirName     = subDir.Substring(srcDir.Length + 1);
                    var subsrcPath  = Path.Combine(subDir, srcPattern);
                    var subdestPath = Path.Combine(destPath, dirName) + Path.DirectorySeparatorChar;
                    CopyFiles(subsrcPath, subdestPath);
                }
            }
        }
Example #2
0
        static public void WriteIfChanged(string strTargetPath, byte[] bufferDataToWrite, long length, bool forceWrite = false)
        {
            strTargetPath = PathTool.NormalizePath(strTargetPath);
            PathTool.NormalizePathAndCreate(Path.GetFullPath(strTargetPath));
            //string targetDir = Path.GetDirectoryName(strTargetPath);
            string targetFileName = Path.GetFileName(strTargetPath);

            var sourceFileBuffer   = new byte[7 * 1024];
            int sourceFileReadSize = 0;
            var targetFileBuffer   = new byte[7 * 1024];
            int targetFileReadSize = 0;


            bool bIsMissMatched = false;

            try
            {
                using (var source = new FileStream(strTargetPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    using (var memoryFile = new MemoryStream(bufferDataToWrite, 0, (int)length, false))
                    {
                        if (source.Length != length)
                        {
                            bIsMissMatched = true;
                        }

                        while (!bIsMissMatched)
                        {
                            sourceFileReadSize = source.Read(sourceFileBuffer, 0, sourceFileBuffer.Length);
                            targetFileReadSize = memoryFile.Read(targetFileBuffer, 0, targetFileBuffer.Length);

                            if (targetFileReadSize != sourceFileReadSize)
                            {
                                bIsMissMatched = true;
                                break;
                            }
                            else if (targetFileReadSize == 0)
                            {
                                break;
                            }

                            int iChar = 0;
                            for (iChar = 0; iChar < sourceFileReadSize; iChar++)
                            {
                                if (sourceFileBuffer[iChar] != targetFileBuffer[iChar])
                                {
                                    bIsMissMatched = true;
                                    break;
                                }
                            }
                        }
                    }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
                // maybe file not exist, create new one
                bIsMissMatched = true;
            }

            if (bIsMissMatched)
            {
                Console.WriteLine("Writing : " + targetFileName);
                using (var source = new FileStream(strTargetPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    source.Write(bufferDataToWrite, 0, (int)length);
                }
            }
            else
            {
                //Console.WriteLine("All updated. Skipping : " + targetFileName);
            }
        }