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);
}
}
}
}
}