Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';不要列出一些文件夹_C#_.net_Windows 7 - Fatal编程技术网

C# Can';不要列出一些文件夹

C# Can';不要列出一些文件夹,c#,.net,windows-7,C#,.net,Windows 7,当我尝试列出电脑上的一些文件夹(如“c:\Documents and Settings”)时,出现“拒绝访问”异常。我的应用程序正在从管理员帐户运行,并具有提升的权限() 我还需要做些什么才能访问这些文件夹?以管理员身份运行是必要的,但还不够,您还需要启用权限,如此处所示: %SystemDrive%\Documents and Settings目录现在存储在%SystemDrive%\Users目录中 […]备份应用程序必须能够备份和恢复 连接点 下面是一个有效的例子: using (Priv

当我尝试列出电脑上的一些文件夹(如“c:\Documents and Settings”)时,出现“拒绝访问”异常。我的应用程序正在从管理员帐户运行,并具有提升的权限(


我还需要做些什么才能访问这些文件夹?

以管理员身份运行是必要的,但还不够,您还需要启用权限,如此处所示:

%SystemDrive%\Documents and Settings目录现在存储在%SystemDrive%\Users目录中 […]备份应用程序必须能够备份和恢复 连接点

下面是一个有效的例子:

using (Privilege p = new Privilege(Privilege.Backup))
{
    foreach (string path in Directory.GetFileSystemEntries(@"c:\documents and settings"))
    {
        Console.WriteLine(path);
    }
}
注意,据我所知,没有公共.NET类以编程方式启用特权,但System.Security.AccessControl(mscorlib)中有一个内部类,我已使用精简包装器公开了它。它基于完全公开的窗口

//
///定义Windows权限。
/// 
公共密封类权限:IDisposable
{
私有静态类型_privilegeType;
私有对象特权;
静态特权()
{
_privilegeType=typeof(string.Assembly.GetType(“System.Security.AccessControl.Privilege”,false);//mscorlib
}
/// 
///初始化类的新实例并启用特权。
/// 
///特权名称。
公共特权(字符串名称)
:此(名称,true)
{
}
/// 
///初始化类的新实例。
/// 
///特权名称。
///如果设置为true,则启用特权。
公共权限(字符串名称,bool enable)
{
if(name==null)
抛出新的ArgumentNullException(“名称”);
if(_privilegeType==null)
抛出新的NotSupportedException();
_privilege=\u privilegegetype.GetConstructors()[0]。调用(新对象[]{name});
如果(启用)
{
启用();
}
}
/// 
///从当前线程禁用此权限。
/// 
公屋
{
_privilegeType.InvokeMember(“Revert”,BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,null,_privilege,null);
}
/// 
///获取一个值,该值指示是否必须调用Revert。
/// 
/// 
///如果必须调用Revert,则为true;否则为false。
/// 
公共学校需要移交
{
得到
{
return(bool)_privilegeType.InvokeMember(“NeedToRevert”,BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty,null,_privilege,null);
}
}
/// 
///对当前线程启用此权限。
/// 
公共作废启用()
{
_privilegeType.InvokeMember(“Enable”,BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,null,_privilege,null);
}
/// 
///执行与释放、释放或重置非托管资源相关的应用程序定义的任务。
/// 
公共空间处置()
{
如果(需要转换)
{
回复();
}
}
/// 
///SE_ASSIGNPRIMARYTOKEN_名称特权。
/// 
public const string AssignPrimaryToken=“SeAssignPrimaryTokenPrivilege”;
/// 
///SE_AUDIT_NAME权限。
/// 
public const string Audit=“SeAuditPrivilege”;
/// 
///SE_BACKUP_NAME特权。
/// 
public const string Backup=“SeBackupPrivilege”;
/// 
///SE_CHANGE_NOTIFY_NAME权限。
/// 
public const string ChangeNotify=“SeChangeNotifyPrivilege”;
/// 
///SE_创建_全局_名称权限。
/// 
public const string CreateGlobal=“SeCreateGlobalPrivilege”;
/// 
///SE_CREATE_PAGEFILE_NAME权限。
/// 
public const string CreatePageFile=“SeCreatePagefilePrivilege”;
/// 
///SE_创建_永久名称权限。
/// 
public const string CreatePermanent=“SeCreatePermanentPrivilege”;
/// 
///SE_CREATE_SYMBOLIC_LINK_NAME权限。
/// 
public const string CreateSymbolicLink=“secreateSymbolicClinkPrivilege”;
/// 
///SE_CREATE_TOKEN_NAME权限。
/// 
public const string CreateToken=“SeCreateTokenPrivilege”;
/// 
///SE_DEBUG_NAME权限。
/// 
public const string Debug=“SeDebugPrivilege”;
/// 
///SE_ENABLE_DELEGATION_NAME权限。
/// 
public const string EnableDelegation=“SeEnableDelegationPrivilege”;
/// 
///SE_模拟_名称权限。
/// 
public const string Impersonate=“SeImpersonatePrivilege”;
/// 
///SE_INC_BASE_PRIORITY_NAME特权。
/// 
public const string IncreaseBasePriority=“SeIncreaseBasePriorityPrivilege”;
/// 
///SE_增加_配额_名称权限。
/// 
public const string IncreaseQuota=“SeIncreaseQuotaPrivilege”;
/// 
///SE_INC_WORKING_SET_NAME特权。
/// 
public const string IncreaseWorkingSet=“SeIncreaseWorkingSetPrivilege”;
/// 
///SE_LOAD_DRIVER_NAME权限。
/// 
public const string LoadDriver=“SeLoadDriverPrivilege”;
/// 
///SE_LOCK_MEMORY_NAME特权。
/// 
public const string LockMemory=“SeLockMemoryPrivilege”;
/// 
///SE_MACHINE_ACCOUNT_NAME权限。
/// 
public const string MachineAccount=“SeMachineAccountPrivilege”;
/// 
///SE_MANAGE_VOLUME_NAME权限。
/// 
public const string ManageVolume=“SeManageVolumePrivilege”;
/// 
///SE_PROF_SINGLE_PROCESS_NAME权限。
/// 
public const string ProfileSingleProcess=“SeProfileSingleProcessPrivilege”;
/// 
///SE_RELABEL_NAME特权。
/// 
public const string Relabel=“SeRelabelPrivilege”;
/// 
///SE_远程\u关机\u名称权限。
/// 
public const string RemoteShutdown=“SeRemoteShutdownPrivilege”;
/////
/// <summary>
/// Defines a Windows privilege.
/// </summary>
public sealed class Privilege : IDisposable
{
    private static Type _privilegeType;
    private object _privilege;

    static Privilege()
    {
        _privilegeType = typeof(string).Assembly.GetType("System.Security.AccessControl.Privilege", false); // mscorlib
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Privilege"/> class and enable the privilege.
    /// </summary>
    /// <param name="name">The privilege name.</param>
    public Privilege(string name)
        : this(name, true)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Privilege" /> class.
    /// </summary>
    /// <param name="name">The privilege name.</param>
    /// <param name="enable">if set to <c>true</c> the privilege is enabled.</param>
    public Privilege(string name, bool enable)
    {
        if (name == null)
            throw new ArgumentNullException("name");

        if (_privilegeType == null)
            throw new NotSupportedException();

        _privilege = _privilegeType.GetConstructors()[0].Invoke(new object[] { name });
        if (enable)
        {
            Enable();
        }
    }

    /// <summary>
    /// Disable this privilege from the current thread. 
    /// </summary>
    public void Revert()
    {
        _privilegeType.InvokeMember("Revert", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, _privilege, null);
    }

    /// <summary>
    /// Gets a value indicating whether Revert must be called.
    /// </summary>
    /// <value>
    ///   <c>true</c> if Revert must be called; otherwise, <c>false</c>.
    /// </value>
    public bool NeedToRevert
    {
        get
        {
            return (bool)_privilegeType.InvokeMember("NeedToRevert", BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, _privilege, null);
        }
    }

    /// <summary>
    /// Enables this privilege to the current thread. 
    /// </summary>
    public void Enable()
    {
        _privilegeType.InvokeMember("Enable", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, _privilege, null);
    }

    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
        if (NeedToRevert)
        {
            Revert();
        }
    }

    /// <summary>
    /// The SE_ASSIGNPRIMARYTOKEN_NAME privilege.
    /// </summary>
    public const string AssignPrimaryToken = "SeAssignPrimaryTokenPrivilege";

    /// <summary>
    /// The SE_AUDIT_NAME privilege.
    /// </summary>
    public const string Audit = "SeAuditPrivilege";

    /// <summary>
    /// The SE_BACKUP_NAME privilege.
    /// </summary>
    public const string Backup = "SeBackupPrivilege";

    /// <summary>
    /// The SE_CHANGE_NOTIFY_NAME privilege.
    /// </summary>
    public const string ChangeNotify = "SeChangeNotifyPrivilege";

    /// <summary>
    /// The SE_CREATE_GLOBAL_NAME privilege.
    /// </summary>
    public const string CreateGlobal = "SeCreateGlobalPrivilege";

    /// <summary>
    /// The SE_CREATE_PAGEFILE_NAME privilege.
    /// </summary>
    public const string CreatePageFile = "SeCreatePagefilePrivilege";

    /// <summary>
    /// The SE_CREATE_PERMANENT_NAME privilege.
    /// </summary>
    public const string CreatePermanent = "SeCreatePermanentPrivilege";

    /// <summary>
    /// The SE_CREATE_SYMBOLIC_LINK_NAME privilege.
    /// </summary>
    public const string CreateSymbolicLink = "SeCreateSymbolicLinkPrivilege";

    /// <summary>
    /// The SE_CREATE_TOKEN_NAME privilege.
    /// </summary>
    public const string CreateToken = "SeCreateTokenPrivilege";

    /// <summary>
    /// The SE_DEBUG_NAME privilege.
    /// </summary>
    public const string Debug = "SeDebugPrivilege";

    /// <summary>
    /// The SE_ENABLE_DELEGATION_NAME privilege.
    /// </summary>
    public const string EnableDelegation = "SeEnableDelegationPrivilege";

    /// <summary>
    /// The SE_IMPERSONATE_NAME privilege.
    /// </summary>
    public const string Impersonate = "SeImpersonatePrivilege";

    /// <summary>
    /// The SE_INC_BASE_PRIORITY_NAME privilege.
    /// </summary>
    public const string IncreaseBasePriority = "SeIncreaseBasePriorityPrivilege";

    /// <summary>
    /// The SE_INCREASE_QUOTA_NAME privilege.
    /// </summary>
    public const string IncreaseQuota = "SeIncreaseQuotaPrivilege";

    /// <summary>
    /// The SE_INC_WORKING_SET_NAME privilege.
    /// </summary>
    public const string IncreaseWorkingSet = "SeIncreaseWorkingSetPrivilege";

    /// <summary>
    /// The SE_LOAD_DRIVER_NAME privilege.
    /// </summary>
    public const string LoadDriver = "SeLoadDriverPrivilege";

    /// <summary>
    /// The SE_LOCK_MEMORY_NAME privilege.
    /// </summary>
    public const string LockMemory = "SeLockMemoryPrivilege";

    /// <summary>
    /// The SE_MACHINE_ACCOUNT_NAME privilege.
    /// </summary>
    public const string MachineAccount = "SeMachineAccountPrivilege";

    /// <summary>
    /// The SE_MANAGE_VOLUME_NAME privilege.
    /// </summary>
    public const string ManageVolume = "SeManageVolumePrivilege";

    /// <summary>
    /// The SE_PROF_SINGLE_PROCESS_NAME privilege.
    /// </summary>
    public const string ProfileSingleProcess = "SeProfileSingleProcessPrivilege";

    /// <summary>
    /// The SE_RELABEL_NAME privilege.
    /// </summary>
    public const string Relabel = "SeRelabelPrivilege";

    /// <summary>
    /// The SE_REMOTE_SHUTDOWN_NAME privilege.
    /// </summary>
    public const string RemoteShutdown = "SeRemoteShutdownPrivilege";

    ///// <summary>
    ///// The SE_RESERVE_PROCESSOR_NAME privilege.
    ///// </summary>
    //public const string ReserveProcessor = "SeReserveProcessorPrivilege";

    /// <summary>
    /// The SE_RESTORE_NAME privilege.
    /// </summary>
    public const string Restore = "SeRestorePrivilege";

    /// <summary>
    /// The SE_SECURITY_NAME privilege.
    /// </summary>
    public const string Security = "SeSecurityPrivilege";

    /// <summary>
    /// The SE_SHUTDOWN_NAME privilege.
    /// </summary>
    public const string Shutdown = "SeShutdownPrivilege";

    /// <summary>
    /// The SE_SYNC_AGENT_NAME privilege.
    /// </summary>
    public const string SyncAgent = "SeSyncAgentPrivilege";

    /// <summary>
    /// The SE_SYSTEM_ENVIRONMENT_NAME privilege.
    /// </summary>
    public const string SystemEnvironment = "SeSystemEnvironmentPrivilege";

    /// <summary>
    /// The SE_SYSTEM_PROFILE_NAME privilege.
    /// </summary>
    public const string SystemProfile = "SeSystemProfilePrivilege";

    /// <summary>
    /// The SE_SYSTEMTIME_NAME privilege.
    /// </summary>
    public const string SystemTime = "SeSystemtimePrivilege";

    /// <summary>
    /// The SE_TAKE_OWNERSHIP_NAME privilege.
    /// </summary>
    public const string TakeOwnership = "SeTakeOwnershipPrivilege";

    /// <summary>
    /// The SE_TCB_NAME privilege.
    /// </summary>
    public const string TrustedComputingBase = "SeTcbPrivilege";

    /// <summary>
    /// The SE_TIME_ZONE_NAME privilege.
    /// </summary>
    public const string TimeZone = "SeTimeZonePrivilege";

    /// <summary>
    /// The SE_TRUSTED_CREDMAN_ACCESS_NAME privilege.
    /// </summary>
    public const string TrustedCredentialManagerAccess = "SeTrustedCredManAccessPrivilege";

    /// <summary>
    /// The SE_UNDOCK_NAME privilege.
    /// </summary>
    public const string Undock = "SeUndockPrivilege";

    /// <summary>
    /// The SE_UNSOLICITED_INPUT_NAME privilege.
    /// </summary>
    public const string UnsolicitedInput = "SeUnsolicitedInputPrivilege";
}