Lodestone6
Lodestone6

Reputation: 166

Is it ok to .OpenFile() a file that is already Open?

This doesnt require any actual problem solving. Just wondering about the nature of something.

I noticed that if I trigger this event twice in a row, selecting the same file both times, I don't get any sort of error even though I have never explicitly closed the file. Does the file close automagically when ReadLine reaches the end of the file, or is it simply OK to attempt to open a file that is already open so long as it is opened by the same application?

 private void uIDownloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int lineNum = 1;
            using (OpenFileDialog dlgOpen = new OpenFileDialog())
                try
                {
                    // Default file extension
                    dlgOpen.DefaultExt = "*.hex";
                    // SaveFileDialog title
                    dlgOpen.Title = "Download UI Image";
                    // Available file extensions
                    dlgOpen.Filter = "Hex Files (*.hex)|*.hex|All Files (*.*)|*.*";
                    // Show SaveFileDialog box and save file
                    if (dlgOpen.ShowDialog() == DialogResult.OK)
                    {
                        dlgOpen.OpenFile();
                        string file = dlgOpen.FileName;
                        StreamReader reader = new StreamReader(file);
                        var result = MessageBox.Show("Please confirm the file:" + file, "Confirm", MessageBoxButtons.OKCancel);
                        if (result == DialogResult.OK)
                        {
                            commandConstruct(OP.SETSTATE, DEV.SPI_DEV, "1" , "");
                            if (ready == true)
                            {
                                using (reader)
                                {

                                    string check;
                                    bool verified = true;
                                    do
                                    {
                                        check = reader.ReadLine();

                                    } while (check != null);
                                }
                            }
                        }

                    }
                }
                catch (Exception errorMsg)
                {
                    MessageBox.Show(errorMsg.Message);
                }
        }

Upvotes: 0

Views: 203

Answers (3)

Pramod
Pramod

Reputation: 195

If you are bundled it inside Using() then you need not worry, If not, It won't throw any error but it's just that memory chunk will be still occupied. So, If you think the file is very large and you have to have a control on memory management, then it's better to bundle up using Using()

Upvotes: 1

Lodestone6
Lodestone6

Reputation: 166

I discovered that "using" closes it on its own.

Upvotes: 0

Mukul Varshney
Mukul Varshney

Reputation: 3141

StreamReader should be closed once file is read. If you have opened the file ONLY for reading, it should be ok. Refer the link.

using (StreamReader r = new StreamReader("file.txt"))
{
  allFileText = r.ReadToEnd();
}

Upvotes: 0

Related Questions