English 中文(简体)
替换C#中的一部分档案名称
原标题:Replace part of a filename in C#

我有<代码>的夹。 在大多数档案的名称中,我想用另一个插图取代具体的插图。

这里是我撰写的。

  private void btnGetFiles_Click(object sender, EventArgs e)
    {
        string dir = tbGetFIles.Text;
        List<string> FileNames = new List<string>();

        DirectoryInfo DirInfo = new DirectoryInfo(dir);

        foreach (FileInfo File in DirInfo.GetFiles())
        {
            FileNames.Add(File.Name);       
        }

        lbFileNames.DataSource = FileNames;
    }

Here I extract all file names in List Box.

    private void btnReplace_Click(object sender, EventArgs e)
    {
        string strReplace = tbReplace.Text; // The existing string
        string strWith = tbWith.Text; // The new string

        string dir = tbGetFIles.Text;
        DirectoryInfo DirInfo = new DirectoryInfo(dir);
        FileInfo[] names = DirInfo.GetFiles();


        foreach (FileInfo f in names)
        {
            if(f.Name.Contains(strReplace))
            {
                f.Name.Replace(strReplace, strWith);
            }

        }

在这里,我想做的是替代,但事情是错误的。 什么?

最佳回答

你想改变磁盘上的档案名称。 如果是的话,你需要使用<代码>File.MoveA/60/8诉s. 更改实际编号,即档案名称。

您提出的另一个错误是Replace。 http://www.un.org/Depts/DGACM/index_chinese.htm 看到你需要将新的价值重新分配给一个变量

string newName = f.Name.Replace(strReplace, strWith);
File.Move(f.Name, newName);
问题回答

f.Name is a read-only property. f.Name.Replace(..) simply returns a new string with the filename you want, but never actually changes the file.
I suggest something along the following, though I haven t tested it:

File.Move(f.Name, f.Name.Replace(strReplace, strWith));

Replace return another string, it doesn t change the original string.
So you need to write

string newName = f.Name.Replace(strReplace, strWith); 

of course this doesn t change the name of the file on disk.
If that was your intention then you should look at

File.Move(f.Name, newName);

还铭记这一档案。 如果存在目的档案,则调查将例外。

See here for a example

从一看,似乎你不把所替换的地块重新分配给你。 引证:

string NewFileName = f.Name.Replace(strReplace, strWith);
File.Copy(f.Name, NewFileName);
File.Delete(f.Name);

页: 1 替换。 相反,它正在恢复新的扼杀。

你们需要把自己的法典改成像:

if(f.Name.Contains(strReplace)) 
{ 
    string newFileName = f.Name.Replace(strReplace, strWith); 
    //and work here with your new string
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签