首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以编程方式创建和设置对新主文件夹的权限

以编程方式创建和设置对新主文件夹的权限
EN

Stack Overflow用户
提问于 2010-04-02 23:29:06
回答 2查看 10.1K关注 0票数 2

我已经创建了一个应用程序来标准化我们的AD域的用户创建。现在,我希望能够创建、共享和设置该文件夹的权限。我知道如何创建远程文件夹,但我不清楚在VB08中共享和设置权限的最佳方式。

提前谢谢你,

克里斯托弗

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-04-02 23:31:56

这是一个很好的教程http://weblogs.asp.net/cumpsd/archive/2004/02/08/69403.aspx和主路径,您可以从%HOMEPATH% env获得。变量

票数 1
EN

Stack Overflow用户

发布于 2010-05-28 03:26:59

为了让人们知道我最终做了什么,下面是创建远程文件夹的最终成功代码,将文件夹上的NTFS权限设置为所选用户的完全控制权限,然后在新文件夹上创建一个对每个人都具有完全权限的共享。

代码语言:javascript
运行
复制
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);
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2567482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档