/// <summary>
 /// Wrapper around File.Copy to provide feedback as to whether the file wasn't copied because it didn't exist.
 /// </summary>
 /// <param name="cachePath"></param>
 /// <param name="suggestedPath"></param>
 /// <param name="overwrite"></param>
 /// <returns></returns>
 public static string SafeCopyFile(string sourceFileName, string destFileName, SafeCopyFileOptions options)
 {
     switch (options)
     {
         case SafeCopyFileOptions.PreserveOriginal:
             if (!File.Exists(destFileName))
             {
                 File.Copy(sourceFileName, destFileName);
                 return destFileName;
             }
             return null;
         case SafeCopyFileOptions.Overwrite:
             File.Copy(sourceFileName, destFileName, true);
             return destFileName;
         case SafeCopyFileOptions.FindBetterName:
             string directoryPart = Path.GetDirectoryName(destFileName);
             string fileNamePart = Path.GetFileNameWithoutExtension(destFileName);
             string extensionPart = Path.GetExtension(destFileName);
             foreach (string path in GenerateFileNames(directoryPart, fileNamePart, extensionPart))
             {
                 if (!File.Exists(path))
                 {
                     File.Copy(sourceFileName, path);
                     return path;
                 }
             }
             return null;
     }
     throw new ArgumentException("Invalid enumeration value", "options");
 }
Exemple #2
0
        internal void SaveToFile(FacebookImageDimensions requestedSize, string path, bool addExtension, FacebookImageSaveOptions options, SaveImageAsyncCallback callback, object userState, int?index, int?total)
        {
            Verify.IsNeitherNullNorEmpty(path, "path");
            Verify.IsNotNull(callback, "callback");
            Assert.Implies(total != null, index != null);
            Assert.Implies(total == null, index == null);

            SafeCopyFileOptions scfo = (options == FacebookImageSaveOptions.FindBetterName)
                ? SafeCopyFileOptions.FindBetterName
                : (options == FacebookImageSaveOptions.Overwrite)
                    ? SafeCopyFileOptions.Overwrite
                    : SafeCopyFileOptions.PreserveOriginal;

            SourceService.WebGetter.GetLocalImagePathAsync(this, null, _GetSmallUriFromRequestedSize(requestedSize),
                                                           (sender, e) =>
            {
                string cachePath = e.ImagePath;
                if (addExtension)
                {
                    string ext = Path.GetExtension(cachePath);
                    path       = Path.ChangeExtension(path, ext);
                }

                try
                {
                    string actualPath = Utility.SafeCopyFile(cachePath, path, scfo);
                    if (actualPath == null)
                    {
                        throw new IOException("Unable to save the image to the requested location.");
                    }

                    SaveImageCompletedEventArgs sicea = null;
                    if (total == null)
                    {
                        sicea = new SaveImageCompletedEventArgs(actualPath, userState);
                    }
                    else
                    {
                        sicea = new SaveImageCompletedEventArgs(actualPath, index.Value, total.Value, userState);
                    }

                    callback(this, sicea);
                    return;
                }
                catch (Exception ex)
                {
                    callback(this, new SaveImageCompletedEventArgs(ex, false, userState));
                    return;
                }
            });
        }
Exemple #3
0
        public static string SafeCopyFile(string sourceFileName, string destFileName, SafeCopyFileOptions options)
        {
            switch (options)
            {
            case SafeCopyFileOptions.PreserveOriginal:
                if (!File.Exists(destFileName))
                {
                    File.Copy(sourceFileName, destFileName);
                    return(destFileName);
                }
                return(null);

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

            case SafeCopyFileOptions.FindBetterName:
                string directoryPart = Path.GetDirectoryName(destFileName);
                string fileNamePart  = Path.GetFileNameWithoutExtension(destFileName);
                string extensionPart = Path.GetExtension(destFileName);
                foreach (string path in GenerateFileNames(directoryPart, fileNamePart, extensionPart))
                {
                    if (!File.Exists(path))
                    {
                        File.Copy(sourceFileName, path);
                        return(path);
                    }
                }
                return(null);
            }
            throw new ArgumentException("Invalid enumeration value", "options");
        }
Exemple #4
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);
        }