如何通过c#代码为新共享文件夹上的所有用户设置权限? 这是我添加共享文件夹的代码:
public static string sharedFolder()
{
try
{
// create a directory
Directory.CreateDirectory(@"C:\MyTestShare");
// Create a ManagementClass object
ManagementClass managementClass = new ManagementClass("Win32_Share");
// Create ManagementBaseObjects for in and out parameters
ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
ManagementBaseObject outParams;
// Set the input parameters
inParams["Description"] = "My Files Share";
inParams["Name"] = "My Files Share";
inParams["Path"] = @"C:\MyTestShare";
inParams["Type"] = 0x0; // Disk Drive
// Invoke the method on the ManagementClass object
//InvokeMethodOptions _invokeMethodOptions=new InvokeMethodOptions.InfiniteTimeout.h
outParams = managementClass.InvokeMethod("Create", inParams,null);
setPermissionsToShareFolder(inParams["Path"].ToString());
// AddDirectorySecurity(inParams["Path"].ToString());
// Check to see if the method invocation was successful
if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
{
return ("Unable to share directory.");
}
return ("Able to share directory.");
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
答案 0 :(得分:10)
我也一直在谷歌搜索,主要是找到像这样的未解答的问题。
以下是我编写的代码,其中包含在不同位置找到的所有代码:
public static void CreateSharedFolder(string FolderPath, string ShareName, string Description)
{
try
{
// Create a ManagementClass object
ManagementClass managementClass = new ManagementClass("Win32_Share");
// Create ManagementBaseObjects for in and out parameters
ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
ManagementBaseObject outParams;
// Set the input parameters
inParams["Description"] = Description;
inParams["Name"] = ShareName;
inParams["Path"] = FolderPath;
inParams["Type"] = 0x0; // Disk Drive
//Another Type:
// DISK_DRIVE = 0x0
// PRINT_QUEUE = 0x1
// DEVICE = 0x2
// IPC = 0x3
// DISK_DRIVE_ADMIN = 0x80000000
// PRINT_QUEUE_ADMIN = 0x80000001
// DEVICE_ADMIN = 0x80000002
// IPC_ADMIN = 0x8000003
//inParams["MaximumAllowed"] = 2;
inParams["Password"] = null;
NTAccount everyoneAccount = new NTAccount(null, "EVERYONE");
SecurityIdentifier sid = (SecurityIdentifier)everyoneAccount.Translate(typeof(SecurityIdentifier));
byte[] sidArray = new byte[sid.BinaryLength];
sid.GetBinaryForm(sidArray, 0);
ManagementObject everyone = new ManagementClass("Win32_Trustee");
everyone["Domain"] = null;
everyone["Name"] = "EVERYONE";
everyone["SID"] = sidArray;
ManagementObject dacl = new ManagementClass("Win32_Ace");
dacl["AccessMask"] = 2032127;
dacl["AceFlags"] = 3;
dacl["AceType"] = 0;
dacl["Trustee"] = everyone;
ManagementObject securityDescriptor = new ManagementClass("Win32_SecurityDescriptor");
securityDescriptor["ControlFlags"] = 4; //SE_DACL_PRESENT
securityDescriptor["DACL"] = new object[] { dacl };
inParams["Access"] = securityDescriptor;
// Invoke the "create" method on the ManagementClass object
outParams = managementClass.InvokeMethod("Create", inParams, null);
// Check to see if the method invocation was successful
var result = (uint)(outParams.Properties["ReturnValue"].Value);
switch (result)
{
case 0:
Console.WriteLine("Folder successfuly shared.");
break;
case 2:
Console.WriteLine("Access Denied");
break;
case 8:
Console.WriteLine("Unknown Failure");
break;
case 9:
Console.WriteLine("Invalid Name");
break;
case 10:
Console.WriteLine("Invalid Level");
break;
case 21:
Console.WriteLine("Invalid Parameter");
break;
case 22:
Console.WriteLine("Duplicate Share");
break;
case 23:
Console.WriteLine("Redirected Path");
break;
case 24:
Console.WriteLine("Unknown Device or Directory");
break;
case 25:
Console.WriteLine("Net Name Not Found");
break;
default:
Console.WriteLine("Folder cannot be shared.");
break;
}
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex.Message);
}
}
internal static void RemoveSharedFolder(string ShareName)
{
try
{
// Create a ManagementClass object
ManagementClass managementClass = new ManagementClass("Win32_Share");
ManagementObjectCollection shares = managementClass.GetInstances();
foreach (ManagementObject share in shares)
{
if (Convert.ToString(share["Name"]).Equals(ShareName))
{
var result = share.InvokeMethod("Delete", new object[] { });
// Check to see if the method invocation was successful
if (Convert.ToInt32(result) != 0)
{
Console.WriteLine("Unable to unshare directory.");
}
else
{
Console.WriteLine("Folder successfuly unshared.");
}
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex.Message);
}
}
}
“FolderPath”必须存在。
答案 1 :(得分:0)
我假设您对everyone
的描述是“所有用户”。
MSDN Create Method of the Win32_Share Class
参数
访问[in]
用户级权限的安全描述符。安全描述符包含 有关权限,所有者和访问权限的信息 资源。如果未提供此参数或为NULL,则Everyone已阅读 访问dhare。有关更多信息,请参阅Win32_SecurityDescriptor和 更改安全对象的访问安全性。
否则,根据MSDN - Chaning Access Security on Securable Objects,您需要将Win32_LogicalShareSecuritySetting
类与MSDN上描述的SetSecurityDescriptor
方法一起使用 - SetSecurityDescriptor Method of the Win32_LogicalShareSecuritySetting Class