/// <summary>
/// 自动更新(从网络共享路径),程序删除,从网络复制,系统启动
/// </summary>
private void UpdateFromShare()
{
string strXCopyFiles = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XCopyFiles.bat");
string tempUpdatePath = "\\\\192.168.1.100\\AutoUpdate\\AutomatedOrdering"; //自动更新路径
string netUser = "autoupdate"; //网络用户名
string netPassword = "autoupdate"; //网络密码
//生成更新BAT(NET USE 获取网络读取权限,复制文件)
using (StreamWriter swXcopy = new StreamWriter(strXCopyFiles,false,Encoding.Default))
{
swXcopy.WriteLine(
string.Format(
@"@echo off"+Environment.NewLine
+"net use {0} {2} /user:{1}"+Environment.NewLine
+ "xcopy /y/s/e/v \"" + tempUpdatePath + "\" \"" + Directory.GetCurrentDirectory() + "\\\"",
tempUpdatePath,
netUser,
netPassword
));
}
//生成BAT(删除旧版本,删除BAT,启动更新后的程序)
string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "KillApp.bat");
using (StreamWriter bat = new StreamWriter(filename, false, Encoding.Default))
{
// 自删除,自启动
bat.WriteLine(string.Format(@"
@echo off
:selfkill
attrib -a -r -s -h {0}
del {0}
if exist {0} goto selfkill
call XCopyFiles.bat
del XCopyFiles.bat "+ Environment.NewLine
+ "\"" + Application.ExecutablePath + "\"" + Environment.NewLine
+ " del %0 ",
AppDomain.CurrentDomain.FriendlyName
));
}
// 启动自删除批处理文件
ProcessStartInfo info = new ProcessStartInfo(filename);
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
// 强制关闭当前进程
Environment.Exit(0);
}
网友评论