C# 在C中共享文件夹权限#

C# 在C中共享文件夹权限#,c#,share,directory,C#,Share,Directory,无论如何,我正在尝试创建一个工具来帮助用户控制共享文件夹 我对共享文件夹中的权限有问题 我做的一切都很好,但是当测试权限时没有执行 这是用于创建共享文件夹的命令: // use CMD to sharing the folder System.Diagnostics.Process command = new System.Diagnostics.Process(); command.StartInfo.CreateNoWindow = true

无论如何,我正在尝试创建一个工具来帮助用户控制共享文件夹 我对共享文件夹中的权限有问题 我做的一切都很好,但是当测试权限时没有执行

这是用于创建共享文件夹的命令:

 // use CMD to sharing the folder
            System.Diagnostics.Process command = new System.Diagnostics.Process();
            command.StartInfo.CreateNoWindow = true;
            command.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            command.StartInfo.FileName = "cmd.exe";
            command.StartInfo.Arguments = "/C net share " + ShareName + "=D:\\FolderName";
            command.Start();
            command.WaitForExit();
            command.Close();
但是这个共享的问题,给了Everyone阅读文件夹的权限,我不想要这个,我不知道如何解决这个问题

无论如何,我使用这两种方法来授予和删除帐户权限

授予权限方法:

        // Add sharing permission method
    public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
    {

        try
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the  
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings. 
            dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

    }
// remove sharing permission method
    public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
    {
        // Create a new DirectoryInfo object.
        DirectoryInfo dInfo = new DirectoryInfo(FileName);

        // Get a DirectorySecurity object that represents the  
        // current security settings.
        DirectorySecurity dSecurity = dInfo.GetAccessControl();

        // Add the FileSystemAccessRule to the security settings. 
        dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));

        // Set the new access settings.
        dInfo.SetAccessControl(dSecurity);

    }
删除权限方法:

        // Add sharing permission method
    public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
    {

        try
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the  
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings. 
            dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

    }
// remove sharing permission method
    public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
    {
        // Create a new DirectoryInfo object.
        DirectoryInfo dInfo = new DirectoryInfo(FileName);

        // Get a DirectorySecurity object that represents the  
        // current security settings.
        DirectorySecurity dSecurity = dInfo.GetAccessControl();

        // Add the FileSystemAccessRule to the security settings. 
        dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));

        // Set the new access settings.
        dInfo.SetAccessControl(dSecurity);

    }
所以,当我测试这个方法时,没有任何变化 它只是创建一个共享文件夹


有什么问题?

从内存中,您正在更改文件夹的权限,而不是使用上述代码更改共享。相反,您需要使用WMI和相关类来完成此任务。请看一个例子。非常感谢Dash,它成功了