Click here to Skip to main content
16,016,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DirectoryInfo Dir = new DirectoryInfo(strpath); 
FileInfo[] FileList = Dir.GetFiles("*.krs*"); 
foreach (FileInfo FI in FileList ) 
{ 
listBox1.Items.Add(FI.Name); //write this list to a text file please help me 
} 
Posted
Updated 1-Jul-11 2:34am
v3

DirectoryInfo dir = new DirectoryInfo(@"D:\Logs\BodyShop.US\Warning");
FileInfo[] fi = dir.GetFiles("*.log");

StreamWriter sw = new StreamWriter(@"C:\test.txt");

foreach (FileInfo f in fi)
{
   listBox1.Items.Add(f.Name);
   sw.WriteLine(f.Name);
}
sw.Close();
 
Share this answer
 
Use a stringbuilder to build your list in to a string that you want to write, then File.WriteAllText.
 
Share this answer
 
I'd first try this one
using (System.IO.FileStream fs = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None)) 
{
  foreach (FileInfo FI in FileList )
  {
    listBox1.Items.Add(FI.Name);
  
    //write this list to a text file please help me
    byte[] writeFileName = ASCIIEncoding.GetBytes(FI.Name);
    fs.Write(writeFileName, 0, writeFileName.Length);
  }
}
Maybe ASCIIEncoding is not what you want, then try another encoding like UTF8Encoding or so.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900