Bonjour,

J'ai un petit probleme avec la classe win32_Printer.
Je peux récupérer différente infos mais impossible de récupérer les informations Attributes ou Availability de la classe.


Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
 
namespace DriverImprimante
{
    public class RechercheDriver
    {
        public RechercheDriver()
        {
 
        }
 
        public Info_Imprimantes[] ListeDriver()
        {
            List<Info_Imprimantes> lst = new List<Info_Imprimantes>();
            ManagementObjectSearcher searcher= new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
            foreach(ManagementObject queryObj in searcher.Get())
            {
                lst.Add(new Info_Imprimantes(queryObj));
            }
 
            return lst.ToArray();
 
        }
    }
 
 
    public class Info_Imprimantes
    {
        String _Nom_Imprimante;
 
        public String Nom_Imprimante
        {
            get { return _Nom_Imprimante; }
            private set { _Nom_Imprimante = value; }
        }
 
        String _Nom_Driver;
 
        public String Nom_Driver
        {
            get { return _Nom_Driver; }
            private set { _Nom_Driver = value; }
        }
 
 
 
          Int32 _Attributes;
 
        public Int32 Attributes
        {
            get { return _Attributes; }
            private set { _Attributes = value; }
        }
 
 
 
 
 
        public Info_Imprimantes(ManagementObject queryObj)
        {
            Nom_Driver = (String)queryObj["DriverName"];
            Nom_Imprimante = (String)queryObj["Name"];
            Attributes = (Int32)queryObj["Attributes"];
        }
 
        public Info_Imprimantes()
        {
 
        }
    }
 
}


A chaque fois j ai ce message d erreur :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
L'invocation de la méthode <ListeDriver()> du type <Info_Imprimantes[]> a échoué
Le framework .NET a renvoyé l'erreur suivante : 
System.Reflection.TargetInvocationException: Une exception a été levée par la cible d'un appel. ---> System.InvalidCastException: Le cast spécifié n'est pas valide.
   à DriverImprimante.Info_Imprimantes..ctor(ManagementObject queryObj)
   à DriverImprimante.RechercheDriver.ListeDriver()
   --- Fin de la trace de la pile d'exception interne ---
   à System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   à System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   à System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   à System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   à CDotNetMethod.bInvoke(SByte* pszTypeName, SByte* pszMethodName, SByte* pszParameters, MethodInfo gcMethod, Object gcObj, CSLevel* pclPile, Int32 nNbParamPile, Int32 bValeurRetour, STOperationDotNet* pstOperation)

Une idée d'où vient mon problème ?


Merci