开发者

Creating share programmatically fails with error 9

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());
}
}

I am using the following code to set up a share, but I am always getting a return value of 9 which means invalid name. I am passing a string and have tried to use an explicit string and I still get error 9.

I am creating the share remotely rather than on local machine howe开发者_如何学编程ver. I have tried to make sure I am connecting to the remote WMI provider, but I am not sure if I have been successful.

Any suggestions from WMI gurus and others is greatly appreciated.


Found the answer on another site. The folder path needs to be the local path to the machine the share is created on, not a UNC path like I was using.


I had the same error. In my case though the problem was a trailing backslash. Doing directoryPath.TrimEnd('\') solved the problem.


Return Values

Returns one of the values in the following table or any other value to indicate an error. 0 – Success

2 – Access denied

8 – Unknown failure

9 – Invalid name

10 – Invalid level

21 – Invalid parameter

22 – Duplicate share

23 – Redirected path

24 – Unknown device or directory

25 – Net name not found

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜