保存到由 openfiledialog 打开的文件 (C# 2008)

发布于 2024-12-06 12:57:09 字数 2568 浏览 1 评论 0原文

我想做的很可能非常简单,但花了几个小时后我仍然不知道如何正确地做到这一点。我可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-12-13 12:57:09

使用SaveFileDialog代替OpenFileDialog并且可以使用FileStream写入文件。

要检查文件是否正在使用,这就是我所做的..

public bool IsFileInUse(String file)
{
    bool retVal = false;
    try
    {
        using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
        {
            //file is not locked
        }
    }
    catch
    {
        retVal = true;
    }

    return retVal;
}

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..

public bool IsFileInUse(String file)
{
    bool retVal = false;
    try
    {
        using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
        {
            //file is not locked
        }
    }
    catch
    {
        retVal = true;
    }

    return retVal;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文