rss

C Sharp

Management task: Remote shutdown utility

by on Oct.17, 2011, under C Sharp, Desktop, Windows

I run a simple web server to serve as a storage medium for my mobile devices (I don’t like using dropbox or other online storage solutions because they don’t offer much space and they hogs up bandwidth unnecessarily). Before I go to sleep I usually shut down the server to save energy, but in order to do so, I have to have another computer turned on to perform remote access into the server and shut it down via command line. Occasionally, I would turn off my work computer before I remember that I want to shut down the server and I’m too lazy by then to turn on my work computer again.

That’s why I wanted an application that is capable of shutting down Windows on my behalf. The scenario is like this: because I can access FTP service from my phone, I will create a file, namely ‘shutdown’; when the server sees this file, it will delete the file and initiate shutdown. Quite simple.

To shut down, I use Windows management interface (System.Management namespace in C#)

        static void Shutdown()
        {
            ManagementBaseObject mboShutdown = null;
            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
            mcWin32.Get();

            // You can't shutdown without security privileges
            mcWin32.Scope.Options.EnablePrivileges = true;
            ManagementBaseObject mboShutdownParams =
                     mcWin32.GetMethodParameters("Win32Shutdown");

            // Flag 1 means we want to shut down the system. Use "2" to reboot.
            mboShutdownParams["Flags"] = "1";
            mboShutdownParams["Reserved"] = "0";
            foreach (ManagementObject manObj in mcWin32.GetInstances())
            {
                mboShutdown = manObj.InvokeMethod("Win32Shutdown",
                                               mboShutdownParams, null);
            }
        }

From here

And the rest is just a loop to check if the file exists

        ///

        /// The main entry point for the application.
        /// 

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            while (true)
            {
                if (!System.IO.File.Exists("D:\\Shutdown"))
                {
                    Thread.Sleep(500);
                }
                else
                {
                    System.IO.File.Delete("D:\\Shutdown");
                    Shutdown();
                    return;
                }
            }
        }

That’s it, I run the application and it will wait until I create a file named ‘shutdown’ in D:\, it will perform shutdown and quit

Here’s the sample code and binary

Leave a Comment :, , , , , , , , , more...

How to cope with designer errors in Visual Studio

by on Sep.17, 2011, under C Sharp, Development

From time to time, when you have some errors in your controls, Visual may just stop responding altogether and forces you to restart without any indicator on what happened and how to fix it. To overcome this, there is two things developers should keep in mind:

  1. Check database and connection routines, make sure they are initialized properly or blocked from running by checking the special property DesignMode (available as Form.DesignMode and UserControl.DesignMode).
  2. To see the cause of the error, you have to debug Visual Studio itself. When designer error happened to you, choose debug this program and start a new instance of Visual Studio. The second instance should give you the unhandled exception and the code location of the error.
I think this is an artifact from the first days of Visual Studio carried over when they expect all code to behave (after all, developers have to spend years just for planning and resources is not available as much as today). For this Microsoft are way behind Eclipse: In android development tools, if you have errors in your control, the designer at least give you a message about the error and refuses to continue rendering the screen but it won’t crash. Visual Studio, on the other hand, requires a full restart which consumes a lot of time (this particular time it took me 2 hours to find the source of the error).
Leave a Comment :, , , , , more...

preg_match() in action

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post to let me know! :)