在C#中共享文件夹权限

时间:2014-05-21 10:04:47

标签: c# share directory

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

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

 // 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);

    }

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

有什么问题?

0 个答案:

没有答案