YaelS

Hi,

How to write method cheack if a text file was created then read (call TextFileReader())But if not --> create it (call TextFileCreate())

Open/create file:

Code Snippet

private void TextFileReader()

{

String line;

String[] newline;

try

{

using (StreamReader sr = new StreamReader("C:\\MatarotToolbar\\MatarotData.txt"))

{

while ((line = sr.ReadLine()) != null)

{

//...

}

}

Console.ReadLine();

}

catch (Exception) { }

Code Snippet

private void TextFileCreate()

{

string filepath = "C:\\MatarotToolbar\\MatarotData.txt";

using (TextWriter writer = File.CreateText(filepath))

{

writer.WriteLine("type="+ TypeTextBox.Text);

writer.WriteLine("name="+ NameTextBox.Text);

writer.WriteLine("my name="+ MyNameTextBox.Text);

writer.WriteLine("email="+ EmailTextBox.Text);

writer.Close();

}

}

Thank you!




Re: Visual C# General System.IO

Itzik Samara

using System.IO;

try

{

string FileName = //filename with path

if(File.Exists(FileName)

{

Read from File;

}

else

{

File.Create(FileName)

Read from File. if Needed;

}

}

catch(Exception)

{

}






Re: Visual C# General System.IO

YaelS

Thank's,

So I don't have method in file stream that read and apend..

I need to check it yes

Thank's!






Re: Visual C# General System.IO

Itzik Samara

u can use the Functions inside the File Class..

Open*****

which can be used to read,write in the file and then close it...

u can also read and write to the file without Stream.

but the File Functions for Write always creates new File when used.






Re: Visual C# General System.IO

YaelS

Thank's for yourreplay,

So this is the good way

try

{

string FileName = //filename with path

if(File.Exists(FileName)

{

Read from File;

}

else

{

File.Create(FileName)

Read from File. if Needed;

}






Re: Visual C# General System.IO

Itzik Samara

yep this is a good way for checking if File Exist and to read from it..