I am creating ac# program to output a list of folders and files from a removable drive such as a USB stick or Disc to a text file.
I have a combo box ( cbo1
) which contains the drives, the start button which is suppose to create the text file and a refresh button ( btnRefresh
) which refreshes and displays what drives are available.
I'm currently working on the start button and using StreamWriter
, but for some reason (I can't see why) it's not working.
Any idea's? If you need any other info let me know...
private void enumerateFiles()
{
using (StreamWriter writer = new StreamWriter(
@"C:\\Users\\r.samways\\Desktop\\test.txt"))
{
string selValue = Cbo1.SelectedValue.ToString();
var _files = System.IO.Directory.EnumerateFiles(
selValue,
"*.*",
SearchOption.AllDirectories);
foreach (string file in _files)
{
var fileinfo = new FileInfo(file);
var _fileLength = fileinfo.Length;
var _fileName = fileinfo.Name;
var _fileFullname = fileinfo.FullName;
writer.WriteLine(_fileFullname);
{writer.Flush();}
}
If you use the @ at the start of your path you don't need to escape the back slash. Try this as your path
@"C:\\Users\\r.samways\\Desktop\\test.txt"
try:
StreamWriter writer = new StreamWriter(@"C:\\Users\\r.samways\\Desktop\\test.txt",false)
also don't forget to close your StreamWriter when finished.
writer.Close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.