I would like to create a void method in C# which will create a txt file and writ开发者_开发技巧e all the paths of all the file names on a computer.
There is a cmd command, (dir c:\ /s /b >c:\bla.txt
), but I don't want that.
How can I do it in C#?
Methods to look at:
File.CreateText
(returns aStreamWriter
for the newly created file)TextWriter.WriteLine
(note thatStreamWriter
derives fromTextWriter
)Directory.EnumerateFiles
(avoids reading all the files in a single call - instead, it will let you iterate over them usingforeach
)
Don't forget a using
statement for the StreamWriter
, so that the file handle will get closed even if an exception is thrown
Try this:
void GetFiles()
{
StreamWriter wr = File.CreateText(Server.MapPath("") + "txt1.text");
DriveInfo[] drive = DriveInfo.GetDrives();
for (long count = 0; count < drive.LongLength; count++)
{
string[] dir = Directory.GetDirectories(drive.GetValue(count).ToString(), "*", SearchOption.AllDirectories);
for (long dr = 0; dr < dir.LongLength; dr++)
{
string[] file = Directory.GetFiles(dir.GetValue(dr).ToString(), "*", SearchOption.AllDirectories);
for (long fl = 0; fl < file.LongLength; fl++)
{
wr.WriteLine(file.GetValue(fl));
}
}
}
}
Code is below. It is written on Mono on Mac OS X, so you'll need to change the directory. Also, if you don't have and run under administrator, it'll probably throw all kinds of errors in certain directories.
class MainClass
{
public static void Main (string[] args)
{
WritePaths("files.txt", "/", true);
}
public static void WritePaths(string fileName, string directory, bool recursive)
{
try
{
string[] files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
using(StreamWriter sw = new StreamWriter(fileName))
{
foreach(string file in files)
{
sw.WriteLine(file);
}
}
}
catch(Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
}
Based on other answers I've modified my method to use the newer Directory.EnumerateFiles
. I like this version much better.
public static void WritePaths(string fileName, string directory)
{
try
{
using(StreamWriter sw = new StreamWriter(fileName))
{
foreach (string f in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories))
{
sw.WriteLine(f);
}
}
}
catch(Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
I would suggest to write a recursive method although it cannot be a void method. I would iterate through a given path say C:. or take out all drives using Drives System.IO class and pass to the method. Then in the method I would iterate and rip out the filename in the directory and subdirectory.
There is a cmd command, (
dir c:\ /s /b >c:\bla.txt
), but I don't want that.
You don't want to manually type the command and execute it in the Windows console, or you don't want to use the DIR command (you want to implement in C# yourself)?
If you just want a C# program that handles the manual typing for you, you can start a process using C# and run the built-in DIR command. See Process.Start.
Process.Start("cmd", @"/c dir c:\ /s /b >c:\bla.txt");
Simple, readable, and I'm 100% sure there are no bugs. So why not?
精彩评论