110,534
社区成员
发帖
与我相关
我的任务
分享
string filePath = null;
string newDirectory = @"D:/demo/";
private void btn1_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
filePath = op.FileName;
}
}
private void btn2_Click(object sender, EventArgs e)
{
if (filePath != null)
{
string name = Path.GetFileName(filePath);
using (StreamReader sr = new StreamReader(this.filePath))
{
string txt = sr.ReadToEnd();
using (var sw = File.CreateText(Path.Combine(newDirectory, name)))
{
sw.Write(txt.Replace(",", string.Empty));
}
}
}
}
大致代码就这样了,你看着办吧
public static void DataSave(string saveDirectory, string saveName, string saveContent)
{
string filePath = saveDirectory + saveName + ".txt";
if (!Directory.Exists(saveDirectory))
{
Directory.CreateDirectory(saveDirectory);
}
using (StreamWriter sw = File.AppendText(filePath))
{
sw.Write(saveContent);
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
using (StreamReader streamReader = File.OpenText(openFile.FileName))
{
string txtLine = streamReader.ReadToEnd();
//存到对应的控件中修改例如textBox1
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string fileDirectory = String.Empty;
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
fileDirectory = fbd.SelectedPath;
}
DataSave( fileDirectory ,"文件名称","文件内容");
}