I would like to create a void method in C# which will create a txt file and write 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#?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Methods to look at:
File.CreateText(returns aStreamWriterfor the newly created file)TextWriter.WriteLine(note thatStreamWriterderives 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
usingstatement for theStreamWriter, so that the file handle will get closed even if an exception is thrown