程序难免有升级,或因功能修改,或因功能增加,或因BUG修复
当升级时,你是去公司的各个部门的每个用户的电脑上一台台重新安装应用程序还是让用户只点击一个按钮就实现呢!下面我就把我项目中用到的经验和代码分享
可能写得很菜,指高手指点。
我在公司的FTP网站上放有一个版本XML文件,一个安装文件:Setup.msi
版本XML文件为:Version.xml,内容格式见下:
<?xml version="1.0"?>
<Software>
<Version Info="0.0.0.3" />
</Software>程序先从FTP网站下载版本XML文件,读取FTP网站上安装文件的版本,然后与自己当前的程序版本比较,如果是FTP网站的版本比自己新,就下载Setup.msi安装文件,下载完成后,就运行Setup.msi安装文件,同时关闭退出自己。思路已说,见代码:下载XML版本文件
string sDownloadFileName = Application.StartupPath + @"\Version.xml";
try
{
string sFtpURL = "ftp://192.168.0.8/andy/Barcode/Version.xml";
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(sFtpURL);
ftpRequest.Credentials = new NetworkCredential("erp", "erp");
if (System.IO.File.Exists(sDownloadFileName))
{
System.IO.File.Delete(sDownloadFileName);
}
FileStream DownloadStream = new FileStream(sDownloadFileName, FileMode.Create);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
Stream ftpDownloadStream = ftpResponse.GetResponseStream();
long lLength = ftpResponse.ContentLength;
byte[] bBuffer = new byte[2048];
int iReadCount = ftpDownloadStream.Read(bBuffer, 0, 2048);
while (iReadCount > 0)
{
DownloadStream.Write(bBuffer, 0, iReadCount);
iReadCount = ftpDownloadStream.Read(bBuffer, 0, 2048);
}
ftpDownloadStream.Close();
DownloadStream.Close();
ftpResponse.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}网上XML版本文件的版本号与当前运行的程序的版本号对比:
string sNewVersion=null; //网上版本
XDocument xDoc = XDocument.Load(sDownloadFileName);
var versions = from version in xDoc.Descendants("Version")
select version.Attribute("Info").Value;
foreach (var version in versions)
{
sNewVersion = version;
}
string sNowVersion = Application.ProductVersion; //程序现在版本
StringBuilder sbNewVersion = new StringBuilder(5);
StringBuilder sbNowVersion = new StringBuilder(5);
for (int i1 = 0; i1 < sNewVersion.Length; i1++)
{
if (sNewVersion[i1] == '.')
{
continue;
}
else
{
sbNewVersion.Append(sNewVersion[i1]);
}
}
for (int i1 = 0; i1 < sNowVersion.Length; i1++)
{
if (sNowVersion[i1] == '.')
{
continue;
}
else
{
sbNowVersion.Append(sNowVersion[i1]);
}
}
if (Convert.ToInt32(sbNewVersion.ToString()) > Convert.ToInt32(sbNowVersion.ToString()))
{
//有新版本,需要升级
}
升级见下:string sDownloadSetup = Application.StartupPath + @"\Setup.msi";
try
{
string sFtpURL = "ftp://192.168.0.8/andy/Barcode/Setup.msi";
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(sFtpURL);
ftpRequest.Credentials = new NetworkCredential("erp", "erp");
if (System.IO.File.Exists(sDownloadSetup))
{
System.IO.File.Delete(sDownloadSetup);
}
FileStream DownloadStream = new FileStream(sDownloadSetup, FileMode.Create);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
Stream ftpDownloadStream = ftpResponse.GetResponseStream();
long lLength = ftpResponse.ContentLength;
byte[] bBuffer = new byte[2048];
int iReadCount = ftpDownloadStream.Read(bBuffer, 0, 2048);
while (iReadCount > 0)
{
DownloadStream.Write(bBuffer, 0, iReadCount);
iReadCount = ftpDownloadStream.Read(bBuffer, 0, 2048);
}
ftpDownloadStream.Close();
DownloadStream.Close();
ftpResponse.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
//启动安装文件
System.Diagnostics.Process.Start(sDownloadSetup);
//关闭自己
Application.Exit();
当升级时,你是去公司的各个部门的每个用户的电脑上一台台重新安装应用程序还是让用户只点击一个按钮就实现呢!下面我就把我项目中用到的经验和代码分享
可能写得很菜,指高手指点。
我在公司的FTP网站上放有一个版本XML文件,一个安装文件:Setup.msi
版本XML文件为:Version.xml,内容格式见下:
<?xml version="1.0"?>
<Software>
<Version Info="0.0.0.3" />
</Software>程序先从FTP网站下载版本XML文件,读取FTP网站上安装文件的版本,然后与自己当前的程序版本比较,如果是FTP网站的版本比自己新,就下载Setup.msi安装文件,下载完成后,就运行Setup.msi安装文件,同时关闭退出自己。思路已说,见代码:下载XML版本文件
string sDownloadFileName = Application.StartupPath + @"\Version.xml";
try
{
string sFtpURL = "ftp://192.168.0.8/andy/Barcode/Version.xml";
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(sFtpURL);
ftpRequest.Credentials = new NetworkCredential("erp", "erp");
if (System.IO.File.Exists(sDownloadFileName))
{
System.IO.File.Delete(sDownloadFileName);
}
FileStream DownloadStream = new FileStream(sDownloadFileName, FileMode.Create);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
Stream ftpDownloadStream = ftpResponse.GetResponseStream();
long lLength = ftpResponse.ContentLength;
byte[] bBuffer = new byte[2048];
int iReadCount = ftpDownloadStream.Read(bBuffer, 0, 2048);
while (iReadCount > 0)
{
DownloadStream.Write(bBuffer, 0, iReadCount);
iReadCount = ftpDownloadStream.Read(bBuffer, 0, 2048);
}
ftpDownloadStream.Close();
DownloadStream.Close();
ftpResponse.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}网上XML版本文件的版本号与当前运行的程序的版本号对比:
string sNewVersion=null; //网上版本
XDocument xDoc = XDocument.Load(sDownloadFileName);
var versions = from version in xDoc.Descendants("Version")
select version.Attribute("Info").Value;
foreach (var version in versions)
{
sNewVersion = version;
}
string sNowVersion = Application.ProductVersion; //程序现在版本
StringBuilder sbNewVersion = new StringBuilder(5);
StringBuilder sbNowVersion = new StringBuilder(5);
for (int i1 = 0; i1 < sNewVersion.Length; i1++)
{
if (sNewVersion[i1] == '.')
{
continue;
}
else
{
sbNewVersion.Append(sNewVersion[i1]);
}
}
for (int i1 = 0; i1 < sNowVersion.Length; i1++)
{
if (sNowVersion[i1] == '.')
{
continue;
}
else
{
sbNowVersion.Append(sNowVersion[i1]);
}
}
if (Convert.ToInt32(sbNewVersion.ToString()) > Convert.ToInt32(sbNowVersion.ToString()))
{
//有新版本,需要升级
}
升级见下:string sDownloadSetup = Application.StartupPath + @"\Setup.msi";
try
{
string sFtpURL = "ftp://192.168.0.8/andy/Barcode/Setup.msi";
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(sFtpURL);
ftpRequest.Credentials = new NetworkCredential("erp", "erp");
if (System.IO.File.Exists(sDownloadSetup))
{
System.IO.File.Delete(sDownloadSetup);
}
FileStream DownloadStream = new FileStream(sDownloadSetup, FileMode.Create);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
Stream ftpDownloadStream = ftpResponse.GetResponseStream();
long lLength = ftpResponse.ContentLength;
byte[] bBuffer = new byte[2048];
int iReadCount = ftpDownloadStream.Read(bBuffer, 0, 2048);
while (iReadCount > 0)
{
DownloadStream.Write(bBuffer, 0, iReadCount);
iReadCount = ftpDownloadStream.Read(bBuffer, 0, 2048);
}
ftpDownloadStream.Close();
DownloadStream.Close();
ftpResponse.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
//启动安装文件
System.Diagnostics.Process.Start(sDownloadSetup);
//关闭自己
Application.Exit();
这个很详细