private void btn_chooseefolder_Click( object sender, EventArgs e)
{
try
{
FolderBrowserDialog folderOpen = new FolderBrowserDialog();
folderOpen.Description = "请选择放置更新文件的位置...";
folderOpen.ShowNewFolderButton = false;
folderOpen.RootFolder = Environment.SpecialFolder.MyComputer;
if (folderOpen.ShowDialog() == DialogResult.OK)
{
this.txt_serverpath.Text = folderOpen.SelectedPath;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.txt_serverpath.Text = string.Empty;
}
}
private void btn_chooseefile_Click( object sender, EventArgs e)
{
try
{
OpenFileDialog dlgFileOpen = new OpenFileDialog();
dlgFileOpen.InitialDirectory = @"F:\";
dlgFileOpen.ShowReadOnly = false;
dlgFileOpen.ReadOnlyChecked = true;
dlgFileOpen.Filter = "升级文件(*.DLL)|*.DLL";
if (dlgFileOpen.ShowDialog() == DialogResult.OK)
{
if (dlgFileOpen.ReadOnlyChecked == true)
{
this.txt_upfilepath.Text = dlgFileOpen.FileName.ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.txt_upfilepath.Text = string.Empty;
}
}
private void btn_quit_Click( object sender, EventArgs e)
{
base.Close();
}
private void btn_upfile_Click( object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txt_serverpath.Text.Trim()))
{
MessageBox.Show( "请输入或选择更新文件服务器地址!", "信息框", MessageBoxButtons.OK, MessageBoxIcon.Information);
txt_serverpath.Focus();
return;
}
if (String.IsNullOrEmpty(txt_upfilepath.Text.Trim()))
{
MessageBox.Show( "请输入或选择上传的更新文件!", "信息框", MessageBoxButtons.OK, MessageBoxIcon.Information);
txt_upfilepath.Focus();
return;
}
//服务器路径
string uriString = txt_serverpath.Text.Trim();
//保存Libinfo.xml位置
string uri = uriString;
//上传文件(路径+文件名)
string upFilePath = txt_upfilepath.Text.Trim();
//文件名
string fileName = upFilePath.Substring(upFilePath.LastIndexOf("\\") + 1);
//上传文件存放文件夹
string fileFolder= string.Empty;
if (!String.IsNullOrEmpty( this.txt_filefolder.Text.Trim()))
fileFolder = this.txt_filefolder.Text.Trim();
else
fileFolder = fileName.Substring(0, 2);
try
{
this.xmldocLibInfo = new XmlDocument();
//检查是否存在Libinfo.xml文件,如果本地存在升级配置文件,则直接加载此文件.
if (File.Exists(uri + @"\Libinfo.xml"))
{
this.xmldocLibInfo.Load(uri + @"\Libinfo.xml");
}
//如果本地不存在升级配置文件,则加载创建指定格式的XML文件.
else
{
this.xmldocLibInfo.LoadXml( "<?xml version=\"1.0\" encoding=\"utf-8\" ?><filesinfo></filesinfo>");
}
//如果服务器地址结尾非 / 则补充 /
if (!uriString.EndsWith( "/"))
{
uriString = uriString + "/";
}
//判断服务器端是否存在新上传文件对应的文件夹,上传路径+上传文件前俩位做为上传文件的文件夹
uriString = uriString + fileFolder;
if (!Directory.Exists(uriString))
{
Directory.CreateDirectory(uriString);
}
//服务器地址+上传文件名
uriString = uriString + @"\" + fileName;
// 创建WebClient实例
myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//使用UploadFile方法上载文件
myWebClient.UploadFile(uriString, "PUT", upFilePath);
//获取上传DLL,EXE的版本号
myAssemblyName = AssemblyName.GetAssemblyName(uriString);
//新增或更新升级配置文件
this.UpdateLibInfo(fileName, myAssemblyName.Version.ToString(), fileFolder);
MessageBox.Show(fileName + "上传成功!", "信息框",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.txt_upfilepath.Text = string.Empty;
this.txt_filefolder.Text = string.Empty;
}
catch (WebException errMsg)
{
MessageBox.Show(fileName + "上传失败:" + errMsg.Message, "信息框", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txt_upfilepath.Text = string.Empty;
}
catch (Exception)
{
throw new Exception( "LibInfo.xml文件装载失败!");
}
finally
{
this.xmldocLibInfo.Save(uri + @"\Libinfo.xml");
}
}
/// <summary>
/// 创建升级配置文件的节点属性:文件名,文件版本号,文件夹
/// </summary>
/// <param name="doc">升级配置文件</param>
/// <param name="node">xml文件节点</param>
/// <param name="strAttr">属性名</param>
/// <param name="strAttrValue">属性值</param>
private void addAttribute(XmlDocument doc, XmlElement node, string strAttr, string strAttrValue)
{
XmlAttribute attribute = doc.CreateAttribute(strAttr);
attribute.Value = strAttrValue;
node.Attributes.SetNamedItem(attribute);
}
/// <summary>
/// 新增或更新升级配置文件
/// </summary>
/// <param name="strFile">更新文件名</param>
/// <param name="strVersion">更新文件版本</param>
/// <param name="strRelPath">更新文件地址</param>
/// <returns></returns>
private bool UpdateLibInfo( string strFile, string strVersion, string strRelPath)
{
XmlDocumentFragment fragment = this.xmldocLibInfo.CreateDocumentFragment();
XmlElement element = this.xmldocLibInfo.CreateElement( "file");
this.addAttribute( this.xmldocLibInfo, element, "name", strFile);
this.addAttribute( this.xmldocLibInfo, element, "version", strVersion);
this.addAttribute( this.xmldocLibInfo, element, "relpath", strRelPath);
fragment.AppendChild(element);
XmlNodeList list = this.xmldocLibInfo.DocumentElement.SelectNodes( "/filesinfo/file");
XmlElement element2 = (XmlElement) this.xmldocLibInfo.SelectSingleNode( "//filesinfo");
bool flag = false;
foreach (XmlNode node in list)
{
if (strFile == node.Attributes["name"].Value)
{
element2.ReplaceChild(fragment.FirstChild, (XmlElement)node);
flag = true;
break;
}
}
if (!flag)
{
element2.AppendChild(fragment.FirstChild);
}
return true;
}