質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top