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 selectFilesList = new List(); Dictionary idKeyDic = new Dictionary();//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); } } /// /// 获取文件 /// /// /// 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 = @" "; 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 += "\n"; xml += "" +file.ChildNodes[0].InnerText+ "\n"; xml += "" + file.ChildNodes[1].InnerText + "\n"; xml += "" + file.ChildNodes[2].InnerText + "\n"; xml += "\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 += "\n"; xml += "" +Path.GetFileName( treeDataSource.Rows[i]["文件路径"].ToString()) + "\n"; xml += "" + baseUpadtePath+ treeDataSource.Rows[i]["文件路径"].ToString().Replace(Application.StartupPath,"") + "\n"; xml += "" +Convert.ToDateTime(treeDataSource.Rows[i]["修改时间"].ToString()).ToString("yyyyMMddHHmmss") + "\n"; xml += "\n"; } string endStr = @" "; return xml + endStr; } private string GetXml() { string baseUpadtePath = ""; object obj = AppConfig.InvokeWebservice(AppConfig.BaseServiceUri, "WebBaseService", "BaseService", "GetBaseUploadUrl", new object[] { }); baseUpadtePath = obj.ToString(); Dictionary nodelist = new Dictionary(); 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 = @" "; 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 += "\n"; xml += "" + nodelist[key][0] + "\n"; xml += "" + nodelist[key][1] + "\n"; xml += "" + nodelist[key][2] + "\n"; xml += "\n"; } string endStr = @" "; 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); } /// /// 设置子节点的状态 /// /// /// 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); } } /// /// 设置父节点的状态 /// /// /// 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(); } //// } }