How can I access the "Documents and Settings" folder?
Asked Answered
M

3

7

I'm using C# .NET 4 with VS 2010.

When Iterating over some paths, I'm running this line:

files = Directory.GetFiles(path, searchPattern);

I get an exception when the path is the documents and settings folder. How can I access it? And no, I don't want to skip the folder with a try and catch. I want to be able to access it somehow.

Edit: I got a follow up question. As I told you, I'm iterating over the paths. Is there a way to use Environment.GetFolderPath but somehow idetifying the correct speical folder according to the path I'm currently checking?

Momism answered 25/5, 2013 at 18:11 Comment(0)
S
10

You have to use like this

var mydocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

To get access to the MyDocuments folder.

Sylvester answered 25/5, 2013 at 18:14 Comment(7)
I edited my question to add a follow up question if you may know the answer to that as well.Momism
you can very well equate the path that you have with this Environment.GetFolderPath(...) and if matched, you can continue or else you can just call Continue in the looping construct.Sylvester
I meant something automatically. something like (pseudo code): SpeicalFolder folder = Environment.GetSpeicalFolderByFullPath(path);Momism
No.. Environment.GetFolderPath takes only the enumeration as a path. Not a string.Sylvester
I would like to suggest you the following. During the application start, you can just cache the full path of all of the special folders and then check them during runtime. Let me know if that would be fine for your requirement.Sylvester
There's something I don't understand. The path that threw and exception is C:\\Documents and Settings and the path that was given to me by the environment is C:\\Users\\Yonatan\\Documents. How can I compare the paths during runtime to know that the path that threw an exception was actually supposed to be the other special path? I can't just do if(path == "C:\\Documents and Settings") since it might change between different computers.Momism
Please note that the path that contains the Documents and Settings pertain to Windows XP and the Users\... belongs to Windows 7 and higher, vista also i guess. So you should be also checking for the OSVersion too i guess.Sylvester
R
5

From Environment.SpecialFolder

The system special folders are folders such as Program Files, Programs, System, or Startup, which contain common information. Special folders are set by default by the system, or explicitly by the user, when installing a version of Windows.

The GetFolderPath method returns the locations associated with this enumeration. The locations of these folders can have different values on different operating systems, the user can change some of the locations, and the locations are localized.

Just use

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
files = Directory.GetFiles(path, searchPattern);

In my computer, it returns as C:\Users\Soner\Documents

Is there a way to use Environment.GetFolderPath but somehow idetifying the correct speical folder according to the path I'm currently checking?

Since SpecialFolder is enum type, you can iterate their values in a loop. Here how it looks like;

public enum SpecialFolder
{
    AdminTools = 0x30,
    ApplicationData = 0x1a,
    CDBurning = 0x3b,
    CommonAdminTools = 0x2f,
    CommonApplicationData = 0x23,
    CommonDesktopDirectory = 0x19,
    CommonDocuments = 0x2e,
    CommonMusic = 0x35,
    CommonOemLinks = 0x3a,
    CommonPictures = 0x36,
    CommonProgramFiles = 0x2b,
    CommonProgramFilesX86 = 0x2c,
    CommonPrograms = 0x17,
    CommonStartMenu = 0x16,
    CommonStartup = 0x18,
    CommonTemplates = 0x2d,
    CommonVideos = 0x37,
    Cookies = 0x21,
    Desktop = 0,
    DesktopDirectory = 0x10,
    Favorites = 6,
    Fonts = 20,
    History = 0x22,
    InternetCache = 0x20,
    LocalApplicationData = 0x1c,
    LocalizedResources = 0x39,
    MyComputer = 0x11,
    MyDocuments = 5,
    MyMusic = 13,
    MyPictures = 0x27,
    MyVideos = 14,
    NetworkShortcuts = 0x13,
    Personal = 5,
    PrinterShortcuts = 0x1b,
    ProgramFiles = 0x26,
    ProgramFilesX86 = 0x2a,
    Programs = 2,
    Recent = 8,
    Resources = 0x38,
    SendTo = 9,
    StartMenu = 11,
    Startup = 7,
    System = 0x25,
    SystemX86 = 0x29,
    Templates = 0x15,
    UserProfile = 40,
    Windows = 0x24
}
Retral answered 25/5, 2013 at 18:17 Comment(2)
I edited my question to add a follow up question if you may know the answer to that as well.Momism
Is there a way to do it automatically? Something like (pseudo code): SpeicalFolder folder = Environment.GetSpeicalFolderByFullPath(path); Also, There's something I don't understand. The path that threw and exception is C:\\Documents and Settings and the path that was given to me by the environment is C:\\Users\\Yonatan\\Documents. How can I compare the paths during runtime to know that the path that threw an exception was actually supposed to be the other special path? I can't just do if(path == "C:\\Documents and Settings") since it might change between different computers.Momism
D
0

You can set the program so you can only run as administrator.

In Visual Studio:

Right click on the Project -> Properties -> Security -> Enable ClickOnce Security

After you clicked it, a file will be created under the Project's properties folder called app.manifest once this is created, you can uncheck the Enable ClickOnce Security Settings option

Open that file and change this line :

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

to:

 <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

This will make the program require administrator privileges, and it will guarantee you have access to that folder.

Donaldson answered 25/5, 2013 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.