Пример #1
0
        public static string SafeCopyFile(string sourceFileName, string destFileName, SafeCopyFileOptions options)
        {
            string str;

            switch (options)
            {
            case SafeCopyFileOptions.PreserveOriginal:
            {
                if (File.Exists(destFileName))
                {
                    return(null);
                }
                File.Copy(sourceFileName, destFileName);
                return(destFileName);
            }

            case SafeCopyFileOptions.Overwrite:
            {
                File.Copy(sourceFileName, destFileName, true);
                return(destFileName);
            }

            case SafeCopyFileOptions.FindBetterName:
            {
                string directoryName            = Path.GetDirectoryName(destFileName);
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(destFileName);
                string extension = Path.GetExtension(destFileName);
                using (IEnumerator <string> enumerator = Utility.GenerateFileNames(directoryName, fileNameWithoutExtension, extension).GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        string current = enumerator.Current;
                        if (File.Exists(current))
                        {
                            continue;
                        }
                        File.Copy(sourceFileName, current);
                        str = current;
                        return(str);
                    }
                    return(null);
                }
                return(str);
            }

            default:
            {
                throw new ArgumentException("Invalid enumeration value", "options");
            }
            }
            return(null);
        }