【发布时间】:2023-03-22 09:58:01
【问题描述】:
使用文件夹对话框浏览包含特定文件的文件夹...
private void btnBrowse_Click(object sender, EventArgs e)
{
DialogResult result = fbdPath.ShowDialog();
if (result == DialogResult.OK)
{
string[] files = Directory.GetFiles(fbdPath.SelectedPath);
txtPath.Text = fbdPath.SelectedPath;
MessageBox.Show(txtPath.Text.ToString());
}
}
更新文件...
try
{
String path = txtPath.Text;
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files = dir.GetFiles();
foreach( FileInfo file in files)
{
StreamReader sReader;
sReader = File.OpenText(dir + @"\" + file.Name );
StreamWriter sWriter = new StreamWriter(@"f:\" + file.Name);
while (sReader.EndOfStream == false)
{
string contents = sReader.ReadLine();
String Flag = contents.Substring(655, 29);
String emBossFName = contents.Substring(41, 40);
String emBossLName = contents.Substring(121, 40);
String emBossFullName = emBossFName.Trim() + " " + emBossLName.Trim();
String newString = emBossFullName;
sWriter.WriteLine(contents.Replace(Flag, newString));
}
sWriter.Close();
sReader.Close();
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
我需要替换 29 个字符的字符串的第 655 位(由空格分隔的人的名字和姓氏的组合),但问题是如何保持字符串的固定长度685 个字符长?
【问题讨论】: