Solved

Local Directory UnauthorizedAccessException?

Posted on 2010-08-23
3
493 Views
Last Modified: 2012-05-10
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
Comment
Question by:pointeman
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
daveamour earned 500 total points
ID: 33501254
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
 

Author Comment

by:pointeman
ID: 33501831
I discoved it's impossible to open a 'path' as a fle, doh...
Also, the 'Using' statement works much better, thanks...
0
 
LVL 19

Expert Comment

by:daveamour
ID: 33501859
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now