2010-04-02 38 views
2

我已經創建了一個應用程序來爲我們的AD域標準化用戶創建。現在我想能夠在文件夾上創建,共享和設置權限。我知道如何創建一個遠程文件夾,但我不清楚在VB08中共享和設置權限的最佳方法。以編程方式在新主文件夾上創建和設置權限

由於提前,

克里斯托弗

回答

2

只是讓人們知道我最終做了什麼,這裏是創建遠程文件夾的最終成功代碼,將文件夾上的NTFS權限設置爲完全控制所選用戶,然後在新文件夾上創建共享爲每個人提供完整的權限。

using System.IO; 
using System.Management; 
using System.Security.AccessControl; 

public static void CreateFolder(String accountName, String homeFolder) 
    { 
     String folderName; 
     String localfolderpath; 
     String shareName; 

     try 
     { 
      folderName = "\\\\server\\c$\\Home\\" + homeFolder + "\\" + accountName; 
      Directory.CreateDirectory(folderName); 

      localfolderpath = "C:\\Home\\" + homeFolder + "\\" + accountName; 
      shareName = accountName + "$"; 

      FolderACL(accountName, folderName); 

      makeShare(localfolderpath, shareName); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error: " + ex.ToString()); 
     } 
    } 

    public static void FolderACL(String accountName, String folderPath) 
    { 

     FileSystemRights Rights; 

     //What rights are we setting? 

     Rights = FileSystemRights.FullControl; 
     bool modified; 
     InheritanceFlags none = new InheritanceFlags(); 
     none = InheritanceFlags.None; 

     //set on dir itself 
     FileSystemAccessRule accessRule = new FileSystemAccessRule(accountName, Rights, none, PropagationFlags.NoPropagateInherit, AccessControlType.Allow); 
     DirectoryInfo dInfo = new DirectoryInfo(folderPath); 
     DirectorySecurity dSecurity = dInfo.GetAccessControl(); 
     dSecurity.ModifyAccessRule(AccessControlModification.Set, accessRule, out modified); 

     //Always allow objects to inherit on a directory 
     InheritanceFlags iFlags = new InheritanceFlags(); 
     iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; 

     //Add Access rule for the inheritance 
     FileSystemAccessRule accessRule2 = new FileSystemAccessRule(accountName, Rights, iFlags, PropagationFlags.InheritOnly, AccessControlType.Allow); 
     dSecurity.ModifyAccessRule(AccessControlModification.Add, accessRule2, out modified); 

     dInfo.SetAccessControl(dSecurity); 
    } 



    private static void makeShare(string filepath, string sharename) 
    { 
     try 
     { 
      String servername = "server"; 
      // assemble the string so the scope represents the remote server 
      string scope = string.Format("\\\\{0}\\root\\cimv2", servername); 
      // connect to WMI on the remote server 
      ManagementScope ms = new ManagementScope(scope); 
      // create a new instance of the Win32_Share WMI object 
      ManagementClass cls = new ManagementClass("Win32_Share"); 
      // set the scope of the new instance to that created above 
      cls.Scope = ms; 
      // assemble the arguments to be passed to the Create method 
      object[] methodargs = { filepath, sharename, "0" }; 
      // invoke the Create method to create the share 
      object result = cls.InvokeMethod("Create", methodargs); 

      MessageBox.Show(result.ToString()); 
     } 
     catch (SystemException e) 
     { 
      Console.WriteLine("Error attempting to create share {0}:", sharename); 
      Console.WriteLine(e.Message); 
     } 
    } 
相關問題