Search code examples
c#textstreamreader

Reading text file not local directory


I have a text file that I would like to read into a rich text box. Everything is working fine but I am working off my USB pen and the text file is stored on that being the I:\ drive, stored in the programs debug folder. How could I make it so that no matter where I plug my USB in, and what the directory is called the program will read the file? I have got round it using my database by using a datasource but I don't know if there's such a thing for reading text files.

  using (StreamReader reader = new StreamReader(@"I:\filep\filep\bin\Debug\text.txt"))

Solution

  • You could try iterating over your drives, checking

    drive.Name + "filep\filep\bin\Debug\text.txt"
    

    on each pass to see if the file exists.

    Edit from MethodMan's comment: if you know for sure your file lives on a thumb drive, you'll want this check in there as well

    drive.DriveType == DriveType.Removal