近期项目中需要解决软件的时间限制问题,软件必须要在2014年12月31日才能运行,网上搜到相关软件runasdate
系统界面如下:
但是项目是通过C#打开exe文件代码如下
string exeFileName="XXXXXXXXX.exe";//exe文件路径
Process p = new Process();
p.StartInfo.FileName = exeFileName;
p.start();
那么问题来了,利用runasdate解决的时间限制问题如何把路径参数传进来呢?我们可以打开查看快捷方式的属性发现其位置是这样的:D:\时间限制\runasdate\RunAsDate.exe 31\12\2014 10:00:00 "E:\XXXXXXXXXXXX.exe" 这显然是不合法的。因此利用C#打开runasdata软件处理过的程序宣告失败。
于是灵机一动,为何不直接把系统时间改为符合条件的时间,exe文件启动后再把时间修改回来呢?
本人太菜,网上查了查如何系统时间,于是第一次接触到了进程这种东西,虽然功能实现了,目前还是一脸蒙蔽
主要思路
1.通过进程p1打开cmd,修改系统时间
2.系统时间修改了,再启动exe
3.exe成功打开后再把时间改回来
于是就有了以下代码:
private void cacheToolTool_Click(object sender, EventArgs e)
{
try
{
DateTime time = DateTime.Now;
string Arg = time.Year.ToString() + "-" + time.Month.ToString() + "-" + time.Day.ToString();
//实例一个Process类,启动一个独立进程
Process p1 = new Process();
//Process类有一个StartInfo属性
//设定程序名
p1.StartInfo.FileName = "cmd.exe";
//设定程式执行参数 “/C”表示执行完命令后马上退出
p1.StartInfo.Arguments = "/c date 2014-12-31";
//关闭Shell的使用
p1.StartInfo.UseShellExecute = false;
//重定向标准输入
p1.StartInfo.RedirectStandardInput = true;
p1.StartInfo.RedirectStandardOutput = true;
//重定向错误输出
p1.StartInfo.RedirectStandardError = true;
//设置不显示doc窗口
p1.StartInfo.CreateNoWindow = true;
//启动
p1.Start();
p1.WaitForExit();
p1.Close();
string exeFileName = Application.StartupPath + "\\ImageCacheTile.exe";
if (System.IO.File.Exists(exeFileName))
{
Process p = new Process();
p.StartInfo.FileName = exeFileName;
if (p.Start())
{
p.WaitForInputIdle();
Process p2 = new Process();
p2.StartInfo.FileName = "cmd.exe";
//设定程式执行参数 “/C”表示执行完命令后马上退出
p2.StartInfo.Arguments = "/c date " + Arg;
//关闭Shell的使用
p2.StartInfo.UseShellExecute = false;
//重定向标准输入
p2.StartInfo.RedirectStandardInput = true;
p2.StartInfo.RedirectStandardOutput = true;
//重定向错误输出
p2.StartInfo.RedirectStandardError = true;
//设置不显示doc窗口
p2.StartInfo.CreateNoWindow = true;
//启动
p2.Start();
}
}
else
{
MessageBox.Show(Properties.Resources.Str_CacheTools, Properties.Resources.Str_information,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Properties.Resources.Str_information,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
好了 大功告成。