vb to c# code conversion

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!!!

Else

'Shutdown your system

End If

Next

End Sub



Answer this question

vb to c# code conversion

  • Aaron Smith

    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";

    Process.Start(theProc);

    does this help



  • Demix

    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

     



  • Tollo

  • mNilysg

    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



  • nobitavn94

    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

    Summary:

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

    Parameters:

    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.


  • AboOmar

    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!
  • Bill Baker

    how is this done programmatically some example code using c# would be great
  • mrplatypus

    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



  • Bob Heitzman

    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)

    {

    }

    else

    {

    }

    }

    }

    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'


  • vb to c# code conversion