ObjectGetOptions options = new ObjectGetOptions();
ManagementPath p = new ManagementPath("\\\\server01\\root" + "\\cimv2:Win32_Share");
// Make a connection to a remote computer.
ManagementScope scope = new ManagementScope("\\\\server01\\root\\cimv2");
scope.Connect();
// Create a ManagementClass object
ManagementClass managementClass = new ManagementClass(scope, p, options);
// Create ManagementBaseObjects for in and out parameters
ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
ManagementBaseObject outParams;
// Set the input parameters
//inParams["Description"] = String.Empty;
inParams["Name"] = "test";
inParams["Path"] = @folderPath;
inParams["Type"] = 0x0; // Disk Drive
// Invoke the method on the ManagementClass object
outParams = managementClass.InvokeMethod("Create", inParams, null);
// Check to see if the method invocation was successful
if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
{
throw new Exception("Unable to share directory. Error code: " + outParams.Properties["ReturnValue"].Value);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
我使用以下代码来设置共享,但我总是得到返回值9,这意味着名称无效.我传递一个字符串,并尝试使用显式字符串,我仍然得到错误9.
我是远程创建共享而不是在本地计算机上创建共享.我试图确保我连接到远程WMI提供程序,但我不确定我是否成功.
WMI大师和其他人的任何建议都非常感谢.
返回值
返回下表中的一个值或任何其他值以指示错误.0 - 成功
2 - 拒绝访问
8 - 未知故障
9 - 名称无效
10 - 无效级别
21 - 参数无效
22 - 重复份额
23 - 重定向路径
24 - 未知设备或目录
25 - 找不到网络名称