C# - 无法访问所有文件 [英] C# - Cannot access all files
问题描述
我的应用程序使用.NET对象 Directory.GetFiles()
我使用的实际过载是
VAR allFiles = Directory.GetFiles(C:\\Users\\Dave,*。 *,SearchOption.AllDirectories);
的问题是当源文件夹是C:\Users\UserName因为它当时试图。期待通过Application Data文件夹
当它试图从应用程序数据文件夹读取,则抛出异常:
< BLOCKQUOTE>
访问路径C:\Users\Dave\AppData\Local\Application
数据被拒绝
块引用>
所以,我的问题是没有任何一个有意见,以我的选择?我假设我必须要么改变我收集所有的文件,或者有可能是内置的过载或方法,可以让我继续这样(我显然不知道)。
$ B的方式$ b
如果有帮助,这样做的目的是采取由检索到的所有文件Directory.GetFiles()
和粘贴他们别的地方(一个荣耀的复制和粘贴/备份)。实际上,我不太担心系统文件,只是用户文件。
目录 %APPDATA%
是一个系统保护的目录。 Windows将尝试尽快访问没有被授权(从另一个用户不是管理员的访问)块到这个目录的任何访问。
只有默认的管理员有权权限读取和/此目录的写入。
另外,你可以捕捉异常,看看结果是被拒绝访问
。然后,您可以提示用户以管理员身份来完成此步骤运行。这里有一个简单的例子来提示用户以管理员身份
试
{
VAR allFiles = Directory.GetFiles(*。*C \\Users\\Dave,SearchOption.AllDirectories);如果
}
赶上(例外EX)
{
(EX.Message.ToLower()。包含(被拒绝。))
{
的ProcessStartInfo PROC =新的ProcessStartInfo();
proc.UseShellExecute = TRUE;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb =运行方式; //需要以管理员身份
试
{
的Process.Start(PROC)运行;
}
抓
{
//用户拒绝授权
}
}
}
然而,你可能总是提示用户授权时,应用程序启动时它并不总是建议。要做到这一点,你必须编辑您的项目 app.manifest
文件
找到并更改以下行
<预类=郎咸平的XML prettyprint-覆盖>
< requestedExecutionLevel水平=asInvokeruiAccess =假/>
到
<预类=郎咸平的XML prettyprint-覆盖>
< requestedExecutionLevel水平=requireAdministratoruiAccess =FALSE/>
谢谢,
结果,我希望对您有所帮助:)
My application uses the .NET object Directory.GetFiles()
The actual overload I'm using is
var allFiles = Directory.GetFiles("C:\\Users\\Dave", "*.*", SearchOption.AllDirectories);
The issue is when the source folder is C:\Users\UserName as it then tries to look through application data folder.
When it tries to read from the application data folder, an exception is thrown:
"Access to the path 'C:\Users\Dave\AppData\Local\Application Data' is denied."
So, my question is does any one have an opinion as to my options? I would assume I have to either change the way I collect all the files or, there may be a built in overload or method which will allow me to continue this (which I clearly don't know about).
If it helps, the goal of this is to take all the files retrieved by Directory.GetFiles()
and 'paste' them else where (a glorified copy and paste/back up). I'm actually not too worried about system files, just 'user files'.
The directory %AppData%
is a system-protected directory. Windows will try to block any access to this directory as soon as the access was not authorized (An access from another user than the Administrator).
Only the Administrator by default has privileges to read and write from/to this directory.
Alternatively, you can catch the exception and see if the result is Access Denied
. Then, you may prompt the user to run as an Administrator to complete this step. Here's a simple example to prompt the user to run as Administrator
try
{
var allFiles = Directory.GetFiles("C:\\Users\\Dave", "*.*", SearchOption.AllDirectories);
}
catch (Exception EX)
{
if (EX.Message.ToLower().Contains("is denied."))
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Verb = "runas"; //Required to run as Administrator
try
{
Process.Start(proc);
}
catch
{
//The user refused to authorize
}
}
}
However, you may always prompt the user to authorize when your application launches which is NOT always RECOMMENDED. To do this, you'll have to edit your project app.manifest
file
Locate and change the following line
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
to
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Thanks,
I hope you find this helpful :)
这篇关于C# - 无法访问所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!