Path access error denied

Asked

Viewed 6,570 times

2

I am with a program created in Visual Studio / Windows Forms. In it access the following file to get some information:

1st Code (works normal)

StreamReader file = new StreamReader(@ "E:\OneDrive\VisualStudio\codigosAIH\codigosAIH\bin\Debug\tb_cid.txt", Encoding.GetEncoding("iso-8859-1"));

I changed the code for the program to search the file in the installation folder:

2º Code (gives access error denied)

StreamReader file = new StreamReader(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\tb_cid.txt"), Encoding.GetEncoding("iso-8859-1"));

In doing so, I get the Access to path error denied. I have used a Messagebox to confirm the path and it is correct.

I don’t think it’s a permission issue, otherwise I wouldn’t be able to access it in the form of the first code.

What can it be?

  • What is the first example?

  • I refer to the first code with the full written address

  • There’s only one little piece of code in there, is he first? If he is, what’s second?

  • amended the post, ask you to look again ^^

  • You are wanting to open the txt?

  • yes, I want to read the file. (I didn’t post the rest of the code because everything works normal, only this line gives error when I try to get the file path)

  • 1

    Please stop using the tag visual-studio in your questions that are not about Visual Studio =D

  • honestly now I thought I should use. sorry again

  • The first is not a code, it is a loose address. If you put this and try to compile with C# it will give error because this is not a code.

  • got it. I referred the wrong way. corrected again

Show 5 more comments

1 answer

5


Path.GetDirectoryName returns the directory of a given path.

So that

Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.ToString() + "\tb_cid.txt");

will return the way without the file name. Just put the parenthesis in the right place.

Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.ToString()) + @"\tb_cid.txt";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.