获取北京时间,矫正电脑系统时间的有关问题
获取北京时间,矫正电脑系统时间的问题
我把程序放在wen7的电脑上运行,可以进行矫正电脑系统时间,但是我把程序放在wen8.1的电脑上运行,就不能进行矫正电脑系统时间,为什么呢
namespace 淘宝整点秒杀助手
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
private static extern bool SetLocalTime(ref SYSTEMTIME time);
[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}
public Form1()
{
InitializeComponent();
webBrowser1.Url = new System.Uri("http://ju.taobao.com/tg/point_list.htm?spm=1.7274553.754904965.d9.4ONjTD");
// GetBeijingTime();
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Enabled=false;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
GetBeijingTime();
MessageBox.Show("时间矫正完成!", "温馨提示");
}
private void button3_Click(object sender, EventArgs e)
{
comboBox1.Enabled = true;
}
// 获取网络时间
public DateTime GetBeijingTime()
{
DateTime dt;
WebRequest wrt = null;
WebResponse wrp = null;
try
{
wrt = WebRequest.Create("http://www.beijing-time.org/time15.asp");
wrp = wrt.GetResponse();
string html = string.Empty;
using (Stream stream = wrp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
html = sr.ReadToEnd();
}
}
string[] tempArray = html.Split(';');
for (int i = 0; i < tempArray.Length; i++)
{
tempArray[i] = tempArray[i].Replace("\r\n", "");
}
string year = tempArray[1].Split('=')[1];
string month = tempArray[2].Split('=')[1];
string day = tempArray[3].Split('=')[1];
string hour = tempArray[5].Split('=')[1];
string minite = tempArray[6].Split('=')[1];
string second = tempArray[7].Split('=')[1];
dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":"
+ minite + ":" + second);
// label1.Text = dt.ToString();
SYSTEMTIME st;
st.year = (short)dt.Year;
st.month = (short)dt.Month;
st.dayOfWeek = (short)dt.DayOfWeek;
st.day = (short)dt.Day;
st.hour = (short)dt.Hour;
st.minute = (short)dt.Minute;
st.second = (short)dt.Second;
st.milliseconds = (short)dt.Millisecond;
SetLocalTime(ref st);
}
catch (WebException)
{
return DateTime.Parse("2011-1-1");
}
catch (Exception)
{
return DateTime.Parse("2011-1-1");
}
finally
{
if (wrp != null)
wrp.Close();
if (wrt != null)
wrt.Abort();
}
// timer1.Enabled = true;
return dt;
}
//定时比对时间
private void timer1_Tick_1(object sender, EventArgs e)
{
if (comboBox1.Text == DateTime.Now.ToString("hh")) {
foreach (HtmlElement element in webBrowser1.Document.GetElementsByTagName("button"))
{
if (element.GetAttribute("ClassName") == "buyaction J_BuySubmit")
{
element.InvokeMember("Click");
timer1.Enabled =false;
break;
}
}
};
}
//设置在本窗体中加载
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ //将所有的链接的目标,指向本窗体
foreach (HtmlElement archor in this.webBrowser1.Document.Links)
{
archor.SetAttribute("target", "_self");
}
//将所有的FORM的提交目标,指向本窗体
foreach (HtmlElement form in this.webBrowser1.Document.Forms)
{
form.SetAttribute("target", "_self");
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
------解决思路----------------------
win7是不是关掉了UAC,win8没关?
win8里右键你的程序,以管理员方式启动之后能行么?
我把程序放在wen7的电脑上运行,可以进行矫正电脑系统时间,但是我把程序放在wen8.1的电脑上运行,就不能进行矫正电脑系统时间,为什么呢
namespace 淘宝整点秒杀助手
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
private static extern bool SetLocalTime(ref SYSTEMTIME time);
[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}
public Form1()
{
InitializeComponent();
webBrowser1.Url = new System.Uri("http://ju.taobao.com/tg/point_list.htm?spm=1.7274553.754904965.d9.4ONjTD");
// GetBeijingTime();
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Enabled=false;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
GetBeijingTime();
MessageBox.Show("时间矫正完成!", "温馨提示");
}
private void button3_Click(object sender, EventArgs e)
{
comboBox1.Enabled = true;
}
// 获取网络时间
public DateTime GetBeijingTime()
{
DateTime dt;
WebRequest wrt = null;
WebResponse wrp = null;
try
{
wrt = WebRequest.Create("http://www.beijing-time.org/time15.asp");
wrp = wrt.GetResponse();
string html = string.Empty;
using (Stream stream = wrp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
html = sr.ReadToEnd();
}
}
string[] tempArray = html.Split(';');
for (int i = 0; i < tempArray.Length; i++)
{
tempArray[i] = tempArray[i].Replace("\r\n", "");
}
string year = tempArray[1].Split('=')[1];
string month = tempArray[2].Split('=')[1];
string day = tempArray[3].Split('=')[1];
string hour = tempArray[5].Split('=')[1];
string minite = tempArray[6].Split('=')[1];
string second = tempArray[7].Split('=')[1];
dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":"
+ minite + ":" + second);
// label1.Text = dt.ToString();
SYSTEMTIME st;
st.year = (short)dt.Year;
st.month = (short)dt.Month;
st.dayOfWeek = (short)dt.DayOfWeek;
st.day = (short)dt.Day;
st.hour = (short)dt.Hour;
st.minute = (short)dt.Minute;
st.second = (short)dt.Second;
st.milliseconds = (short)dt.Millisecond;
SetLocalTime(ref st);
}
catch (WebException)
{
return DateTime.Parse("2011-1-1");
}
catch (Exception)
{
return DateTime.Parse("2011-1-1");
}
finally
{
if (wrp != null)
wrp.Close();
if (wrt != null)
wrt.Abort();
}
// timer1.Enabled = true;
return dt;
}
//定时比对时间
private void timer1_Tick_1(object sender, EventArgs e)
{
if (comboBox1.Text == DateTime.Now.ToString("hh")) {
foreach (HtmlElement element in webBrowser1.Document.GetElementsByTagName("button"))
{
if (element.GetAttribute("ClassName") == "buyaction J_BuySubmit")
{
element.InvokeMember("Click");
timer1.Enabled =false;
break;
}
}
};
}
//设置在本窗体中加载
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ //将所有的链接的目标,指向本窗体
foreach (HtmlElement archor in this.webBrowser1.Document.Links)
{
archor.SetAttribute("target", "_self");
}
//将所有的FORM的提交目标,指向本窗体
foreach (HtmlElement form in this.webBrowser1.Document.Forms)
{
form.SetAttribute("target", "_self");
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
------解决思路----------------------
win7是不是关掉了UAC,win8没关?
win8里右键你的程序,以管理员方式启动之后能行么?