0

I am having a very strange issue right now. I am attempting to get the files of a directory by using this code:

public static string[] fileList = Directory.GetFiles(@File.ReadAllText(Settings.localdir.Trim()));

However, when I do this, I get this error:

Access to the path 'C:\Users\lazho\Desktop' is denied.

But when I do this code:

public static string[] fileList = Directory.GetFiles(@"C:\Users\lazho\Desktop");

It works without any errors. I have no idea why one would throw an error when they both equal the same value. Any Ideas?

P.S. I have already tried this:

public static string dir = @File.ReadAllText(Settings.localdir.Trim());
    public static string[] fileList = Directory.GetFiles(dir);
  • `Directory.GetFiles(@File.ReadAllText(Settings.localdir.Trim()));` - to check we're on the same page, this will read the directory from a text file (whose location is defined by `Settings.localdir.Trim()` and then get the list of files within that directory? Does the error path represent the value provided by `Settings.localdir.Trim()` or the directory in the text file? – ProgrammingLlama Oct 22 '18 at 04:40
  • @John Yes, it reads the directory from a text file. The error path represents exactly what it should,. –  Oct 22 '18 at 04:45
  • "The error path represents exactly what it should." - Let me rephrase, is the `ReadAllText` part throwing the exception, or the `GetFiles` part? – ProgrammingLlama Oct 22 '18 at 04:50
  • @John Ah, the GetFiles. It is reading the text fine and it equals exactly what it should, but when it attempts to get the files it throws the Access denied error. –  Oct 22 '18 at 04:51
  • I'm not really sure why you're getting such an error. I've tried the same on my machine (text file points to `c:\users\john\desktop`) and your first line works for me. – ProgrammingLlama Oct 22 '18 at 05:00
  • @John Exactly, this is strange. I have tried running it as an admin as well and it still doesn't read it. This is odd because it was working before as well, and I didn't change anything about the code or the text file and it suddenly became unable to get the files. –  Oct 22 '18 at 05:03
  • Does the file have newline in the end or something? Is it *really* character by character correct, so it doesn’t have a newline in the end? – Sami Kuhmonen Oct 22 '18 at 05:07
  • @SamiKuhmonen Yep, it's completely correct. I am even trimming the file. It is only a single line, and when I print it to the console and print the quoted text they are identical. –  Oct 22 '18 at 05:10
  • You’re not trimming it in this code example at least – Sami Kuhmonen Oct 22 '18 at 05:10
  • @SamiKuhmonen Settings.localdir.Trim() is the code im using. Either way, it shouldn't matter. Here is what the file looks like: https://i.imgur.com/ulZTPYd.png –  Oct 22 '18 at 05:11
  • That trims the file path, not the contents of the file. But if a debugger says it’s exactly the same (not printing it out, checking it in a debugger for exact match) then it’s not that – Sami Kuhmonen Oct 22 '18 at 05:13
  • @SamiKuhmonen Yes, they are an exact match. –  Oct 22 '18 at 05:14
  • @LazHoll You can try simple test to check if it's really the same string `@File.ReadAllText(Settings.localdir.Trim()).equals(@"C:\Users\lazho\Desktop")` – Guy Oct 22 '18 at 05:14
  • @Guy I have tried that. They are the same. –  Oct 22 '18 at 05:22
  • You are doing two different things `ReadAllText` and `GetFiles` is not the same. opening a file seem to throw an exception but listing the files does not. – Magnus Oct 22 '18 at 06:06

1 Answers1

1

Your SecurityException is from File.ReadAllText method. you can not read drive C without permission.

ReadAllTextDocs

AmirNorouzpour
  • 1,119
  • 1
  • 11
  • 26
  • When running it as an administrator I still get this issue. –  Oct 22 '18 at 05:12
  • @Guy Directory.GetFiles only get filenames. – AmirNorouzpour Oct 22 '18 at 05:14
  • @AmirNorouzpour I am aware of that, I am trying to know what the names of all the files are on the directory specified in the text file. –  Oct 22 '18 at 05:15
  • @Guy Yes, when using `Directory.GetFiles(@"C:\Users\lazho\Desktop")` I dont get this issue, but when reading from the text file with an identical string it does not work. –  Oct 22 '18 at 05:15
  • @AmirNorouzpour The OP uses `Directory.GetFiles` in both cases. The only difference is where the path is coming from. And in both cases the path leads to `C`. – Guy Oct 22 '18 at 05:16
  • @LazHoll if you need read from Desktop use Environment.SpecialFolder.Desktop – AmirNorouzpour Oct 22 '18 at 05:19
  • @AmirNorouzpour I will need to read from any folder written on the text file. I'm just using the desktop as an example. –  Oct 22 '18 at 05:21
  • @LazHoll use this post [link](https://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line) – AmirNorouzpour Oct 22 '18 at 05:24