2010-04-05 80 views
6
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的返回值,这意味着无效的名称。我传递一个字符串,并试图使用明确的字符串,我仍然得到错误9.以编程方式创建共享失败,错误9

我创建远程共享,而不是在本地计算机上。我试图确保连接到远程WMI提供程序,但我不确定自己是否成功。

WMI大师和其他人的任何建议是非常感谢。

回答

5

在另一个网站上找到答案。文件夹路径需要是创建共享的计算机的本地路径,而不是像我使用的UNC路径。

5

我有同样的错误。在我的情况下,虽然问题是一个尾随反斜杠。做directoryPath.TrimEnd('\')解决了这个问题。

+0

感谢共享 – MichaelS 2014-02-26 15:10:44

+0

除非路径为驱动器的根,例如 -

25未知设备或目录C:\。然后反斜杠是必需的。 – 2015-08-24 18:21:03

5

返回值

返回下表中的一个值或任何其他指示错误的值。 0 - 成功

2 - 访问被拒绝

8 - 未知故障

9 - 无效名称

10 - 无效水平

21 - 无效参数

22 - 重复分享

23 - 重定向路径

24 - 净名称找不到

+0

此列表的来源在哪里? – reasra 2016-11-22 20:29:45

+0

现在不记得它很久以前,但这里有一个链接https://msdn.microsoft.com/en-us/library/aa393598(v=vs.85).aspx – Moji 2016-11-23 16:06:56