7
votes

Créer un partage échoue par programme avec erreur 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 however.  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.

0 commentaires

3 Réponses :


7
votes

a trouvé la réponse sur un autre site. Le chemin du dossier doit être le chemin local de la machine que l'action est créée, pas un chemin UNC comme j'utilisais.


0 commentaires

7
votes

J'ai eu la même erreur. Dans mon cas, bien que le problème soit une barre oblique inverse. Do DistoirePath.trimend ('\') a résolu le problème.


1 commentaires

Sauf si le chemin est pour la racine d'un lecteur, par exemple C: \. Ensuite, la barre oblique inverse est requise.



7
votes

Valeurs de retour

retourne l'une des valeurs dans le tableau suivant ou toute autre valeur pour indiquer une erreur. 0 - Succès

2 - accès refusé

8 - échec inconnu

9 - Nom non valide

10 - Niveau non valide

21 - Paramètre non valide

22 - Dupliquer Partager

23 - Chemin redirigé

24 - Dispositif inconnu ou répertoire

25 - Nom net non trouvé


2 commentaires

Où est la source de cette liste?


Je ne me souviens pas maintenant que c'était il y a longtemps mais voici un lien msdn.microsoft.com/en-us/library/aa393598 (v = vs.85) .aspx