文件夹的完全控制权限赋给aspnet用户(ntfs,cacls)

本文介绍了一种使用C#编程语言来实现为特定用户授予指定文件夹读写权限的方法。通过调用系统命令和利用WMI(Windows Management Instrumentation)进行磁盘驱动器筛选,确保仅当目标驱动器为NTFS格式时才执行权限设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.DirectoryServices;
using System.Collections;
using System.Diagnostics;
using System.Management;

namespace GrantUserWritableFile
{
    
public class GrantUser
    
{
        
private string directoryInstr = "";
        
private string directoryPrtfl = "";
        
private string hostName = "";
        
public string DomainName = "localhost";
        
private string userName = "";
        
private string syspath = "";
        
private string DriverName = "";

        
public static void Main(string[] args)
        
{
            GrantUser gu 
= new GrantUser();
            gu.getInfo();
            Console.WriteLine(
"Grant folder to user complited!");
        }

        
public void getInfo()
        
{
            DirectoryEntry de 
= new DirectoryEntry("IIS://Localhost/W3SVC/1/ROOT/RPA");

            
this.directoryInstr = (string)de.Properties["Path"].Value + "/InstrumentInfo/ChartImages";
            
this.directoryPrtfl = (string)de.Properties["Path"].Value + "/PortfolioView/ChartImages";
            
//this.directoryInstr = "C:/1";
            DriverName = de.Properties["Path"].Value.ToString().Substring(02);
            
this.hostName = Dns.GetHostName().ToString();
            
this.userName = "IUSR_" + hostName;
            
this.syspath = Environment.SystemDirectory + "/";

            ManagementObjectSearcher DiskSearch 
= new ManagementObjectSearcher(new SelectQuery("Select * from Win32_LogicalDisk"));
            ManagementObjectCollection moDiskCollection 
= DiskSearch.Get();
            
foreach (ManagementObject mo in moDiskCollection)
            
{
                
if ((mo.SystemProperties["FileSystem"].Value.ToString().ToUpper() == "NTFS"&& (DriverName == mo.SystemProperties["name"].Value.ToString()))
                
{
                    
this.SetFolder(directoryInstr, userName);
                    
this.SetFolder(directoryPrtfl, userName);
                }

            }

        }


        
public void SetFolder(string PathName,string userName)
        
{
            
//if (Directory.Exists(PathName) == true)
            
//{
            
//    Directory.Delete(PathName, true);
            
//}
            
//Directory.CreateDirectory(PathName);
            Process process = new Process();
            process.StartInfo.FileName 
= syspath + "cmd.exe";
            process.StartInfo.UseShellExecute 
= false;
            process.StartInfo.RedirectStandardInput 
= true;
            process.StartInfo.RedirectStandardOutput 
= true;
            process.StartInfo.RedirectStandardError 
= true;
            process.StartInfo.CreateNoWindow 
= false;
            process.Start();
            process.StandardInput.WriteLine(String.Format(
"cacls   {0}   /t   /e  /g   {1}:f", PathName, userName));
            process.StandardInput.WriteLine(
"exit");
            process.WaitForExit();
            process.Close();
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值