-1

我有 3 个目录。这些是图像,好的,坏的。我从图像目录中获取图像,并将其发送到其他目录。我的问题是,如果好的或坏的目录有这些图像,我该如何更改图像的名称,例如“我有 car.jpg,如果我再次复制它,它的名称会更改为 car(1).jpg。希望你能理解我的问题..

4

2 回答 2

1

我使用这样的代码

internal static partial class IOUtilities
{
    /// <summary>
    /// Move a file to an archive folder
    /// </summary>
    /// <remarks>Renames file if necessary to avoid collision.
    /// See <see cref="File.Move"/> for exceptions</remarks>
    /// <param name="file">path to file to move</param>
    /// <param name="targetFolder">folder to move file to</param>
    public static void ArchiveFile(string file, string targetFolder) {
        if (file == null)
            throw new ArgumentNullException("file");
        if (targetFolder == null)
            throw new ArgumentNullException("targetFolder");

        string targetFilename = Path.Combine(targetFolder, Path.GetFileName(file));
        File.Move(file, FindAvailableFilename(targetFilename));
    }

    /// <summary>
    /// Archive file in the same folder
    /// </summary>
    /// <remarks>Renames file by adding first free "(n)" suffix
    /// See <see cref="File.Move"/> for exceptions</remarks>
    /// <param name="file">path to file to archive</param>
    public static void ArchiveFile(string file) {
        if (file == null)
            throw new ArgumentNullException("file");

        File.Move(file, FindAvailableFilename(file));
    }

    /// <summary>
    /// Find a "free" filename by adding (2),(3)...
    /// </summary>
    /// <param name="targetFilename">Complete path to target filename</param>
    /// <returns>First available filename</returns>
    private static string FindAvailableFilename(string targetFilename) {
        if (!File.Exists(targetFilename))
            return targetFilename;

        string filenameFormat = Path.GetFileNameWithoutExtension(targetFilename) + "({0})" + Path.GetExtension(targetFilename);
        string format = Path.Combine(Path.GetDirectoryName(targetFilename), filenameFormat);
        for (int ii = 2;; ++ii) {
            // until we find a filename that doesn't exist
            string newFilename = string.Format(CultureInfo.InvariantCulture, format, ii);
            if (!File.Exists(newFilename)) // returns false on illegal paths, security problems etc
                return newFilename;
        }
    }
}
于 2013-08-19T06:21:26.873 回答
0

您可以在这里试用示例代码:http: //www.codeproject.com/Questions/212217/increment-filename-if-file-exists-using-csharp

于 2013-08-19T06:10:51.870 回答