Shutdown Time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AniNazir
    New Member
    • Dec 2007
    • 9

    Shutdown Time

    Hai To All,


    I want to create an application which should return shutdown time and startup time of my system....


    Does any one have idea about this???




    Regards,
    Anitha
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    What type of application & platform? Have you started yet?

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Well, I don't know how get this from the Framework, but from a command line you can type:
      Code:
      net statistics workstation | find "Statistics since" > time.txt
      and that will create a text file called time.txt in the working directory with the last boot time.

      So you could make a batch file, have it change the directory to the one you want the file to be in, and run this command. You can run this batch file from your code using the System.Diagnost ics namespace, and then use System.IO to read in and parse the text file.

      Perhaps someone else has a more elegant solution.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Couldn't you just also run this code:
        Code:
        net statistics workstation | find "Statistics since"
        With the Process class and watch the output.
        Hmm or does it not handle pipes correctly?
        Then create a batchfile WITHOUT the time.txt part and then read the output from the Process class.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Well, to be perfectly honest, I'm not super familiar with the Process class, so if you say that you can read the output, I'm sure you can.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by insertAlias
            Well, to be perfectly honest, I'm not super familiar with the Process class, so if you say that you can read the output, I'm sure you can.
            Well it's the pipe operator that I don't think will work. For that whole thing to work without a batch file, I think you would need 2 Process objects.
            The first one would be 'net statistics workstation' and the output of that one would be redirected to the output of the 2nd Process which would be 'find "Statistics since" '. Then you would read the output of that 2nd Process to get your string.

            However I think you can probably do this with the EventLogs or WMI as well.

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              I did figure out a way to get the boot time from WMI:
              Code:
              string wmiQuery = "SELECT * from Win32_OperatingSystem";
              ManagementObjectSearcher search = new ManagementObjectSearcher("root\\cimv2", wmiQuery);
              string time = null;
              foreach (ManagementBaseObject o in search.Get())
              {
                  time = (o["LastBootUpTime"].ToString());
              }
              time = time.Substring(0, 14);
              int yyyy = Int32.Parse(time.Substring(0, 4));
              int MM = Int32.Parse(time.Substring(4, 2));
              int dd = Int32.Parse(time.Substring(6, 2));
              int hh = Int32.Parse(time.Substring(8, 2));
              int mm = Int32.Parse(time.Substring(10, 2));
              int ss = Int32.Parse(time.Substring(12, 2));
              DateTime dt = new DateTime(yyyy, MM, dd, hh, mm, ss);
              Console.WriteLine(dt.ToString());
              The date format it returns is pretty convoluted, so I parsed it out to a DateTime.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Would this not also work?
                [code=c#]
                DateTime mydt;
                DateTimeFormatI nfo myDTFI = new CultureInfo("en-US", false).DateTime Format;
                DateTime.TryPar seExact(time, "yyyyMMddhhmmss ", myDTFI, DateTimeStyles. None, out myDTFI);
                [/code]

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

                  #9
                  Why do I always have to do things the hard way!?!?

                  Well, you learn something new every day.

                  I think that we have thoroughly answered the OP's question.

                  Comment

                  • AniNazir
                    New Member
                    • Dec 2007
                    • 9

                    #10
                    Thanks for all who answered my questions. Actually iam trying to do trail version of my application. So i need working time for my system. That is, i want startup time and shut down time of my system. How can i get this???









                    Regards,
                    Anitha

                    Comment

                    • Curtis Rutland
                      Recognized Expert Specialist
                      • Apr 2008
                      • 3264

                      #11
                      I'm not sure that it stores the last time you shut down. Just the most recent startup. If you want to calculate uptime, you can get the difference between now and the last startup date.

                      Sorry, that's all the help I can give. We've shown you how to get the most recent startup time, anyway.

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        You could create a service who's only task was to write a date when it starts running and when it stops running to some file that you could then look up?

                        Comment

                        • joedeene
                          Contributor
                          • Jul 2008
                          • 579

                          #13
                          well, like plater said, but couldnt you just embed it in the main application itself?

                          such as on the main form load, get the time and log it to a file

                          and for each form from the startup form, you could set something in it's Closing event to write down the time? would that not work, and set some kind of global value to true to know the closing time has been written.

                          Comment

                          • Curtis Rutland
                            Recognized Expert Specialist
                            • Apr 2008
                            • 3264

                            #14
                            Well, I've read that you might be able to get the shutdown time from the event log. Open yours up and see if you can find it.

                            Comment

                            Working...