1

I've been trying to write this in order to list my folders , the append part doesn't work and it rewrites the whole thing , also if i try to list something else without exiting the app it gives the the "file is in use" error which does not make sense to me because I used close() at end of each loop.

I have checked almost all Microsoft documents and lots of posts here but couldn't find my answer .

The Picture that shows the Error I get

Thanks a lot in advance !

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Anime_Lister
{
    public partial class Anime0Lister : Form
    {


        public void Appen()
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path0save.Text, true))
            {


                DirectoryInfo directory = new DirectoryInfo(path0tb.Text);

                DirectoryInfo[] directory0Arr = directory.GetDirectories();

                foreach (DirectoryInfo dir in directory0Arr)
                {
                    String Parent = Convert.ToString(dir.Parent);
                    String Name = Convert.ToString(dir.Name);
                    String Root = Convert.ToString(dir.Root);


                    File.AppendAllText(path0save.Text, Parent);
                    File.AppendAllText(path0save.Text, "   || ");
                    File.AppendAllText(path0save.Text, Name + System.Environment.NewLine);
                    File.AppendAllText(path0save.Text, Root + System.Environment.NewLine);


                }

            }
        }

        public void Writer()
        {

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path0save.Text))
            {

                DirectoryInfo directory = new DirectoryInfo(path0tb.Text);

                DirectoryInfo[] directory0Arr = directory.GetDirectories();

                foreach (DirectoryInfo dir in directory0Arr)
                {
                    String Parent = Convert.ToString(dir.Parent);
                    String Name = Convert.ToString(dir.Name);
                    String Root = Convert.ToString(dir.Root);

                    file.Write(Parent);
                    file.Write("   || ");
                    file.WriteLine(Name);

                    file.WriteLine(Root);




                }

            }



        }


        public void Browser()
        {

            FolderBrowserDialog address = new FolderBrowserDialog();
            if (address.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                path0tb.Text = address.SelectedPath;



        }

        public void Browser2()
        {

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path0save.Text = saveFileDialog1.FileName;
            }



        }

        public Anime0Lister()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //_____________________________________________________________List







            {

                try
                {


                    if (existing0file.Checked == true)
                    {
                        Appen();
                    }
                    else
                    {
                        Writer();
                    }    

                }


                catch (Exception Fail)
                {
                    MessageBox.Show(Fail.Message);


                }



            }

        }


    }
}
Sina M.Azad
  • 27
  • 1
  • 9
  • 1
    Are you using wrong method ? Not "File.AppendAllText" but "file.Write" ? –  Jun 30 '16 at 07:10
  • on top of it in another function im using the file.appenedalltext – Sina M.Azad Jun 30 '16 at 07:11
  • if so, then you must pass "file" to that function. Otherwise how do you want to append it to a file ? Probably you create file everytime new. –  Jun 30 '16 at 07:14

1 Answers1

5

A disposable object like a StreamWriter should be always enclosed in a Using Statement to be sure, that even in case of exception, the object is closed and its unmanaged resources freed for reuse. The language gives you a specific statement for this, (Using Stetement) but, in your specific case, if you open a file using a StreamWriter then you can't use the File.AppendAllText because this method tries itself to open the file and find it 'in use' by the previous opened StreamWriter

    public void Appen()
    {
        using(System.IO.StreamWriter file = new System.IO.StreamWriter(path0save.Text, true))
        {
             DirectoryInfo directory = new DirectoryInfo(path0tb.Text);
             DirectoryInfo[] directory0Arr = directory.GetDirectories();

             foreach (DirectoryInfo dir in directory0Arr)
             {
                 // No need for those Convert.ToString. 
                 // The DirectoryInfo properties used are already strings.

                 file.Write(dir.Parent);
                 file.Write("   || ");
                 file.WriteLine(dir.Name);
                 file.WriteLine(dir.Root);

                 // or just in one line
                 //file.WriteLine(string.Concat(dir.Parent, 
                 //                      "   || ", 
                 //                      dir.Name, 
                 //                      Environment.NewLine,  
                 //                      dir.Root));  

            }
        }
    }

The same using statement should be applied to the Writer method...

Steve
  • 213,761
  • 22
  • 232
  • 286
  • thanks for taking your time and answering my question , but i still get the same error which tells me the file is in use by another process ! ( I actually used it for append part) – Sina M.Azad Jun 30 '16 at 07:18
  • I have tested this code and doesn't give any error of this kind, so, perhaps the problem is elsewhere. (also if the two textboxes points to the same folder) Are you sure that the first textbox contains a filename and not a folder name? (albeit the exception is different) – Steve Jun 30 '16 at 07:20
  • added a link to a picture that shows the problem also changed the code to full code im using at the moment (once more thank you) I'm mainly focusing on append part – Sina M.Azad Jun 30 '16 at 07:32
  • Again, looking at your code (if that's the whole code) there is nothing that should lock the file. Are you sure that the destination file is not opened elsewhere by some other app or by some different thread running on your own folder (Timer, Task, BackgroundWorker)? – Steve Jun 30 '16 at 07:36
  • ah i see your point , maybe the streamwriter from my first try is still open thank you once more also thanks for cleaning up the code ! – Sina M.Azad Jun 30 '16 at 07:40
  • it is very strange , published it and installed it on another system , when I write to a file it works just fine , but after that , when i try to append to the same .txt file I get the same error . as if the txt file is open if background , without me knowing it , is there a way to close it ? – Sina M.Azad Jun 30 '16 at 08:21
  • Did you change also the Writer method with the using statement? – Steve Jun 30 '16 at 08:24
  • yeah , changed both of them , unless i've done it in a wrong way ! (you can see the code I used in my question above , nothing is different from it) – Sina M.Azad Jun 30 '16 at 08:28
  • In the code above the Appen method still has the call to File.WriteAllText on the same file opened with the StreamWriter – Steve Jun 30 '16 at 08:41
  • ah , i see it now -_- , am i blind or something ? thanks again ! – Sina M.Azad Jun 30 '16 at 09:03
  • 1
    I don't think the first sentence is correct. See here: http://stackoverflow.com/a/7525134/148897 – Henrik Jun 30 '16 at 11:32