Thursday, June 30, 2011

Saving a file to a Location in Local System(Ex:txt)

Dear Guys,
Today I post a small article to Save a text file from our dotnet Code to specified location in Local System.
here the Process:
1.First get your data(content) into a String.

2.Create Directory as follows.

if (!Directory.Exists(SaveLocation))
Directory.CreateDirectory(SaveLocation);

*SaveLocation is a string which stores the Path where the folder will be created.

3.Append the FileName to the SaveLocation as follows.
SaveLocation += "/" + fileName +".txt";

4.Check whether file Exists or Not and Write Content into it as follows.

if (!File.Exists(SaveLocation))
{
TextWriter TW = File.CreateText(SaveLocation);
TW.WriteLine(fileContent);
TW.Close();
}

Its Done....

No comments:

Post a Comment