Console output copied into .txt file

Get the console output copied into .txt file

c# code:

static void Main(string[] args)
{

A obj = new A();

using (StreamWriter writer = new StreamWriter(“C:out.txt”))
{
Console.SetOut(writer);
obj.OpenFiles(@”C:Usersashish.fugatDocumentsvisual studio 2010ProjectsConsoleApp”);
}

Console.ReadLine();

}

class A
{
int i = 0;
string ParentDir = “”;

public void OpenFiles(string directoryPath)
{
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
FileStream fileStream = null;
string TabS = “”;
if (directoryInfo.Exists)
{
FileInfo[] fileInfo = directoryInfo.GetFiles();
DirectoryInfo[] subdirectoryInfo = directoryInfo.GetDirectories();
if (i == 0)
{
ParentDir = directoryInfo.FullName;
Console.WriteLine(directoryInfo.Name);
Console.WriteLine(“t|”);

}

foreach (DirectoryInfo subDirectory in subdirectoryInfo)
{
i++;
for (int j = 0; j < i; j++)
{
TabS += “t”;
}

Console.WriteLine(TabS + “–{0} “, subDirectory.Name);

Console.WriteLine(TabS + “t|”);
TabS = “”;
OpenFiles(subDirectory.FullName);
–i;

}

foreach (FileInfo file in fileInfo)
{
try
{
fileStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);

if (file.DirectoryName == ParentDir)
{
Console.WriteLine(“|”);
Console.WriteLine(“–[{0}] “, file.Name);
}
else
{
for (int j = 0; j < i; j++)
{
TabS += “t”;
}
Console.WriteLine(TabS + “t|[{0}]”, file.Name); TabS = “”;
}
}
catch (IOException)
{
//The exception is thrown when the file is unavailable (i.e. locked) because it is in use by other thread
//Log.WriteInformation(“some others are using this file”);
Console.WriteLine(“some others are using this file”);
}
finally
{
if (fileStream != null)
fileStream.Close();
}
}
}
//Console.ReadLine();

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s