如何更改多个文件的路径c#[关闭]

问题描述 投票:-2回答:1

在此代码中,我从用户选择的路径中获取文件名,我想编辑文本文件并将其保存为文本,但我希望它是(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);
            }
        }
c# windows filepath
1个回答
0
投票

您可以将文件名除以.并构造新的Path,如以下代码:

private static string GetNewPath(string oldPath)
{
    string[] splitedFileName = oldPath.Split('.');

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

调用您代码中的GetNewPath

string path = GetNewPath(filename);

我希望能帮助您解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.