源码如下:
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
}
}
}
}