How can I access the “Documents and Settings” folder?

大兔子大兔子 提交于 2019-11-28 12:21:07
Saravanan

You have to use like this

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

To get access to the MyDocuments folder.

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
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!