Ok, first of all, hi everyone at Daniweb! This is my first post on this amazing site and I'am building a little application that checks at load up if a text file is empty, and if it's empty a FolderBrowserDialog will pop up and let you choose a path wich goes into the textfile. But either or not the FolderBrowserDialog pops up and the text file is cleared.

Here's a bit of the code:

FileInfo fi = new FileInfo(@"C:\Users\Sebastian\Desktop\Konvererare BinärDecimal\Konvererare BinärDecimal\bin\Debug\path.cfg");
            StreamWriter sw = new StreamWriter(@"C:\Users\Sebastian\Desktop\Konvererare BinärDecimal\Konvererare BinärDecimal\bin\Debug\path.cfg");

            
            

            if (checkBox1.Checked)
            {

                if (fi.Length < 3)
                {
                    FolderBrowserDialog fol = new FolderBrowserDialog();
                    fol.ShowDialog();

                  
                    sw.WriteLine(fol.SelectedPath);
                    sw.Close();


                }
            }

Even if the file does contain text the FolderBrowserDialog pops up...

Edited 6 Years Ago by elmaco93: n/a

I'am building a little application that checks at load up if a text file is empty..

and if it's empty a FolderBrowserDialog will pop up and let you choose a path wich goes into the textfile.

Here's a bit of the code:

FileInfo fi = new FileInfo(@"C:\Users\Sebastian\Desktop\Konvererare BinärDecimal\Konvererare BinärDecimal\bin\Debug\path.cfg");
            StreamWriter sw = new StreamWriter(@"C:\Users\Sebastian\Desktop\Konvererare BinärDecimal\Konvererare BinärDecimal\bin\Debug\path.cfg");

            
            

            if (checkBox1.Checked)
            {

                if (fi.Length < 3)
                {
                    FolderBrowserDialog fol = new FolderBrowserDialog();
                    fol.ShowDialog();

                  
                    sw.WriteLine(fol.SelectedPath);
                    sw.Close();


                }
            }

Even if the file does contain text the FolderBrowserDialog pops up...

You might want to try this:

if (checkBox1.CheckState == CheckState.Checked)
            {

                if (fi.Length < 3)
                {
                    FolderBrowserDialog fol = new FolderBrowserDialog();
                    fol.ShowDialog();

                  
                    sw.WriteLine(fol.SelectedPath);
                    sw.Close();
                }
               else 
              {
                // Do something Or do nothing.
               }
            }

EDIT: ADD

I'm assuming that 'fi.Length' is already checking the 'fi.Text' length?

It might even be better to (as an example with a textbox)

if (textbox1.text != "";
{
                    FolderBrowserDialog fol = new FolderBrowserDialog();
                    fol.ShowDialog();

                  
                    sw.WriteLine(fol.SelectedPath);
                    sw.Close();
}

Edited 6 Years Ago by ZoMbiESsSZ: Added

Your problem is that you are opening the StreamWriter at the top of your code, even though you might not use it. If the file already holds a value then the streamwriter isnt used, then when it is disposed at the end of the method it saves its contents to file. This replaces the file with an empty one.

Try this isntead:

FileInfo fi = new FileInfo(@"C:\Users\Sebastian\Desktop\Konvererare BinärDecimal\Konvererare BinärDecimal\bin\Debug\path.cfg");

            if (checkBox1.Checked)
            {
                if (fi.Length < 3)
                {

                    using (StreamWriter sw = new StreamWriter(@"C:\Users\Sebastian\Desktop\Konvererare BinärDecimal\Konvererare BinärDecimal\bin\Debug\path.cfg"))
                    {
                        FolderBrowserDialog fol = new FolderBrowserDialog();
                        fol.ShowDialog();
                        sw.WriteLine(fol.SelectedPath);
                    }

                }
            }

By moving the streamwriter instantiation inside the If statement, you ensure that the file is only written to if it is empty.
Also, by putting it inside a using statement you ensure that it is closed and disposed of correctly after it is used.

Oh, and did you know that fi.Length returns the length of the file in bytes, not the length of the text contained in the file? To get the text length use something like:

int length = 0;
            using (StreamReader sr = new StreamReader(@"C:\Users\Sebastian\Desktop\Konvererare BinärDecimal\Konvererare BinärDecimal\bin\Debug\path.cfg"))
            {
                length = sr.ReadToEnd().Length;
            }

Again, the 'using' block ensures the streamreader is disposed correctly before your code continues. This is essential if you are going to access the file again. If you don't dispose of the streamreader/writer then it will hold a lock on your file and until it is properly disposed of you will get an IO FileInUse error if you try to read/write to the file.

Finally, you might want to include some code to handle the event where the user cancels out of the FolderBrowserDialog. If they dont select a folder then the file remains empty. If this is intended then ignore the suggestion :)

Thanks guys for all the help, will try them out as soon as possible.
And yes Ryshad I know fi.Length returns the length of the file in bytes:)

This question has already been answered. Start a new discussion instead.