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());
}
}
Korzystam z następującego kodu, aby ustawić udział, ale zawsze otrzymuję zwrotną wartość 9, która oznacza nieprawidłową nazwę. Przekazuję ciąg znaków i próbuję użyć jawnego ciągu znaków i nadal pojawia się błąd 9.Tworzenie udziału programowo kończy się niepowodzeniem z błędem 9
Jednak zdarza się, że tworzę udział zdalnie, a nie na komputerze lokalnym. Próbowałem upewnić się, że łączę się ze zdalnym dostawcą WMI, ale nie jestem pewien, czy odniosłem sukces.
Wszelkie sugestie od guru WMI i innych są bardzo mile widziane.
Thanks for sharing – MichaelS
Jeżeli ścieżka jest dla korzenia napędu, na przykład DO:\. Następnie wymagany jest ukośnik odwrotny. –