none
Read FileInfo FullName from one directory, write all lines to a 2nd file

    Question

  • Hello all:

    I have a directory containing .mp3 files and want to use the FileInfo object's FullName property to get the full path and file name from each file and write it to a text file in another directory.

    I've tried using the following but after trying a couple of different methods, I either get only the last of the .mp3 file names or receive the exception "System.IO.IOException was unhandled
      Message=The process cannot access the file 'C:\Another folder\testFile.txt' because it is being used by another process."

    I need to loop through and write append each file name and its directory path into a text file. This is what I've tried so far.

    public static void WriteToFile()
    {
        DirectoryInfo myDir = new DirectoryInfo(@"C:\ a folder\some files");
    
        string path = @"C:\Another folder\testFile.txt";
    
        foreach (FileInfo f in myDir.GetFiles("*.mp3"))
        {
            //File.AppendAllLines(path, f.FullName);
            File.Open(path, FileMode.Append);	
        }
    }

    I've also see examples of using a StreamWriter, but the examples seem rather hardcoded, i.e. sw.WriteLine("arbitrary Text")

    sw.WriteLine("More arbitrary Text")

    sw.Close();

    However, I need to capture the FileInfo FullName of each .mp3 file in the first directory (acquiring it's folder path and full name) and write each of those into a text file and save it with a given name.

    Can someone please suggest how I can accomplish this? Thank you

    Monday, August 19, 2013 12:56 AM

Answers

  • How about something like:

      public static void WriteToFile()
      {
       DirectoryInfo myDir = new DirectoryInfo(@"C:\ a folder\some files");
       string path = @"C:\Another folder\testFile.txt";
       using (StreamWriter l_writer = new StreamWriter(path, false))
       {
        foreach (FileInfo f in myDir.GetFiles("*.mp3"))
        {
         l_writer.WriteLine(f.FullName);
        }
       }
      }


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by CodingMann Monday, August 19, 2013 11:24 PM
    Monday, August 19, 2013 3:33 AM

All replies

  • How about something like:

      public static void WriteToFile()
      {
       DirectoryInfo myDir = new DirectoryInfo(@"C:\ a folder\some files");
       string path = @"C:\Another folder\testFile.txt";
       using (StreamWriter l_writer = new StreamWriter(path, false))
       {
        foreach (FileInfo f in myDir.GetFiles("*.mp3"))
        {
         l_writer.WriteLine(f.FullName);
        }
       }
      }


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by CodingMann Monday, August 19, 2013 11:24 PM
    Monday, August 19, 2013 3:33 AM
  • Hello TSoftware and thank you for your very helpful answer.

    This works great and is just what I've been trying to do, but couldn't quite get the syntax correct.

    It does indeed loop through each .mp3 file and write the full path and file name into another file which my application will make use of.

    Thank you again for your input and ready response.    :-)

    Monday, August 19, 2013 11:24 PM
  • Glad I could be of help.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Tuesday, August 20, 2013 2:39 AM