7
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の達人や他の人からの提案は大歓迎です。

4

3 に答える 3

7

別のサイトで答えを見つけました。フォルダパスは、私が使用していたようなUNCパスではなく、共有が作成されたマシンへのローカルパスである必要があります。

于 2010-05-25T19:49:07.313 に答える
7

同じエラーが発生しました。私の場合、問題は末尾のバックスラッシュでしたが。directoryPath.TrimEnd('\')を実行すると、問題が解決しました。

于 2013-06-13T14:07:25.960 に答える
7

戻り値

次の表の値の1つ、またはエラーを示すその他の値を返します。0 –成功

2 –アクセスが拒否されました

8 –不明な障害

9 –無効な名前

10 –無効なレベル

21 –無効なパラメーター

22 –重複共有

23 –リダイレクトされたパス

24 –不明なデバイスまたはディレクトリ

25 –ネット名が見つかりません

于 2014-01-08T11:41:53.343 に答える