代码之家  ›  专栏  ›  技术社区  ›  Christopher

以编程方式创建共享失败,出现错误9

  •  6
  • Christopher  · 技术社区  · 15 年前
    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。

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

    我们非常感谢来自wmi专家和其他人的任何建议。

    3 回复  |  直到 11 年前
        1
  •  5
  •   Christopher    15 年前

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

        2
  •  5
  •   ctusch    12 年前

    我也犯了同样的错误。在我的例子中,虽然问题是一个尾随的反斜杠。执行directorypath.trimend('\')解决了问题。

        3
  •  5
  •   Manoj    11 年前

    返回值

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

    2拒绝访问

    8–未知故障

    9–无效名称

    10无效级别

    21–无效参数

    22份重复股份

    23重定向路径

    24–未知设备或目录

    25–未找到网络名称