我打開了一個目錄,並且讀取了該目錄中的一些文件,並做了一些更改,現在我想用相同的文件名保存更改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