我打开了一个目录,并且读取了该目录中的一些文件,并做了一些更改,现在我想用相同的文件名保存更改F:\ BI \ Out \并保留原始文件。 当我添加这两行 var outFilePath = @“F:\ BI \ Out \”+ Path.GetFileName(file); File.WriteAllText(outFilePath,text); 我能够将文件保存在新文件夹\ Out, 下,但是当我打开它们时,我发现只有一个文件被正确更改,所有其他文件中的旧文字被空白空间替换,而不是新文字。 谁能帮我谢谢我做了一些更改后,如何在某个目录下的某个目录下保存某些文件
string text = "";
string[] files;
private void Form1_Load(object sender, EventArgs e)
{
try
{
files = Directory.GetFiles(@"F:\BI\In\", "*.*", SearchOption.AllDirectories);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
this.Close();
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
if (txtEtlPath.Text == "")
{
MessageBox.Show("Please Enter path");
txtEtlPath.Focus();
}
else
{
foreach (string file in files)
{
if (System.IO.File.Exists(file))
{
text = File.ReadAllText(file);
text = text.Replace("Company_Address", txtCompanyAddress.Text + "_BI");
text = text.Replace("Company_Name", txtCompanyName.Text.Trim() + "_BIDW");
text = text.Replace("C:\\BIfolder",cboDrive.Text + txtEtlPath.Text.Trim());
var outFilePath = @"F:\BI\Out\" + Path.GetFileName(file);
File.WriteAllText(outFilePath, text);
}
搜索是你的朋友,无论是在谷歌或Stackoverflow。[**'Directory.CreateDirectory' **方法](http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory%28v=vs。 110%29.aspx)和[**'如何:写入文本文件**](http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx)? – Prix 2014-11-09 01:28:00