Path access error was denied

2

I am using 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 so that the program looks for the file in the installation folder:

2nd Code (give access error denied)

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

Doing so, I get the Path Access error was denied. I have already used a MessageBox to confirm the path and it is correct.

I do not think it's a permission problem, otherwise I would not be able to access the form of the first code.

What can it be?

    
asked by anonymous 19.01.2017 / 17:23

1 answer

4

Path.GetDirectoryName returns the directory of a given path.

So, this

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

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

Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.ToString()) + @"\tb_cid.txt";
    
19.01.2017 / 17:38