C# 创建文件夹并且设置共享添加everyone用户完全控制权限

本文介绍了如何使用C#编程在Windows中创建并共享文件夹,设置Everyone用户组具有完全控制权限,以及通过netshare命令行工具进行高级自定义权限设置。
摘要由CSDN通过智能技术生成

源码如下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class FolderSharing
    {
        public static void CreateAndShareFolder(string folderPath, string shareName, string shareDescription)
        {
            // 创建文件夹
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            // 设置共享
            ManagementClass share = new ManagementClass("Win32_Share");
            ManagementBaseObject inParams = share.GetMethodParameters("Create");

            inParams["Path"] = folderPath;
            inParams["Name"] = shareName;
            inParams["Type"] = 0; // 设置共享类型,0 表示 Disk Drive, 1 表示 Print Queue, 2 表示 Device 等
            inParams["Description"] = shareDescription;
            inParams["MaximumAllowed"] = null; // 设置最大访问用户数,NULL 表示无限制
                                               //    inParams["Access"] =null;//默认的共享权限是Everyone
            ManagementBaseObject outParams = share.InvokeMethod("Create", inParams, null);

            // 检查是否成功创建共享
            if ((uint)(outParams["ReturnValue"]) == 0)
            {
                Console.WriteLine("文件夹共享成功!");
            }
            else
            {
                Console.WriteLine("文件夹共享失败,错误代码:" + outParams["ReturnValue"]);
            }
        }
    }

将everyone添加到管理组设置权限为everyone
    public class Program
    {
        public static void AddSecurityControll2Folder(string dirPath)
        {
            //获取文件夹信息
            DirectoryInfo dir = new DirectoryInfo(dirPath);
            //获得该文件夹的所有访问权限
            System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All);
            //设定文件ACL继承
            InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
            //添加ereryone用户组的访问权限规则 完全控制权限
            FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
            //添加Users用户组的访问权限规则 完全控制权限
            FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
            bool isModified = false;
            dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified);
            dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, out isModified);
            //设置访问权限
            dir.SetAccessControl(dirSecurity);
        }

static void Main(string[] args)
        {
         

            #region 创建文件夹
            string folderPath = @"E:\MySharedFolder";
            string shareName = "MySharedFolder";
            string shareDescription = "This is a shared folder";
            FolderSharing.CreateAndShareFolder(folderPath, shareName, shareDescription);
            string networkFolderPath = @"E:\MySharedFolder";
            AddSecurityControll2Folder(networkFolderPath);
            #endregion

            #region 执行cmd命令  (高级自定义权限设置everyone 完全控制权限)
            //MyShared  是共享名称,共享名称不能重复00
            // 要执行的cmd命令       MyShared 是共享名称不能重复
            string command = "net share MyShared=E:\\MySharedFolder /grant:Everyone,FULL";

            // 使用Process启动cmd.exe
            using (Process process = new Process())
            {
                
                process.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = false;

                process.Start();

                // 将命令写入cmd.exe
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(process.StandardInput.BaseStream))
                {
                    sw.WriteLine(command);
                    sw.WriteLine("exit");
                }

                // 获取cmd的输出
                string result = process.StandardOutput.ReadToEnd();

                process.WaitForExit();

                // 打印输出结果
                Console.WriteLine(result);

                #endregion

            }
        }
    }
}
 

  • 23
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值