vb to c# code conversion RRS feed

  • Question

  • does anyone have any idea on how to convert this to c#?


        Sub shutdown()

            Dim t As Single

            Dim objWMIService, objComputer As Object

            'Now get some privileges

            objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")

            For Each objComputer In objWMIService.InstancesOf("Win32_OperatingSystem")


                t = objComputer.Win32Shutdown(8 + 4, 0)


                If t <> 0 Then

                    'Error occurred!!!


                    'Shutdown your system

                End If


        End Sub

    Thursday, November 9, 2006 9:07 PM


All replies

  • i converted the code and these are the errors that i recieved when i tried to compile it

            public void shutdown()


                float t;

                Object objWMIService;

                Object objComputer;

                objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}");

                foreach (int objComputer in objWMIService.InstancesOf("Win32_OperatingSystem"))


                    t = objComputer.Win32Shutdown(8 + 4, 0);

                    if (t != 0)








    error CS0103: The name 'GetObject' does not exist in the current context

    error CS0117: 'object' does not contain a definition for 'InstancesOf'

    error CS0136: A local variable named 'objComputer' cannot be declared in this scope because it would give a different meaning to 'objComputer', which is already used in a 'parent or current' scope to denote something else

    error CS0117: 'object' does not contain a definition for 'Win32Shutdown'

    Thursday, November 9, 2006 10:46 PM
  • hi plz use a biger font for you code

    i assume GetObject is a method in your project, and i don't know what you are trying to do, but i guess you used WMI so you need to add reference for System.Management to your project and to add "using System.Management" to your class,  i don't know if you gonna need to use Microsoft.win32 namespace or not

    hope this helps


    Thursday, November 9, 2006 11:28 PM
  • this is a VB function, what i am trying to do is restart or shutdown a computer.  If you know of a better way to do this??

    Public Function GetObject(Optional ByVal PathName As String = Nothing, Optional ByVal Class As String = Nothing) As Object

    Member of: Microsoft.VisualBasic.Interaction


    Returns a reference to an object provided by a COM component.


    PathName: Optional. String. The full path and name of the file containing the object to retrieve. If PathName is omitted, Class is required.

    Class: Required if PathName is not supplied. String. A string representing the class of the object. The Class argument has the following syntax and parts:appname.objecttypeParameterDescriptionappnameRequired. String. The name of the application providing the object.objecttypeRequired. String. The type or class of object to create.

    Return Values:

    Returns a reference to an object provided by a COM component.

    Friday, November 10, 2006 4:39 PM
  • best way of doing so would be to use the shutdown utility in Windows 2000 and higher I believe.

    shutdown /m \\computerName /s

    would shutdown the computer. Placing a /r instead of /s will restart the computer

    Friday, November 10, 2006 9:33 PM
  • how is this done programmatically?  some example code using c# would be great
    Friday, November 10, 2006 10:15 PM
  • no worries. We need to spin up a Process class and execute it. This allows us to pretty much "shell" or execute programs outside our application. So....

    import the System.Diagnostics namespace. Then....

    ProcessStartInfo theProc = new ProcessStartInfo("shutdown");

    theProc.Arguments = "/m \\computerName /s";



    does this help?

    Friday, November 10, 2006 10:21 PM
  • i could use the same thing for logout and restart right?  instead of using shutdown i would just say logout of restart right?  or what ever command line arguement i would need to specify for logging user out or restarting.  and yes this helps!  not exactly what i wanted but yes it helps!
    Sunday, November 12, 2006 8:17 PM
  • sure you can. you can abort a shutdown/restart or logout action that is happening. the command to abort is:

    shutdown /m \\computerName /a

    and thats it! click start > run > cmd >

    type: shutdown /?

    and this will give you all the options you can use for the shutdown utility

    Sunday, November 12, 2006 8:23 PM