【问题标题】:Creating share programmatically fails with error 9以编程方式创建共享失败并出现错误 9
【发布时间】:2010-04-05 15:41:04
【问题描述】:
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 大师和其他人的任何建议。

【问题讨论】:

    标签: c# wmi share


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 除非路径是驱动器的根目录,例如C:\。然后反斜杠是必需的。
      【解决方案3】:

      返回值

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

      2 – 拒绝访问

      8 – 未知故障

      9 – 名称无效

      10 – 无效级别

      21 – 无效参数

      22 – 重复分享

      23 – 重定向路径

      24 – 未知设备或目录

      25 – 未找到网络名称

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      相关资源
      最近更新 更多