/// <summary> /// 网络文件夹树形目录节点选择 /// </summary> private void TreeViewNetFolder_AfterSelect(object sender, TreeViewEventArgs e) { if (AppCommon.IsNothing(e.Node) == false) { textBoxNetFolderPath.Text = e.Node.FullPath; textBoxNetFolderId.Text = e.Node.Name; } }
private void LoginKeeping() { WebClient WebClient = new WebClient(); Uri PostUri = default(Uri); string PostString = ""; byte[] PostData = { }; PostUri = new Uri("" + AppConfig.ServerHost + "/sync/login.ashx?timestamp=" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ""); PostString = "loginid=" + HttpUtility.UrlEncode(AppConfig.LoginId) + "&password="******""; PostData = Encoding.UTF8.GetBytes(PostString); WebClient.Proxy = null; WebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); WebClient.Headers.Add("Cookie", AppConfig.UserSession); WebClient.UploadDataCompleted += (object sender, UploadDataCompletedEventArgs e) => { try { if (e.Cancelled == false) { if (AppCommon.IsNothing(e.Error) == true) { WebClient.Dispose(); } else { // 失败 } } else { // 失败 } } catch (Exception) { // 失败 } finally { if (AppCommon.IsNothing(WebClient) == false) { WebClient.Dispose(); } } }; WebClient.UploadDataAsync(PostUri, PostData); }
/// <summary> /// 同步设置按钮点击事件 /// </summary> private void ButtonSetting_Click(object sender, EventArgs e) { XmlDocument XDocument = new XmlDocument(); XmlNode XNode = default(XmlNode); string XPath = "/configuration/appSettings/add[@key=\"{key}\"]"; string ConfigPath = AppCommon.PathCombine(AppConfig.AppPath, "dboxSyncer.exe.config"); string IntervalTime = listBoxIntervalTime.Text; string UploadSpeed = textBoxUploadSpeed.Text; string DownloadSpeed = textBoxDownloadSpeed.Text; if (UploadSpeed.TypeInt() < 0 || UploadSpeed.TypeInt() > 1000) { MessageBox.Show(LangTable["tipsUploadSpeedError"].TypeString()); return; } if (DownloadSpeed.TypeInt() < 0 || DownloadSpeed.TypeInt() > 1000) { MessageBox.Show(LangTable["tipsDownloadSpeedError"].TypeString()); return; } XDocument.Load(ConfigPath); XNode = (XmlNode)XDocument.SelectSingleNode(XPath.Replace("{key}", "IntervalTime")); if (AppCommon.IsNothing(XNode) == false) { XNode.Attributes["value"].InnerText = IntervalTime; } XNode = (XmlNode)XDocument.SelectSingleNode(XPath.Replace("{key}", "UploadSpeed")); if (AppCommon.IsNothing(XNode) == false) { XNode.Attributes["value"].InnerText = UploadSpeed; } XNode = (XmlNode)XDocument.SelectSingleNode(XPath.Replace("{key}", "DownloadSpeed")); if (AppCommon.IsNothing(XNode) == false) { XNode.Attributes["value"].InnerText = DownloadSpeed; } XDocument.Save(ConfigPath); AppCommon.ProcessExit("dboxSyncer.Task"); AppCommon.ProcessExecute(AppCommon.PathCombine(AppConfig.AppPath, "dboxSyncer.Task.exe"), "", -1); }
/// <summary> /// 用户登录数据保存 /// </summary> private void LoginDataSave() { XmlDocument XDocument = new XmlDocument(); XmlNode XNode = default(XmlNode); string XPath = "/configuration/appSettings/add[@key=\"{key}\"]"; string ConfigPath = AppCommon.PathCombine(AppConfig.AppPath, "dboxSyncer.exe.config"); XDocument.Load(ConfigPath); XNode = (XmlNode)XDocument.SelectSingleNode(XPath.Replace("{key}", "ServerHost")); if (AppCommon.IsNothing(XNode) == false) { XNode.Attributes["value"].InnerText = AppConfig.ServerHost; } XNode = (XmlNode)XDocument.SelectSingleNode(XPath.Replace("{key}", "LoginId")); if (AppCommon.IsNothing(XNode) == false) { XNode.Attributes["value"].InnerText = AppConfig.LoginId; } XNode = (XmlNode)XDocument.SelectSingleNode(XPath.Replace("{key}", "Password")); if (AppCommon.IsNothing(XNode) == false) { XNode.Attributes["value"].InnerText = AppConfig.Password; } XNode = (XmlNode)XDocument.SelectSingleNode(XPath.Replace("{key}", "UserSession")); if (AppCommon.IsNothing(XNode) == false) { XNode.Attributes["value"].InnerText = AppConfig.UserSession; } XNode = (XmlNode)XDocument.SelectSingleNode(XPath.Replace("{key}", "AutoLogin")); if (AppCommon.IsNothing(XNode) == false) { XNode.Attributes["value"].InnerText = AppConfig.AutoLogin; } XDocument.Save(ConfigPath); }
/// <summary> /// 获取配置文件参数值 /// </summary> private static string ConfigValue(string Name) { System.Configuration.Configuration Config = default(System.Configuration.Configuration); AppSettingsSection AppSetting = default(AppSettingsSection); Config = ConfigurationManager.OpenExeConfiguration(AppCommon.PathCombine(AppConfig.AppPath, "dboxSyncer.exe")); AppSetting = (System.Configuration.AppSettingsSection)Config.GetSection("appSettings"); if (AppCommon.IsNothing(AppSetting.Settings[Name]) == true) { return(string.Empty); } else { return(AppSetting.Settings[Name].Value); } }
/// <summary> /// 同步文件夹删除按钮点击事件 /// </summary> private void ButtonDelete_Click(object sender, EventArgs e) { XmlDocument XDocument = new XmlDocument(); XmlNode XNodes = default(XmlNode); int Item = 0; int Index = 0; if (listViewData.SelectedItems.Count == 0) { MessageBox.Show(LangTable["tipsSelectItem"].TypeString()); return; } XDocument.Load(AppConfig.DataPath); XNodes = XDocument.SelectSingleNode("rules"); if (AppCommon.IsNothing(XNodes) == false) { if (XNodes.ChildNodes.Count == 0) { return; } Item = listViewData.SelectedItems[listViewData.SelectedItems.Count - 1].Index; foreach (XmlNode XNode in XNodes.ChildNodes) { if (Item == Index) { XNode.ParentNode.RemoveChild(XNode); break; } Index++; } XDocument.Save(AppConfig.DataPath); ListViewDataReload(); } }
/// <summary> /// 进程执行(带工作目录参数) /// </summary> public static void ProcessExecute(string FileName, string WorkingDirectory, string Arguments, int Wait) { Process Process = new Process(); try { Process.StartInfo.FileName = FileName; Process.StartInfo.WorkingDirectory = WorkingDirectory; Process.StartInfo.Arguments = Arguments; Process.StartInfo.UseShellExecute = false; Process.StartInfo.RedirectStandardInput = false; Process.StartInfo.RedirectStandardOutput = false; Process.StartInfo.RedirectStandardError = false; Process.StartInfo.CreateNoWindow = true; Process.StartInfo.Verb = "runas"; Process.Start(); if (Wait == 0) { Process.WaitForExit(); } else if (Wait > 0) { Process.WaitForExit(Wait); } } catch (Exception) { } finally { if (AppCommon.IsNothing(Process) == false) { Process.Close(); Process.Dispose(); } } }
/// <summary> /// 文件下载 /// </summary> private static void FileDownload(int FileId, string FileCodeId, string FileName, string LocalFolderPath) { WebClient WebClient = new WebClient(); FileStream FileStream = default(FileStream); Stream WebStream = default(Stream); string DownloadUrl = ""; string TempFilePath = ""; string SaveFilePath = ""; byte[] ByteBuffer = {}; int ByteRead = 0; int IntervalTime = 0; try { DownloadUrl = "" + AppConfig.ServerHost + "/sync/file-download.ashx?id=" + FileId + "&codeid=" + FileCodeId + "×tamp=" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ""; if (Directory.Exists(AppCommon.PathCombine(AppConfig.AppPath, "temp")) == false) { Directory.CreateDirectory(AppCommon.PathCombine(AppConfig.AppPath, "temp")); } TempFilePath = AppCommon.PathCombine(AppConfig.AppPath, "temp", FileName); SaveFilePath = AppCommon.PathCombine(LocalFolderPath, FileName); if (File.Exists(TempFilePath) == true) { File.Delete(TempFilePath); } if (AppConfig.DownloadSpeed.TypeInt() == 0) { IntervalTime = 0; } else { IntervalTime = (int)1000 / (AppConfig.DownloadSpeed.TypeInt()); } FileStream = new FileStream(TempFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 4096, true); WebClient.Proxy = null; WebClient.Headers.Add("Cookie", AppConfig.UserSession); WebStream = WebClient.OpenRead(DownloadUrl); ByteBuffer = new byte[1024]; ByteRead = WebStream.Read(ByteBuffer, 0, 1024); while (ByteRead > 0) { FileStream.Write(ByteBuffer, 0, ByteRead); ByteRead = WebStream.Read(ByteBuffer, 0, 1024); Application.DoEvents(); if (IntervalTime > 0) { Thread.Sleep(IntervalTime); } } WebClient.Dispose(); WebStream.Close(); WebStream.Dispose(); FileStream.Close(); FileStream.Dispose(); File.Copy(TempFilePath, SaveFilePath, true); SyncLog("download", SaveFilePath); } catch (Exception ex) { File.AppendAllText(AppCommon.PathCombine(AppConfig.AppPath, "error.log"), "" + DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n"); } finally { if (AppCommon.IsNothing(WebClient) == false) { WebClient.Dispose(); } if (AppCommon.IsNothing(WebStream) == false) { WebStream.Close(); WebStream.Dispose(); } if (AppCommon.IsNothing(FileStream) == false) { FileStream.Close(); FileStream.Dispose(); } File.Delete(TempFilePath); } }
/// <summary> /// 新版本上传 /// </summary> private static void FileUpversion(string FilePath, string FileSize, string FileHash, int FileId) { WebClient WebClient = new WebClient(); FileStream FileStream = default(FileStream); Stream WebStream = default(Stream); string UpversionUrl = ""; byte[] ByteBuffer = {}; int ByteRead = 0; int IntervalTime = 0; try { UpversionUrl = "" + AppConfig.ServerHost + "/sync/file-upversion.ashx?guid=" + System.Guid.NewGuid().ToString("N") + "&fileid=" + FileId + "&filepath=" + HttpUtility.UrlEncode(FilePath) + "&filesize=" + FileSize + "&filehash=" + FileHash + "×tamp=" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ""; if (AppConfig.UploadSpeed.TypeInt() == 0) { IntervalTime = 0; } else { IntervalTime = (int)1000 / (AppConfig.UploadSpeed.TypeInt()); } FileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true); WebClient.Proxy = null; WebClient.Headers.Add("Cookie", AppConfig.UserSession); WebStream = WebClient.OpenWrite(UpversionUrl); ByteBuffer = new byte[1024]; ByteRead = FileStream.Read(ByteBuffer, 0, 1024); while (ByteRead > 0) { WebStream.Write(ByteBuffer, 0, ByteRead); ByteRead = FileStream.Read(ByteBuffer, 0, 1024); Application.DoEvents(); if (IntervalTime > 0) { Thread.Sleep(IntervalTime); } } WebClient.Dispose(); WebStream.Close(); WebStream.Dispose(); FileStream.Close(); FileStream.Dispose(); SyncLog("upversion", FilePath); } catch (Exception ex) { File.AppendAllText(AppCommon.PathCombine(AppConfig.AppPath, "error.log"), "" + DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n"); } finally { if (AppCommon.IsNothing(WebClient) == false) { WebClient.Dispose(); } if (AppCommon.IsNothing(WebStream) == false) { WebStream.Close(); WebStream.Dispose(); } if (AppCommon.IsNothing(FileStream) == false) { FileStream.Close(); FileStream.Dispose(); } } }
/// <summary> /// 读取网络文件夹获取文件集合返回list<hashtable> /// </summary> private static void NetDataToList(int NetFolderId, ref List <Hashtable> NetDataList, ref Hashtable NetFolderTable) { WebClient WebClient = new WebClient(); Uri HttpUri = default(Uri); byte[] ResponseData = {}; string XData = ""; try { HttpUri = new Uri("" + AppConfig.ServerHost + "/sync/file-list-xml.ashx?folderid=" + NetFolderId + "×tamp=" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ""); WebClient.Proxy = null; WebClient.Headers.Add("Cookie", AppConfig.UserSession); ResponseData = WebClient.DownloadData(HttpUri); WebClient.Dispose(); XData = Encoding.UTF8.GetString(ResponseData); if (XData.IndexOf("<item>") == -1 || XData.IndexOf("</item>") == -1) { return; } XmlDocument XDocument = new XmlDocument(); XmlNodeList XNodes = default(XmlNodeList); XDocument.LoadXml(XData); XNodes = XDocument.SelectNodes("/root/folder"); foreach (XmlNode XNode in XNodes) { NetFolderTable.Add("Share", XNode.SelectSingleNode("share").InnerText); NetFolderTable.Add("Lock", XNode.SelectSingleNode("lock").InnerText); NetFolderTable.Add("Purview", XNode.SelectSingleNode("purview").InnerText); } XNodes = XDocument.SelectNodes("/root/files/item"); foreach (XmlNode XNode in XNodes) { Hashtable Table = new Hashtable(); Table.Add("Id", XNode.SelectSingleNode("id").InnerText); Table.Add("VersionId", XNode.SelectSingleNode("versionId").InnerText); Table.Add("CodeId", XNode.SelectSingleNode("codeId").InnerText); Table.Add("Hash", XNode.SelectSingleNode("hash").InnerText); Table.Add("Name", XNode.SelectSingleNode("name").InnerText); Table.Add("Size", XNode.SelectSingleNode("size").InnerText); Table.Add("Lock", XNode.SelectSingleNode("lock").InnerText); Table.Add("UpdateTime", XNode.SelectSingleNode("updateTime").InnerText); NetDataList.Add(Table); } } catch (Exception ex) { File.AppendAllText(AppCommon.PathCombine(AppConfig.AppPath, "error.log"), "" + DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n"); } finally { if (AppCommon.IsNothing(WebClient) == false) { WebClient.Dispose(); } } }
/// <summary> /// 网络文件夹数据加载 /// </summary> private void NetFolderLoad() { WebClient WebClient = new WebClient(); Uri HttpUri = default(Uri); HttpUri = new Uri("" + AppConfig.ServerHost + "/sync/folder-list-xml.ashx?timestamp=" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ""); labelLoadingTips.Text = LangTable["tipsLoading"].TypeString(); WebClient.Proxy = null; WebClient.Headers.Add("Cookie", AppConfig.UserSession); WebClient.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) => { XmlDocument XDocument = new XmlDocument(); XmlNodeList XNodes = default(XmlNodeList); string XData = ""; try { if (e.Cancelled == false) { if (AppCommon.IsNothing(e.Error) == true) { XData = Encoding.UTF8.GetString(e.Result); WebClient.Dispose(); XDocument.LoadXml(XData); XNodes = XDocument.SelectNodes("/folders/item"); if (AppCommon.IsNothing(XNodes) == false) { if (XNodes.Count == 0) { labelLoadingTips.Text = LangTable["tipsNoData"].TypeString(); } else { labelLoadingTips.Visible = false; NetFolderLoad_NodeAdd(treeViewNetFolder.Nodes, ref XNodes, 0); } } } } } catch (Exception) { labelLoadingTips.Text = LangTable["tipsLoadFailed"].TypeString(); } finally { if (AppCommon.IsNothing(WebClient) == false) { WebClient.Dispose(); } } }; WebClient.DownloadDataAsync(HttpUri); }
/// <summary> /// 同步文件夹添加按钮点击事件 /// </summary> private void ButtonAdd_Click(object sender, EventArgs e) { XmlDocument XDocument = new XmlDocument(); XmlNodeList XNodes = default(XmlNodeList); XmlElement XElement = default(XmlElement); XmlNode XRoot = default(XmlNode); if (string.IsNullOrEmpty(textBoxLocalFolderPath.Text) == true) { MessageBox.Show(LangTable["tipsSelectLocalFolder"].TypeString()); return; } if (string.IsNullOrEmpty(textBoxNetFolderPath.Text) == true) { MessageBox.Show(LangTable["tipsSelectNetFolder"].TypeString()); return; } if (string.IsNullOrEmpty(textBoxNetFolderId.Text) == true) { return; } XDocument.Load(AppConfig.DataPath); XNodes = XDocument.SelectSingleNode("rules").ChildNodes; if (AppCommon.IsNothing(XNodes) == false) { if (XNodes.Count >= 10) { MessageBox.Show(LangTable["tipsFolderCountLimit"].TypeString()); return; } foreach (XmlNode XNode in XNodes) { XElement = (XmlElement)XNode; if (XElement.ChildNodes[0].InnerText == textBoxLocalFolderPath.Text || XElement.ChildNodes[1].InnerText == textBoxNetFolderPath.Text || XElement.ChildNodes[2].InnerText == textBoxNetFolderId.Text) { MessageBox.Show(LangTable["tipsFolderExisted"].TypeString()); return; } } } XRoot = XDocument.SelectSingleNode("rules"); XmlElement XItem = XDocument.CreateElement("item"); XRoot.AppendChild(XItem); XmlElement LocalFolderPath = XDocument.CreateElement("localFolderPath"); LocalFolderPath.InnerText = textBoxLocalFolderPath.Text; XItem.AppendChild(LocalFolderPath); XmlElement NetFolderPath = XDocument.CreateElement("netFolderPath"); NetFolderPath.InnerText = textBoxNetFolderPath.Text; XItem.AppendChild(NetFolderPath); XmlElement NetFolderId = XDocument.CreateElement("netFolderId"); NetFolderId.InnerText = textBoxNetFolderId.Text; XItem.AppendChild(NetFolderId); XmlElement Type = XDocument.CreateElement("type"); if (radioButtonTypeSync.Checked == true) { Type.InnerText = "sync"; } if (radioButtonTypeUpload.Checked == true) { Type.InnerText = "upload"; } if (radioButtonTypeDownload.Checked == true) { Type.InnerText = "download"; } XItem.AppendChild(Type); XDocument.Save(AppConfig.DataPath); this.Close(); }
/// <summary> /// 用户登录处理程序 /// </summary> private void LoginProcess(string ServerHost, string LoginId, string Password, string AutoLogin, bool Taskbar) { WebClient WebClient = new WebClient(); Uri PostUri = default(Uri); string PostString = ""; byte[] PostData = {}; PostUri = new Uri("" + ServerHost + "/sync/login.ashx?timestamp=" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ""); PostString = "loginid=" + HttpUtility.UrlEncode(LoginId) + "&password="******""; PostData = Encoding.UTF8.GetBytes(PostString); buttonLogin.Enabled = false; buttonLogin.Text = LangTable["buttonLogging"].TypeString(); WebClient.Proxy = null; WebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); WebClient.UploadDataCompleted += (object sender, UploadDataCompletedEventArgs e) => { string Result = ""; string Cookie = ""; try { if (e.Cancelled == false) { if (AppCommon.IsNothing(e.Error) == true) { Result = Encoding.UTF8.GetString(e.Result); Cookie = WebClient.ResponseHeaders.Get("Set-Cookie"); WebClient.Dispose(); if (Result == "complete") { AppConfig.UserSession = Cookie; LoginDataSave(); FormMain FormMain = new FormMain(); if (Taskbar == true) { FormMain.Show(); } else { FormMain.notifyIcon.Visible = true; } this.Hide(); } else if (Result == "lock-user-id") { labelLoginTips.Text = LangTable["tipsLockUserId"].TypeString(); } else { labelLoginTips.Text = LangTable["tipsLoginFailed"].TypeString(); } } else { labelLoginTips.Text = LangTable["tipsLoginFailed"].TypeString(); } } else { labelLoginTips.Text = LangTable["tipsLoginFailed"].TypeString(); } } catch (Exception) { labelLoginTips.Text = LangTable["tipsLoginFailed"].TypeString(); } finally { if (AppCommon.IsNothing(WebClient) == false) { WebClient.Dispose(); } buttonLogin.Enabled = true; buttonLogin.Text = LangTable["buttonLogin"].TypeString(); } }; WebClient.UploadDataAsync(PostUri, PostData); }