title: Setting Date and Time on Windows Embedded Standard 7 OS
body: <p>I am working on setting Date and Time on Windows Embedded Standard 7 OS using C# .net. </p>
  
  <p>I tried changing the system date using code in following link.</p>
  
  <p><a href="http://www.pinvoke.net/default.aspx/kernel32.setsystemtime" rel="nofollow">http://www.pinvoke.net/default.aspx/kernel32.setsystemtime</a></p>
  
  <p>But I also saw that one should get the privileges to change the same. But I am receiving an error. here is the code to </p>
  
  <pre><code>Privileges.EnablePrivilege(SecurityEntity.SE_SYSTEMTIME_NAME);
  
          if (SetSystemTime(ref st) == 0)
          {
              return 0;
          }
          return 1;
  </code></pre>
  
  <p>For getting the privaleges I used the code from the following link. </p>
  
  <p><a href="http://www.pinvoke.net/default.aspx/advapi32/AdjustTokenPrivileges.html" rel="nofollow">http://www.pinvoke.net/default.aspx/advapi32/AdjustTokenPrivileges.html</a>
  C# Sample Code 2 (With full error handling):</p>
  
  <p>I have question:
  Is it possible to change the Date and Time using PInvoke. If it is possible what are the change/Setting I should do at the OS.</p>
  
  <p>And what are the other ways to change the date and time. </p>
  
  <p>thank you</p>
  
  <p>Edit:</p>
  
  <pre><code>[DllImport("kernel32.dll")]
      extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
  
  
      public int SetSystemDateTime(DateTime NewSystemDateTime)
      {
          SYSTEMTIME st = new SYSTEMTIME();
          st.wYear = (ushort)NewSystemDateTime.Year;
          st.wMonth = (ushort)NewSystemDateTime.Month;
          st.wDay = (ushort)NewSystemDateTime.Day;
          st.wHour = (ushort)NewSystemDateTime.Hour;
          st.wMinute = (ushort)NewSystemDateTime.Minute;
          st.wMilliseconds = (ushort)NewSystemDateTime.Millisecond;
  
          Privileges.EnablePrivilege(SecurityEntity.SE_SYSTEMTIME_NAME);
  
          if (SetSystemTime(ref st) == 0)
          {
              return 0;
          }
          return 1;
  
      }
  </code></pre>