【发布时间】:2014-11-09 01:25:40
【问题描述】:
我打开了一个目录并读取了该目录中的一些文件并进行了一些更改,现在我想将更改以相同的文件名保存在 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);
}
【问题讨论】:
-
搜索是您的朋友,无论是在 google 还是 Stackoverflow 上。
Directory.CreateDirectoryMethod 和How to: Write to a Text File?
标签: c#