华恒Mes鼎捷代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

499 lines
20 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml;
using System.Threading;
using ICSSoft.Base.Config.AppConfig;
namespace ICSSoft.AutoUpdate.Upload
{
public partial class FormUpload : DevExpress.XtraEditors.XtraForm
{
private string rootPath = "";
private string saveFileName = Application.StartupPath + "\\selectfiles.ini";
private List<string> selectFilesList = new List<string>();
Dictionary<string, string> idKeyDic = new Dictionary<string, string>();//ID键值对
DataTable treeDataSource = null;
public FormUpload()
{
InitializeComponent();
if (!File.Exists(saveFileName))
{
using (File.Create(saveFileName))
{
}
}
using (StreamReader sr = new StreamReader(saveFileName))
{
while (sr.Peek() > -1)
{
string value = sr.ReadLine();
if (!string.IsNullOrEmpty(value))
{
selectFilesList.Add(value);
}
}
}
rootPath = Application.StartupPath;
treeDataSource = new DataTable();
treeDataSource.Columns.Add("文件路径", typeof(string));
treeDataSource.Columns.Add("文件名称", typeof(string));
treeDataSource.Columns.Add("文件类型", typeof(string));
treeDataSource.Columns.Add("文件大小", typeof(string));
treeDataSource.Columns.Add("修改时间", typeof(string));
treeDataSource.Columns.Add("ID", typeof(string));
treeDataSource.Columns.Add("ParentID", typeof(string));
treeDataSource.Columns.Add("选择", typeof(bool));
BindFile(rootPath);//加载文件
treeFile.DataSource = treeDataSource.DefaultView;
}
private void BindDefalutNodeState()
{
foreach(TreeListNode node in treeFile.Nodes)
{
if (selectFilesList.Contains(node.GetValue(colFilePath.ColumnHandle).ToString()) == true)
{
node.CheckState = CheckState.Checked;
}
BindDefalutNodeState(node);
}
}
private void BindDefalutNodeState(TreeListNode tn)
{
foreach (TreeListNode node in tn.Nodes)
{
if (selectFilesList.Contains(node.GetValue(colFilePath.ColumnHandle).ToString()) == true)
{
node.CheckState = CheckState.Checked;
}
BindDefalutNodeState(node);
}
}
private void BindFile(string path)
{
DirectoryInfo fatherFolder = new DirectoryInfo(path);
//递归删除子文件夹内文件
foreach (DirectoryInfo childFolder in fatherFolder.GetDirectories())
{
DataRow dr = treeDataSource.NewRow();
dr["文件路径"] = childFolder.FullName;
dr["文件名称"] = childFolder.Name;
dr["文件类型"] = "文件夹";
dr["文件大小"] = "";
dr["修改时间"] = childFolder.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
dr["ID"] = childFolder.FullName;
dr["ParentID"] = path;
dr["选择"] = selectFilesList.Contains(childFolder.FullName);
treeDataSource.Rows.Add(dr);
treeDataSource.AcceptChanges();
BindFile(childFolder.FullName);
}
//删除当前文件夹内文件
FileInfo[] files = fatherFolder.GetFiles();
foreach (FileInfo file in files)
{
DataRow dr = treeDataSource.NewRow();
dr["文件路径"] = file.FullName;
dr["文件名称"] = Path.GetFileNameWithoutExtension(file.FullName);
dr["文件类型"] = Path.GetExtension(file.FullName);
dr["文件大小"] = file.Length;
dr["修改时间"] = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
dr["ID"] = file.FullName;
dr["ParentID"] = path;
dr["选择"] = selectFilesList.Contains(file.FullName);
treeDataSource.Rows.Add(dr);
treeDataSource.AcceptChanges();
}
}
private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.Close();
}
private void btnSAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
foreach (TreeListNode node in treeFile.Nodes)
{
node.CheckState = CheckState.Checked;
SetCheckedChildNodes(node, node.CheckState);
SetCheckedParentNodes(node, node.CheckState);
}
}
private void btnCAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
foreach (TreeListNode node in treeFile.Nodes)
{
node.CheckState = CheckState.Unchecked;
SetCheckedChildNodes(node, node.CheckState);
SetCheckedParentNodes(node, node.CheckState);
}
}
private void SaveNodeState()
{
foreach (TreeListNode node in treeFile.Nodes)
{
if (node.CheckState == CheckState.Checked)
{
node.SetValue(colSelect.ColumnHandle, true);
}
else
{
node.SetValue(colSelect.ColumnHandle, false);
}
SaveNodeState(node);
}
}
private void SaveNodeState(TreeListNode tn)
{
foreach (TreeListNode node in tn.Nodes)
{
if (node.CheckState == CheckState.Checked)
{
node.SetValue(colSelect.ColumnHandle, true);
}
else
{
node.SetValue(colSelect.ColumnHandle, false);
}
SaveNodeState(node);
}
}
private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
try
{
SaveNodeState();
string mess = "";
for (int i = 0; i < treeDataSource.Rows.Count; i++)
{
if (treeDataSource.Rows[i].RowState == DataRowState.Deleted)
continue;
if (treeDataSource.Rows[i]["文件类型"].ToString() == "文件夹")
continue;
if (Convert.ToBoolean(treeDataSource.Rows[i]["选择"]))
{
mess += treeDataSource.Rows[i]["文件路径"].ToString() + "\r\n";
}
}
using (StreamWriter sw = new StreamWriter(saveFileName))
{
sw.Write(mess);
}
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 获取文件
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public byte[] ConvertStreamToByteBuffer(Stream s)
{
MemoryStream ms = new MemoryStream();
int b;
while ((b = s.ReadByte()) != -1)
{
ms.WriteByte((byte)b);
}
return ms.ToArray();
}
private string GetXmlold()
{
string baseUpadtePath = "";
object obj = AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "GetBaseUploadUrl", new object[] { });
baseUpadtePath = obj.ToString();
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<UpInfos>
<FileInfo>";
object flag = AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "IsHadAutoXml", new object[] { });
bool hadFlag = Convert.ToBoolean(flag);
if (hadFlag)
{
string downFile = baseUpadtePath + "\\autoupdate.xml";
object downObject = AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "DownFile", new object[] { downFile});
byte[] bs = (byte[])downObject;
File.WriteAllBytes(Application.StartupPath + "\\sautoupdate.xml", bs);
Thread.Sleep(3000);
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + "\\sautoupdate.xml"); //加载Xml文件
XmlNode rootElem = doc.SelectSingleNode("UpInfos"); //获取根节点
XmlNodeList personNodes = rootElem.ChildNodes; //获取person子节点集合
foreach (XmlNode node in personNodes)
{
if (node.Name != "FileInfo")
continue;
XmlNodeList fileList = node.ChildNodes;
foreach (XmlNode file in fileList)
{
DataRow[] drs = treeDataSource.Select("文件路径='" + file.ChildNodes[1].InnerText.Replace(baseUpadtePath,Application.StartupPath)+ "'");
if (drs.Length > 0)
continue;
xml += "<File>\n";
xml += "<Name>" +file.ChildNodes[0].InnerText+ "</Name>\n";
xml += "<Url>" + file.ChildNodes[1].InnerText + "</Url>\n";
xml += "<DateTime>" + file.ChildNodes[2].InnerText + "</DateTime>\n";
xml += "</File>\n";
}
}
}
for (int i = 0; i < treeDataSource.Rows.Count; i++)
{
if (treeDataSource.Rows[i].RowState == DataRowState.Deleted)
continue;
if (treeDataSource.Rows[i]["文件类型"].ToString() == "文件夹")
continue;
if (Convert.ToBoolean(treeDataSource.Rows[i]["选择"]) == false)
continue;
xml += "<File>\n";
xml += "<Name>" +Path.GetFileName( treeDataSource.Rows[i]["文件路径"].ToString()) + "</Name>\n";
xml += "<Url>" + baseUpadtePath+ treeDataSource.Rows[i]["文件路径"].ToString().Replace(Application.StartupPath,"") + "</Url>\n";
xml += "<DateTime>" +Convert.ToDateTime(treeDataSource.Rows[i]["修改时间"].ToString()).ToString("yyyyMMddHHmmss") + "</DateTime>\n";
xml += "</File>\n";
}
string endStr = @"
</FileInfo>
</UpInfos>";
return xml + endStr;
}
private string GetXml()
{
string baseUpadtePath = "";
object obj = AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "GetBaseUploadUrl", new object[] { });
baseUpadtePath = obj.ToString();
Dictionary<string, string[]> nodelist = new Dictionary<string, string[]>();
for (int i = 0; i < treeDataSource.Rows.Count; i++)
{
if (treeDataSource.Rows[i].RowState == DataRowState.Deleted)
continue;
if (treeDataSource.Rows[i]["文件类型"].ToString() == "文件夹")
continue;
if (Convert.ToBoolean(treeDataSource.Rows[i]["选择"]) == false)
continue;
string[] xmlinfo = new string[3];
xmlinfo[0] = Path.GetFileName(treeDataSource.Rows[i]["文件路径"].ToString());
xmlinfo[1] = baseUpadtePath + treeDataSource.Rows[i]["文件路径"].ToString().Replace(Application.StartupPath, "");
xmlinfo[2] = Convert.ToDateTime(treeDataSource.Rows[i]["修改时间"].ToString()).ToString("yyyyMMddHHmmss");
if (!nodelist.ContainsKey(xmlinfo[1]))
{
nodelist.Add(xmlinfo[1], xmlinfo);
}
else
{
nodelist[xmlinfo[1]] = xmlinfo;
}
}
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<UpInfos>
<FileInfo>";
object flag = AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "IsHadAutoXml", new object[] { });
bool hadFlag = Convert.ToBoolean(flag);
if (hadFlag)
{
string downFile = baseUpadtePath + "\\autoupdate.xml";
object downObject = AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "DownFile", new object[] { downFile });
byte[] bs = (byte[])downObject;
File.WriteAllBytes(Application.StartupPath + "\\sautoupdate.xml", bs);
Thread.Sleep(3000);
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + "\\sautoupdate.xml"); //加载Xml文件
XmlNode rootElem = doc.SelectSingleNode("UpInfos"); //获取根节点
XmlNodeList personNodes = rootElem.ChildNodes; //获取person子节点集合
foreach (XmlNode node in personNodes)
{
XmlNodeList fileList = node.ChildNodes;
foreach (XmlNode file in fileList)
{
DataRow[] drs = treeDataSource.Select("文件路径='" + file.ChildNodes[1].InnerText.Replace(baseUpadtePath, Application.StartupPath) + "'");
if (drs.Length != 1)
continue;
string[] xmlinfo = new string[3];
xmlinfo[0] = file.ChildNodes[0].InnerText;
xmlinfo[1] = file.ChildNodes[1].InnerText;
xmlinfo[2] = file.ChildNodes[2].InnerText;
if (!nodelist.ContainsKey(xmlinfo[1]))
{
nodelist.Add(xmlinfo[1], xmlinfo);
}
}
}
}
foreach (string key in nodelist.Keys)
{
xml += "<File>\n";
xml += "<Name>" + nodelist[key][0] + "</Name>\n";
xml += "<Url>" + nodelist[key][1] + "</Url>\n";
xml += "<DateTime>" + nodelist[key][2] + "</DateTime>\n";
xml += "</File>\n";
}
string endStr = @"
</FileInfo>
</UpInfos>";
return xml + endStr;
}
private void btnUpLoad_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
string xmlName = Application.StartupPath + "\\autoupdate.xml";
try
{
btnSAll.Enabled = false;
btnCAll.Enabled = false;
btnSave.Enabled = false;
btnUpLoad.Enabled = false;
btnExit.Enabled = false;
SaveNodeState();
for (int i = 0; i < treeDataSource.Rows.Count; i++)
{
if (treeDataSource.Rows[i].RowState == DataRowState.Deleted)
continue;
if (treeDataSource.Rows[i]["文件类型"].ToString() == "文件夹")
continue;
if (treeDataSource.Rows[i]["文件路径"].ToString() == xmlName)
continue;
if (Convert.ToBoolean(treeDataSource.Rows[i]["选择"]))
{
string fileName = treeDataSource.Rows[i]["文件路径"].ToString();
using (Stream stream = (Stream)File.OpenRead(fileName))
{
FileInfo f = new FileInfo(fileName);
byte[] btyes = ConvertStreamToByteBuffer(stream);
string _FileName = fileName.Replace(Application.StartupPath, "");
long FileSize = f.Length;
lblState.Caption = "正在上传:" + _FileName;
Application.DoEvents();
object obj = AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "UploadFile", new object[] { btyes, fileName.Replace(Application.StartupPath, "") });
}
}
}
string xml = GetXml();
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
xdoc.Save(xmlName);
lblState.Caption = "就绪";
Application.DoEvents();
using (Stream stream = (Stream)File.OpenRead(xmlName))
{
byte[] bs = ConvertStreamToByteBuffer(stream);
AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "UploadFile", new object[] { bs, xmlName.Replace(Application.StartupPath, "") });
}
btnSAll.Enabled = true;
btnCAll.Enabled = true;
btnSave.Enabled = true;
btnUpLoad.Enabled = true;
btnExit.Enabled = true;
MessageBox.Show("上传成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
btnSAll.Enabled = true;
btnCAll.Enabled = true;
btnSave.Enabled = true;
btnUpLoad.Enabled = true;
btnExit.Enabled = true;
MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void treeFile_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
private void treeFile_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
/// <summary>
/// 设置子节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
/// <summary>
/// 设置父节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
if (node.ParentNode != null)
{
bool b = false;
CheckState state;
for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
{
state = (CheckState)node.ParentNode.Nodes[i].CheckState;
if (!check.Equals(state))
{
b = !b;
break;
}
}
node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
SetCheckedParentNodes(node.ParentNode, check);
}
}
private void FormUpload_Load(object sender, EventArgs e)
{
BindDefalutNodeState();
}
////
}
}