问题
I'm trying to return the .config
files that exist in %WINDIR%\System32\inetsrv\config
.
For this I am using the following code:
DirectoryInfo configFolder = new DirectoryInfo(Environment.ExpandEnvironmentVariables("%WINDIR%") + @"\System32\inetsrv\");
FileInfo[] configFiles = configFolder.GetFiles("*.config");
This returns zero objects into configFiles
. If I use another folder (say D:\DropBox) is works fine!
This code used to work, has something changed??
Also, FileInfo fi = new FileInfo(Path.Combine(configPath, "applicationHost.config"));
returns ok, but fi.Length throws FileNotFoundException
.
Seems it must be permissions, but I can't see how to check if I have permissions when the code runs!
回答1:
You will need to run the code with elevated priviledges because you are trying to access asystem folder.
If you examine it with the Windows Explorer -> Properties -> Security you will find that that folder restricts access to SYSTEM, Administrators and TrustedInstaller (don't know where the last comes from, might just as well be only on my machine...).
You can configure the execution level within your App.config file like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
You can find an article here: How To Force C# Application To Only Run As Administrator In Windows
回答2:
As I am not a developer, and I only dabble with code (mostly writing admin tools for myself), I wonder if someone can explain or point me to the correct location for the answer?
Basically, I had some code from someone else's project that worked, and copied it to my own project. I'm pretty sure it worked before, but can't be 100% certain. At that time I was running x86 Windows, but I'm now on x64.
The old code still worked, so I went through copying the settings and eventually found the solution.
Setting "Platform Target" in the Project
's Build properties
to Any CPU
(from x86) made it work. Setting it to x64 also worked, but I figure it is some security thing.
Anyway, problem solved! Thanks for all your suggestions!
回答3:
This is not a permissions issue but is actually related to the SysWow64 direction that is happening behind the scenes. C:\windows\system32 is being implicitly redirected to C:\windows\syswow64. This is why changing the build architecture fixes the issue. An alternative that works with any build architecture is to disable the redirection:
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
IntPtr ptr = new IntPtr();
Wow64DisableWow64FsRedirection(ref ptr);
Note that this is a per-thread setting so it must be run in correct thread before you use GetFiles().
来源:https://stackoverflow.com/questions/9485449/directoryinfo-getfiles-method-not-returning-any-files