sharing a folders permission in C#

↘锁芯ラ 提交于 2019-12-12 04:38:59

问题


I am trying to create a tools help the users to control the shared folders, anyway i have a problems with the permissions in the share folder i made everything good but when testing the permissions is not executing

This is command to create a share folder exist:

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

But the problem for this sharing, giving the Everyone permission read the folder, and i don't want this, i don't know how can i solve this

anyway, i use this two methods to give and remove the account permission

Give permission method:

        // 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 permission method:

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

    }

so, when i tested this methods, nothing changes it's just create a share folder

What is the problem ?

来源:https://stackoverflow.com/questions/23780010/sharing-a-folders-permission-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!