Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Local Directory UnauthorizedAccessException?

Posted on 2010-08-23
3
507 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rest webservice call over https via c# 6 49
Set value of dynamically created checkboxlist 2 42
c# - Best approach for objects in functions 3 24
Where is this file? 3 25
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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