• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

Local Directory UnauthorizedAccessException?

This is strange, building Win App (root directory) and cannot access sub-directory 'Logs' "UnauthorizedAccessException"

        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        private static string file = path + "Logs";

        private void btnRead_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read));
            using (sr)
            {
                string line = "";
                while ((line = sr.ReadToEnd()) != null)
                {
                    listBox1.Items.Add(line);
                }
            }
        }  
0
pointeman
Asked:
pointeman
  • 2
1 Solution
 
daveamourCommented:
I think you need this:

while ((line = sr.ReadLine()) != null)
Also I think your using statement should be:

using (StreamReader sr = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read)))
0
 
pointemanAuthor Commented:
I discoved it's impossible to open a 'path' as a fle, doh...
Also, the 'Using' statement works much better, thanks...
0
 
daveamourCommented:
Ok I see - I actually created a text file called Logs without a file extension to test - I thought that was what you were doing!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now