如何更改多个文件的路径c#

how to change path of multi files c#

在此代码中,我从用户选择的路径获取文件名,我想编辑文本文件并将其保存为文本,但我希望它是 (filename+new).txt 我的代码正在获取 filename.txtnew :

private void button1_Click(object sender, EventArgs e)
{
    folderBrowserDialog1.ShowDialog();

    string[] fileArray = System.IO.Directory.GetFiles(folderBrowserDialog1.SelectedPath);
    foreach (string filename in fileArray)
    {
        richTextBox1.Text = "";
        //string path = @"C:\Users\rabih\Desktop\uni\IR\textNoStopWords"+".txt ";
        string path = filename + "new";
        richTextBox1.Text = System.IO.File.ReadAllText(filename);
        string st = richTextBox1.Text;
        string[] split = st.Split(' ');
        foreach (string s in split)
        {
            if (!Stopwords.Contains(s.ToLower()))
            {
                //richTextBox2.Text += s + " ";
            }
        }
        richTextBox2.Text += path + " \n";
        using (File.Create(path)) ;
        richTextBox2.SaveFile(path, RichTextBoxStreamType.RichText);
    }
}

您可以将文件名拆分为.并构造新的FileName,如下代码:

private static string GetNewFileName(string oldFileName)
{
    string[] splitedFileName = oldFileName.Split('.');

    return string.Concat(splitedFileName[0], "new.", splitedFileName[1]);
}

在您的代码中调用 GetNewFileName :

string newFileName = GetNewFileName(filename);

希望能帮助您解决问题。