none
Can't access text file via StreamReader RRS feed

  • Question

  • Here is the error I have when trying to do a stream reader:

    {"Could not find file 'c:\\text.txt'.":"c:\\text.txt"}

    Why is it adding it twice?

      string appPath = System.Windows.Forms.Application.StartupPath;
                    string filePath = @"\text.txt";
                    string fullpath = Path.Combine(appPath, filePath);
                    string line = string.Empty;
                    using (StreamReader sr = new StreamReader(fullpath))
                    {
                        line = sr.ReadToEnd();
                    }

    Friday, May 11, 2018 11:43 AM

Answers

  • string filePath = @"\text.txt";           

    Remove the "\"
    • Marked as answer by ttaylor29 Friday, May 11, 2018 2:08 PM
    Friday, May 11, 2018 12:11 PM
  • Hello,

    Try the following.

    string line = string.Empty;
    using (StreamReader sr = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "text.txt")))
    {
        line = sr.ReadToEnd();
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ttaylor29 Friday, May 11, 2018 2:08 PM
    Friday, May 11, 2018 12:36 PM
    Moderator

All replies

  • string filePath = @"\text.txt";           

    Remove the "\"
    • Marked as answer by ttaylor29 Friday, May 11, 2018 2:08 PM
    Friday, May 11, 2018 12:11 PM
  • Hello,

    Try the following.

    string line = string.Empty;
    using (StreamReader sr = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "text.txt")))
    {
        line = sr.ReadToEnd();
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ttaylor29 Friday, May 11, 2018 2:08 PM
    Friday, May 11, 2018 12:36 PM
    Moderator