-3

In my application every user can set his own save path to save his files and settings so every time the user log in i must search a folder that contains the username+"Data" for example if the user name was "Kim" i need to find the path to the folder KimData when i try to get all directories in C:\ the UnauthorizedAcessException appears

so is there a way to search for that folder or just skip the unauthorized folders while searching ?

tnw
  • 13,521
  • 15
  • 70
  • 111
Th3Wolf
  • 149
  • 1
  • 10
  • just launch Visual studio as Admin – Coder1409 Jun 01 '15 at 16:31
  • 1
    @Coder1409 obviously running as admin is not going to help as not every folder have permissions set up for admin to look at without taking ownership. – Alexei Levenkov Jun 01 '15 at 16:32
  • 1
    if you are trying to find currently logged in user's folder then use : `Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)` – Habib Jun 01 '15 at 16:32
  • Th3Wolf - can you please list related question you've already checked out and explain what problems you can't solve with searching for answer. – Alexei Levenkov Jun 01 '15 at 16:33
  • @AlexeiLevenkov I've searched the whole "internet" :D but no answer my Probleme is to find ta folder path just by it name – Th3Wolf Jun 01 '15 at 16:39
  • The only method I know of to solve the issue is to write you own recursive find method searching one folder at a time and then wrapping the method in an exception handler so you can continue after the exception. – jdweng Jun 01 '15 at 16:40
  • Look at my answer. I know you didn't search the whole internet, because I did a quick google search and found many answers on stack that may lead to your answer. When you google, search specific, but not too specific. – Adam Jun 01 '15 at 16:42
  • @WhyCry in my application each user creates his login and password he sets his saving path for example in MyDocuments the folder name will be the username+"Data" i need to find the path to that folder each time the user login to my application is there a methode that gives you the path to a folder by using the folder name ? :D Thank you for your time :) – Th3Wolf Jun 01 '15 at 16:50
  • I don't know what you mean "searched whole internet", but try following searches that involve method name and exception name - https://www.bing.com/search?q=c%23+getfiles+UnauthorizedAccessException, https://www.bing.com/search?q=c%23+getfolders+UnauthorizedAccessException – Alexei Levenkov Jun 01 '15 at 16:52
  • @Th3Wolf. have you looked at my links to some related questions? – Adam Jun 01 '15 at 17:05
  • @WhyCry Yes Thank you but i can't find an answer to my probleme maybe i didn't understand the answers in your links but i just want to find a path to a floder by name is it possible ? – Th3Wolf Jun 01 '15 at 17:07
  • @Th3Wolf http://stackoverflow.com/questions/3736462/getting-the-folder-name-from-a-path – Adam Jun 01 '15 at 17:08
  • @WhyCry i see that they already know the path but in my case the user can choose his path to save settings the in a folder with the name +"Data" i need to find the path to that folder so the user can restore his settings – Th3Wolf Jun 01 '15 at 17:13

2 Answers2

0

The UnauthorizedAccessExpection means that the caller does not have the required permission to access the directory/file. Since you're doing it locally, there are several options. After you attempt to copy the data from VS to the data in the file (Create, copy, delete) etc.... you can try File.SetAttributes(yourfile, FileAttributes.Normal).

You can also use Environment.GetFolderPath. Accordingly, this:

"Gets the path to the system special folder that is identified by the specified enumeration, and uses a specified option for accessing special folders."

Also, I'm guessing you're simply looking through the entire directory/folder/path all at once. A workaround would be to probe one directory at a time. This is assuming you are adding a file. Once you've found your directory, you can use:

    Directory.GetFiles(path)
        .ToList()
        .ForEach(s => files.Add(s));

    Directory.GetDirectories(path)
        .ToList()
        .ForEach(s => AddFiles(s, files));

EDIT: Some helpful related questions on stack to look at might be:

UnauthorizedAccessException

Directory.GetFiles

Community
  • 1
  • 1
Adam
  • 2,422
  • 18
  • 29
0

Take a look at Ignore folders/files when Directory.GetFiles() is denied access and see if it helps you.

However if you are trying to look for a specific folder - which according to your question the user can place anywhere - in the entire directory tree i advise against it as it probably will be slow.

I would recommend saving the path somewhere and reading it from there when the user logs back on.

If its a desktop app and each user runs the app with its own windows account an even better solution would be to always write the data to the user's ApplicationData folder which you can get with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData). As the name indicates this folder exists to keep applications data and its individual for the user logged in to windows

Community
  • 1
  • 1
JoaoFSA
  • 267
  • 1
  • 7
  • Thank you @JoaoFSA i've checked [link](http://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access) in my appilcation the users don't log in by windows account they create their accounts in my application i know that saving a new file that contains each user's path is a solution but i wondred if there is another solution to get the pathe without that file :D Thanks for your answer – Th3Wolf Jun 01 '15 at 17:18
  • @Th3Wolf The users don't need to sign in to your application with their windows account, they only need to **run the application while logged in to windows with their own account**. The code i pasted will get the directory for the windows account that is being used to run the application not for the user implented on your app – JoaoFSA Jun 01 '15 at 17:29
  • @Th3Wolf If you still go with scanning the directory tree keep in mind that a folder with the same name can already exist somewhere else and the first folder with that name that you find might not actually be the one that contains the user settings – JoaoFSA Jun 01 '15 at 17:31