Beispiel #1
0
        public static void AsyncCopyFile(string srcPath, string dstPath, string fileName, Action <CopyDirResult> resultCallback)
        {
            srcPath = FilterPath(srcPath);
            dstPath = FilterPath(dstPath);

            if (!File.Exists(srcPath + fileName))
            {
                if (resultCallback != null)
                {
                    resultCallback(new CopyDirResult(fileName, false, true, 0));
                }
                return;
            }

            using (FileStream sourceStream = new FileStream(srcPath + fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                byte[] srcBuffer = new byte[sourceStream.Length];
                try
                {
                    sourceStream.Read(srcBuffer, 0, srcBuffer.Length);

                    if (!Directory.Exists(dstPath))
                    {
                        Directory.CreateDirectory(dstPath);
                    }
                }
                catch
                {
                    if (resultCallback != null)
                    {
                        resultCallback(new CopyDirResult(fileName, false, true, 0));
                    }
                    return;
                }

                FileStream destStream = new FileStream(dstPath + fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                destStream.BeginWrite(srcBuffer, 0, srcBuffer.Length, new AsyncCallback((result) =>
                {
                    FileWriteAsyncInfo info = (FileWriteAsyncInfo)result.AsyncState;

                    try
                    {
                        info.Stream.EndWrite(result);

                        if (File.Exists(dstPath + fileName))
                        {
                            if (resultCallback != null)
                            {
                                resultCallback(new CopyDirResult(fileName, true, true, 1));
                            }
                        }
                        else if (resultCallback != null)
                        {
                            resultCallback(new CopyDirResult(fileName, false, true, 0));
                        }
                    }
                    catch
                    {
                        if (resultCallback != null)
                        {
                            resultCallback(new CopyDirResult(fileName, false, true, 0));
                        }
                    }

                    info.Stream.Close();
                }), new FileWriteAsyncInfo(destStream));
            }
        }
Beispiel #2
0
        static System.Collections.IEnumerator AsyncCopyDirRoutine(string srcDirPath, string destDirPath, Action <CopyDirResult> resultCallback)
        {
            srcDirPath  = FilterPath(srcDirPath);
            destDirPath = FilterPath(destDirPath);

            bool hasError = false;

            float currentFileCopied = 0;

            string[] filesPath = Directory.GetFiles(srcDirPath, "*.*", SearchOption.AllDirectories);

            CopyDirResult resultData = new CopyDirResult();

            for (int x = 0; x < filesPath.Length; x++)
            {
                string filePath = filesPath[x];
                string fileName = GetFileName(filePath);

                if (_cancelAsyncCopyDir || hasError)
                {
                    break;
                }

                try
                {
                    using (FileStream sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        string fileRelativePath = filePath.Replace(srcDirPath, "");

                        byte[] srcBuffer = new byte[sourceStream.Length];
                        try
                        {
                            sourceStream.Read(srcBuffer, 0, srcBuffer.Length);

                            DirectoryInfo dirInfo = Directory.GetParent(destDirPath + fileRelativePath);
                            if (!dirInfo.Exists)
                            {
                                Directory.CreateDirectory(dirInfo.FullName);
                            }
                        }
                        catch
                        {
                            resultData.Initialize(fileName, false, true, currentFileCopied / filesPath.Length);
                            hasError = true;
                            break;
                        }

                        if (!hasError)
                        {
                            FileStream destStream = new FileStream(destDirPath + fileRelativePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                            destStream.BeginWrite(srcBuffer, 0, srcBuffer.Length, new AsyncCallback((result) =>
                            {
                                FileWriteAsyncInfo info = (FileWriteAsyncInfo)result.AsyncState;

                                try
                                {
                                    info.Stream.EndWrite(result);
                                    currentFileCopied++;

                                    if (File.Exists(destDirPath + fileRelativePath))
                                    {
                                        resultData.Initialize(GetFileName(fileRelativePath), true, currentFileCopied == filesPath.Length, currentFileCopied / filesPath.Length);
                                    }
                                    else
                                    {
                                        resultData.Initialize(GetFileName(fileRelativePath), false, currentFileCopied == filesPath.Length, currentFileCopied / filesPath.Length);
                                    }
                                }
                                catch
                                {
                                    resultData.Initialize(GetFileName(fileRelativePath), false, currentFileCopied == filesPath.Length, currentFileCopied / filesPath.Length);
                                    hasError = true;
                                }

                                info.Stream.Close();
                            }), new FileWriteAsyncInfo(destStream));
                        }
                    }
                }
                catch
                {
                    resultData.Initialize("???", false, true, 0);
                    hasError = true;
                    break;
                }

                if (resultCallback != null)
                {
                    resultCallback(resultData);
                }

                yield return(new WaitForSeconds(0.1f));
            }

            // Caso a copia seja cancelada ou tenha acontecido algum erro
            // tentamos apagar o que foi copiado
            if (_cancelAsyncCopyDir || hasError)
            {
                TriesToClearAsynCopyWhenCanceled(destDirPath);

                resultData.IsDone             = true;
                resultData.SuccessfullyCopied = false;
                resultData.HasError           = hasError;
                resultData.WasCancelled       = _cancelAsyncCopyDir;
                if (resultCallback != null)
                {
                    resultCallback(resultData);
                }

                _cancelAsyncCopyDir = false;
            }
        }
Beispiel #3
0
        private static void AsyncWriteImg(string path, string fileName, byte[] imgBytes, Action <int> resultCallback)
        {
            string[] filterFileName = fileName.Split('.');
            fileName = filterFileName.Length > 1 ? filterFileName[0] : fileName;
            string absolutePath = path + fileName + ".png";

            // Add a index to the img name if the name matchs a already created one
            int counter = 1;

            try
            {
                while (File.Exists(absolutePath))
                {
                    absolutePath = path + fileName + string.Format(" ({0})", counter) + ".png";
                    counter++;
                }
            }
            catch
            {
                if (resultCallback != null)
                {
                    resultCallback(SAVE_IMAGE_CODE_UNKNOW_ERROR);
                }
            }

            FileStream stream = null;

            try
            {
                stream = new FileStream(absolutePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite, imgBytes.Length);
                stream.BeginWrite(imgBytes, 0, imgBytes.Length, new AsyncCallback((result) =>
                {
                    FileWriteAsyncInfo info = (FileWriteAsyncInfo)result.AsyncState;

                    try
                    {
                        info.Stream.EndWrite(result);

                        if (File.Exists(absolutePath))
                        {
                            if (resultCallback != null)
                            {
                                resultCallback(SAVE_IMAGE_CODE_SUCCESS);
                            }
                        }
                        else if (resultCallback != null)
                        {
                            resultCallback(SAVE_IMAGE_CODE_UNKNOW_ERROR);
                        }
                    }
                    catch
                    {
                        if (resultCallback != null)
                        {
                            resultCallback(SAVE_IMAGE_CODE_UNKNOW_ERROR);
                        }
                    }

                    info.Stream.Close();
                }), new FileWriteAsyncInfo(stream));
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                if (resultCallback != null)
                {
                    resultCallback(SAVE_IMAGE_CODE_UNKNOW_ERROR);
                }
            }
        }