保存到由 openfiledialog 打开的文件 (C# 2008)
我想做的很可能非常简单,但花了几个小时后我仍然不知道如何正确地做到这一点。我可以使用 openfiledialog 打开文本文件,但无法弄清楚如何保存回同一文件。我还希望能够在写入文件之前检查并查看该文件是否正在使用。这是我的打开和保存按钮的代码:
public void openToolStripMenuItem_Click(object sender, EventArgs e)
{
//This if statement checks if the user has saved any changes to the list boxes
if (MessageBox.Show(
"Have you saved your work?\nOpening a new file will clear out all list boxes.",
"Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
//Clears out the listboxes
this.itemListBox.Items.Clear();
this.priceListBox.Items.Clear();
this.qtyListBox.Items.Clear();
//This will open the file dialog windows to allow the user to chose a file
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "Harv's Hardware";
fileDialog.InitialDirectory = Directory.GetCurrentDirectory();
//File Filter
fileDialog.Filter = "txt files (*.txt)|*.txt";
fileDialog.FilterIndex = 2;
fileDialog.RestoreDirectory = true;
//This if statement executes is the user hits OK
if (fileDialog.ShowDialog() == DialogResult.OK)
{
//StreamReader readFile = File.OpenText(fileDialog.FileName);
currentFile = new StreamWriter(OpenFileDialog.FileName);
String inputString = null;
while ((inputString = readFile.ReadLine()) != null)
{
this.itemListBox.Items.Add(inputString);
inputString = readFile.ReadLine();
this.priceListBox.Items.Add(inputString);
inputString = readFile.ReadLine();
this.qtyListBox.Items.Add(inputString);
}
}
}
}
和保存按钮
//关闭并打开文件
//Creates a new saveDialog
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.ShowDialog();
//Listens to the user input
StreamWriter writeFile = File.CreateText(saveDialog.FileName);
int indexInteger = 0;
//Writes the actual File
while (indexInteger < priceListBox.Items.Count)
{
writeFile.WriteLine(itemListBox.Text);
writeFile.WriteLine(itemListBox.Text);
writeFile.WriteLine(qtyListBox.Text);
indexInteger++;
}
}
感谢您的帮助!
What I am trying to do is most likely very simple but afters spending hours I still cant figure out how to do it correctly. I am able to open a text file using the openfiledialog but cannot figure out to save back to that same file. I would like to also be able to check and see if the file is in use before writing to it. Here is my code for the open and save buttons:
public void openToolStripMenuItem_Click(object sender, EventArgs e)
{
//This if statement checks if the user has saved any changes to the list boxes
if (MessageBox.Show(
"Have you saved your work?\nOpening a new file will clear out all list boxes.",
"Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
//Clears out the listboxes
this.itemListBox.Items.Clear();
this.priceListBox.Items.Clear();
this.qtyListBox.Items.Clear();
//This will open the file dialog windows to allow the user to chose a file
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "Harv's Hardware";
fileDialog.InitialDirectory = Directory.GetCurrentDirectory();
//File Filter
fileDialog.Filter = "txt files (*.txt)|*.txt";
fileDialog.FilterIndex = 2;
fileDialog.RestoreDirectory = true;
//This if statement executes is the user hits OK
if (fileDialog.ShowDialog() == DialogResult.OK)
{
//StreamReader readFile = File.OpenText(fileDialog.FileName);
currentFile = new StreamWriter(OpenFileDialog.FileName);
String inputString = null;
while ((inputString = readFile.ReadLine()) != null)
{
this.itemListBox.Items.Add(inputString);
inputString = readFile.ReadLine();
this.priceListBox.Items.Add(inputString);
inputString = readFile.ReadLine();
this.qtyListBox.Items.Add(inputString);
}
}
}
}
and save button
//Closes and open files
//Creates a new saveDialog
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.ShowDialog();
//Listens to the user input
StreamWriter writeFile = File.CreateText(saveDialog.FileName);
int indexInteger = 0;
//Writes the actual File
while (indexInteger < priceListBox.Items.Count)
{
writeFile.WriteLine(itemListBox.Text);
writeFile.WriteLine(itemListBox.Text);
writeFile.WriteLine(qtyListBox.Text);
indexInteger++;
}
}
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用SaveFileDialog代替OpenFileDialog并且可以使用FileStream写入文件。
要检查文件是否正在使用,这就是我所做的..
Use SaveFileDialog instead of OpenFileDialog and can use FileStream to write to the file.
To check if file is in use or not, this is what I do..