简体   繁体   English

如何在C#中重命名文件

[英]How to rename a file in C#

Consider:考虑:

strPath= c:\images\gallery\add.gif

I need to rename this file from add.gif to thumb1.gid , and I should write one command method, whatever the file name.我需要将此文件从add.gif重命名为thumb1.gid ,并且我应该编写一个命令方法,无论文件名如何。 We need to replace that name with this like below.我们需要用下面的名称替换该名称。

string strfilename = **"thumb"**

****Result thum.gif** ****结果thum.gif**

strPath= c:\\images\\gallery\\thum.gif ** strPath= c:\\images\\gallery\\thum.gif **

You have several problems, looking up the value in the XML file, and renaming the file.您有几个问题,在 XML 文件中查找值并重命名文件。

To look up the number corresponding to Gallery2 or whatever, I would recommend having a look at Stack Overflow question How to implement a simple XPath lookup which explains how to look up nodes/values in an XML file.要查找与 Gallery2 或其他内容相对应的数字,我建议您查看 Stack Overflow 问题如何实现简单的 XPath 查找,该问题解释了如何在 XML 文件中查找节点/值。

To rename a file in .NET, use something like this:要在 .NET 中重命名文件,请使用以下内容:

using System.IO;

FileInfo fi = new FileInfo("c:\\images\\gallery\\add.gif");
if (fi.Exists)
{
    fi.MoveTo("c:\\images\\gallery\\thumb3.gif");
}

Of course, you would use string variables instead of string literals for the paths.当然,您将使用字符串变量而不是路径的字符串文字。

That should give you enough information to piece it together and solve your particular lookup-rename problem.这应该为您提供足够的信息来将它们拼凑在一起并解决您的特定查找重命名问题。

I created a utility method to help encapsulate how to rename a file.我创建了一个实用方法来帮助封装如何重命名文件。

    public class FileHelper 
    {
        public static void RenameFile(string oldFilenameWithPathWithExtension, string newFilenameWithoutPathWithExtension)
        {
            try 
            {
                string directoryPath = Path.GetDirectoryName(oldFilenameWithPathWithExtension);
                if (directoryPath == null)
                {
                    throw new Exception($"Directory not found in given path value:{oldFilenameWithPathWithExtension}");
                }

                var newFilenameWithPath = Path.Combine(directoryPath, newFilenameWithoutPathWithExtension);
                FileInfo fileInfo = new FileInfo(oldFilenameWithPathWithExtension);
                fileInfo.MoveTo(newFilenameWithPath);
            }
            catch (Exception e)
            {
                //Boiler plate exception handling
                Console.WriteLine(e);
                throw;
            }
        }
    }

I omitted several other file system checks that could optionally be done, but as @JoelCoehoorn pointed out in a comment on this page, the File System is Volatile, so wrapping it in a try-catch may be all that is necessary.我省略了其他几个可以选择完成的文件系统检查,但正如@JoelCoehoorn 在本页的评论中指出的那样,文件系统是易失性的,因此可能只需要将其包装在 try-catch 中即可。

With that class in your library, now you can simply call:有了图书馆里的那个类,现在你可以简单地调用:

            var fullFilename = @"C:\images\gallery\add.gif";
            var newFilename = "Thumb.gif";
            FileHelper.RenameFile(fullFilename,newFilename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM