C#
c# - 윈도우 시스템 날짜 변경 (관리자 권한)
아스C#
2021. 1. 20. 21:19
반응형
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ChangeDate
{
public partial class Form1 : Form
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
iniFIlePath = System.Windows.Forms.Application.StartupPath;
// ini 읽기
StringBuilder exeFilePath = new StringBuilder();
StringBuilder datestr = new StringBuilder();
StringBuilder autorun = new StringBuilder();
GetPrivateProfileString("SETINGS", "FILEPATH", "", exeFilePath, 1024, iniFIlePath + "\\Setting.ini");
textBox1.Text = exeFilePath.ToString();
GetPrivateProfileString("SETINGS", "DATE", "", datestr, 1024, iniFIlePath + "\\Setting.ini");
if (datestr.ToString() != "")
{
DateTime dtDate;
dtDate = Convert.ToDateTime(datestr.ToString());
dateTimePicker1.Value = dtDate;
}
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
[DllImport("kernel32.dll")]
public static extern bool SetLocalTime(ref SYSTEMTIME time);
public static bool SetSystemDateTime(DateTime dtNew)
{
bool bRtv = false;
if (dtNew != DateTime.MinValue)
{
SYSTEMTIME st;
st.wYear = (ushort)dtNew.Year;
st.wMonth = (ushort)dtNew.Month;
st.wDayOfWeek = (ushort)dtNew.DayOfWeek; // Set명령일 경우 이 값은 무시된다.
st.wDay = (ushort)dtNew.Day;
st.wHour = (ushort)dtNew.Hour;
st.wMinute = (ushort)dtNew.Minute;
st.wSecond = (ushort)dtNew.Second;
st.wMilliseconds = (ushort)dtNew.Millisecond;
bRtv = SetLocalTime(ref st); ; // UTC+0 시간을 설정한다.
// bRtv = YtnWin32.SetSystemTime(ref st); // UTC + 표준시간대(대한민궁의 경우 UTC+9)를 설정한다.
}
return bRtv;
}
private void label6_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
dt = DateTime.Parse(dateTimePicker1.Value.ToString("yyyy MM dd") + DateTime.Now.ToString(" HH:mm:ss"));
if (SetSystemDateTime(dt))
{
MessageBox.Show("변경 완료");
}
else
{
MessageBox.Show("시스템 시간 변경 권한이 없습니다. 관리자 권한으로 시작하십시오.");
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
string datestr = dateTimePicker1.Value.ToString();
WritePrivateProfileString("SETINGS", "DATE", datestr, iniFIlePath + "\\Setting.ini");
}
}
윈도우 시스템의 날짜를 변경하고
변경된 날짜를 INI 파일에 저장했다가
다시 가져오는 코드
반응형