質問
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の達人などから任意の提案を大幅に高く評価されます。
解決
は、別のサイト上で答えを見つけました。フォルダパスは、私が使っていたように、共有が作成されているマシンではなく、UNCパスへのローカルパスである必要があります。
他のヒント
私は、同じエラーが発生しました。私の場合は問題は末尾のバックスラッシュでしたが。 directoryPath.TrimEnd( '\')を実行すると、問題を解決します。
戻り値
は、次の表の値またはエラーを示すために他の値のいずれかを返します。 0 - 成功
2 - アクセスが拒否されまし
8 - 不明の障害
9 - 無効な名前
10 - 無効なレベル
21 - 無効なパラメーター
22 - 重複シェア
23 - リダイレクトパス
24 - 不明なデバイスまたはディレクトリ
25 - ネット名が見つからない
所属していません StackOverflow