Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式创建共享失败,错误为9_C#_Wmi_Share - Fatal编程技术网

C# 以编程方式创建共享失败,错误为9

C# 以编程方式创建共享失败,错误为9,c#,wmi,share,C#,Wmi,Share,我使用下面的代码来设置共享,但是我总是得到一个返回值9,这意味着无效的名称。我正在传递一个字符串,并尝试使用显式字符串,但仍然得到错误9 然而,我正在远程创建共享,而不是在本地计算机上。我已尝试确保连接到远程WMI提供程序,但不确定是否成功 非常感谢WMI专家和其他人的任何建议。在另一个网站上找到了答案。文件夹路径必须是创建共享的计算机的本地路径,而不是我使用的UNC路径。我也有同样的错误。在我的例子中,问题是后面的反斜杠。执行directoryPath.TrimEnd('\')解决了问题。返回

我使用下面的代码来设置共享,但是我总是得到一个返回值9,这意味着无效的名称。我正在传递一个字符串,并尝试使用显式字符串,但仍然得到错误9

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


非常感谢WMI专家和其他人的任何建议。

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

我也有同样的错误。在我的例子中,问题是后面的反斜杠。执行directoryPath.TrimEnd('\')解决了问题。

返回值

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

2–访问被拒绝

8–未知故障

9–无效名称

10–无效级别

21–无效参数

22–重复共享

23–重定向路径

24–未知设备或目录


25–未找到网络名称

除非路径是驱动器的根目录,例如C:\。然后需要反斜杠。这个列表的来源在哪里?不记得很久以前了,但这里有一个链接
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());
}
}