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.以編程方式創建共享失敗,錯誤9
我創建遠程共享,而不是在本地計算機上。我試圖確保連接到遠程WMI提供程序,但我不確定自己是否成功。
WMI大師和其他人的任何建議是非常感謝。
感謝共享 – MichaelS 2014-02-26 15:10:44
除非路徑爲驅動器的根,例如 -
25未知設備或目錄C:\。然後反斜槓是必需的。 – 2015-08-24 18:21:03