如何在c#中重命名文件

时间:2022-10-03 10:48:27

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. We need to replace that name with this like below.

我需要将这个文件从add.gif重命名为thumb1。gid,我应该编写一个命令方法,不管文件名是什么。我们需要将这个名称替换为如下所示。

string strfilename = **"thumb"**

****Result thum.gif**

结果thum.gif * * * * * *

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

strPath = c:\ \图片画廊\铺平。gif * *

1 个解决方案

#1


6  

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.

这将为您提供足够的信息,使您能够将其组合在一起,并解决特定的查找重命名问题。

#1


6  

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.

这将为您提供足够的信息,使您能够将其组合在一起,并解决特定的查找重命名问题。