题
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());
}
}
我使用以下代码来建立一个共享,但我总是得到9:1的返回值,这意味着无效名称。我传递一个字符串,并试图用一个明确的字符串,我仍然得到错误9。
我创建共享远程而非然而本地机器上。我试图以确保我连接到远程WMI提供程序,但我不知道我是否已经成功。
这WMI大师和其他任何建议是极大的赞赏。
解决方案
实测值在其他网站上的答案。文件夹路径需要是该共享创建在机器的本地路径,而不是像我的UNC路径使用。
其他提示
我有同样的错误。在我的情况,虽然这个问题是斜杠。做directoryPath.TrimEnd( '\')解决了这个问题。
返回值
返回以下表或任何其他值,以指示一个错误的值中的一个。 0 - 成功
2 - 访问被拒绝
8 - 未知故障
9 - 无效名称
10 - 无效的水平
21 - 无效的参数
22 - 重复份额
23 - 重定向路径
24 - 未知设备或目录
25 - 净名称找不到
不隶属于 StackOverflow