コード例 #1
0
ファイル: DownloadViewModel.cs プロジェクト: 5l1v3r1/Maze-1
        public void Initialize(DownloadCommandInfo model)
        {
            FileSource.Initialize(model.FileSource);
            TargetPath         = model.TargetPath;
            FileExistsBehavior = model.FileExistsBehavior;

            OnPropertiesChanged();
        }
コード例 #2
0
    public static void Rename(this FileInfo fileInfo, string fileNewName, FileExistsBehavior fileExistsBehavior = FileExistsBehavior.Rename, string fileExistsRenameComplementsFormat = "({0})", params string[] fileExistsRenameComplements)
    {
        string fileNewPath = Path.Combine(fileInfo.Directory.FullName, fileNewName); if (File.Exists(fileNewPath))

        {
            switch (fileExistsBehavior)
            {
            case FileExistsBehavior.None:
                throw new IOException("Can't rename the file " + fileNewPath + " because there's already a file with this name in the destination directory.");

            case FileExistsBehavior.Replace: {
                File.Delete(fileNewPath);//  Remove the current file so its name can be used
                break;
            }

            case FileExistsBehavior.RenameOld: {                                                            //  Rename the current file so its old name then can be used.
                string fileOldPath = fileNewPath;                                                           //  Save target name that's required;
                fileNewPath = validNewName();                                                               // and change fileNewPath to a valid new name to use on the current file:
                new FileInfo(fileOldPath).Rename(Path.GetFileName(fileNewPath), FileExistsBehavior.Rename); // rename the current file to the new fileNewPath.
                fileNewPath = fileOldPath;                                                                  //  Finally, use the saved target name for the file to be renamed as the other file is already renamed
                break;
            }

            case FileExistsBehavior.Rename: {//  Get another valid name to be used instead and don't touch the existing file
                fileNewPath = validNewName();
                break;
            }

            case FileExistsBehavior.Skip://  Do nothing...
            default: return;
            }
        }
        File.Move(fileInfo.FullName, fileNewPath);//  Cloud apps can cause this function to throw a harmless exception after it processes...
        string validNewName()
        {
            string fileNewNameWithoutExtension = Path.GetFileNameWithoutExtension(fileNewName),
                   fileNewNameExtension        = Path.GetExtension(fileNewName),
                   fileNewNameValidated,
                   fileNewPathValidated;
            int duplicateCount = 0;

            do
            {
                duplicateCount++;
                if (fileExistsRenameComplements.Length == 0)
                {
                    fileNewNameValidated = fileNewNameWithoutExtension + String.Format(fileExistsRenameComplementsFormat, duplicateCount) + fileNewNameExtension;
                }
                else
                {
                    string[] complements = new string[fileExistsRenameComplements.Length]; for (int i = 0; i < fileExistsRenameComplements.Length; i++)
                    {
                        complements[i] = String.IsNullOrEmpty(fileExistsRenameComplements[i])?duplicateCount.ToString():fileExistsRenameComplements[i];
                    }
                    fileNewNameValidated = fileNewNameWithoutExtension + String.Format(fileExistsRenameComplementsFormat, complements) + fileNewNameExtension;
                }
                fileNewPathValidated = Path.Combine(fileInfo.Directory.FullName, fileNewNameValidated);
            }while(File.Exists(fileNewPathValidated));
            return(fileNewPathValidated);
        }
    }
コード例 #3
0
        /// <summary>
        /// Downloads a file from the specified URL.
        /// </summary>
        /// <param name="url">The url to download the file from.</param>
        /// <param name="localDirectory">The directory into which the file should be stored.</param>
        /// <param name="suffix">The suffix added to the local file name to make the file unique.</param>
        /// <param name="localFileName">The file name to which the file should be saved. If this arguments is left empty, the file name on the server will be used.</param>
        /// <param name="fileExistsBehavior">Specifies what should be done if the file name already exists locally.</param>
        /// <returns>the local path of the downloaded file</returns>
        private string DownloadFile(string url, string localDirectory, out string suffix, string localFileName = "", FileExistsBehavior fileExistsBehavior = FileExistsBehavior.Skip)
        {
            try
            {
                var request  = WebRequest.Create(url);
                var response = request.GetResponse();
                if (String.IsNullOrEmpty(localFileName))
                {
                    localFileName = Path.GetFileName(response.ResponseUri.LocalPath);
                }
                var localfilePath = Path.Combine(localDirectory, localFileName);

                suffix = "";

                if (File.Exists(localfilePath))
                {
                    switch (fileExistsBehavior)
                    {
                    case FileExistsBehavior.Replace:
                        File.Delete(localfilePath);
                        break;

                    case FileExistsBehavior.Skip:
                        SkippingFile(localFileName);
                        return(localfilePath);

                    case FileExistsBehavior.Rename:
                        localfilePath = GetUniquePath(localfilePath, out suffix);
                        localFileName = suffix + localFileName;
                        break;
                    }
                }
                DownloadingFile(localFileName);
                using (var fs = File.Create(localfilePath))
                {
                    response.GetResponseStream().CopyTo(fs);
                }

                return(localfilePath);
            }
            catch (Exception ex)
            {
                DownloadFailed(url, ex);
                suffix = "";
                return(url);
            }
        }