public FileInformation(FileInformation fileInfo) { this.BookMark = fileInfo.BookMark; this.Path = fileInfo.Path; this.MD5 = fileInfo.MD5; this.Encode = fileInfo.Encode; this.UpdateTime = fileInfo.UpdateTime; }
private void mnuItemFileRename_Click(object sender, EventArgs e) { if (lsvOpenHistory.SelectedIndices.Count > 0) { string newFileName = new FormInputDialog().MyShowDialog("请输入文件名"); if (!string.IsNullOrEmpty(newFileName)) { FileInformation file = new FileInformation(CommonFunc.Config.FileInfoList[lsvOpenHistory.SelectedIndices[0]]); newFileName = Path.Combine(Path.GetDirectoryName(file.Path), Path.GetFileNameWithoutExtension(newFileName) + Path.GetExtension(file.Path)); FileHelper.RenameFileSafely(file.Path, newFileName); file.Path = newFileName; CommonFunc.Config.RemoveFile(lsvOpenHistory.SelectedIndices[0]); CommonFunc.Config.AddFile(file); SetListView(); } } }
public static FileInformation CheckFileMD5(FileInformation newFile, List<FileInformation> existFiles) { FileInformation result = null; for (int i = 0; i < existFiles.Count; i++) { if (newFile.MD5.Equals(existFiles[i].MD5)) { result = existFiles[i]; break; } } return result; }
/// <summary> /// 打开一个文件,并将文件添加到配置文件中去 /// </summary> /// <param name="filePath">文件完整路径</param> public void OpenFile(string filePath) { try { //System.IO.StreamReader reader = new System.IO.StreamReader(filePath); //TODO 提高读取和显示的效率,考虑两种办法:1、使用流;2、将整个txt缓存起来,通过指针遍历 //TODO 有必要的话,考虑限制可以打开txt的最大大小 //TODO 提供大文件分割功能 FileInformation newFile = new FileInformation(filePath); // 获得历史记录中所有与当前文件路径相同的记录集 List<FileInformation> existFiles = CommonFunc.Config.GetFileOnlyWithPath(newFile); // 获得已经获得的记录集中与当前文件MD5相同的记录 FileInformation existFile = CommonFunc.CheckFileMD5(newFile, existFiles); if (existFiles.Count > 0) { if (null == existFile) { StringBuilder message = new StringBuilder(); message.AppendLine("正在打开的文件与历史记录中的信息不符,是覆盖历史记录?"); message.AppendLine("如果选择“是”,将覆盖历史记录。"); message.AppendLine("如果选择“否”,将生成一条新记录。"); message.AppendLine("如果选择“取消”,将停止打开文件。"); DialogResult dr = MessageBox.Show(message.ToString(), "文件已改变", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); switch (dr) { case DialogResult.No: CommonFunc.ActiveFile = newFile; break; case DialogResult.Yes: foreach (FileInformation info in existFiles) { info.MD5 = newFile.MD5; } CommonFunc.ActiveFile = existFiles[0]; CommonFunc.ActiveFile.UpdateTime = newFile.UpdateTime; break; default: return; break; } } else { CommonFunc.ActiveFile = existFile; } } else // 新的文件 { if (CommonFunc.ActiveFile != null && File.Exists(filePath) && MessageBox.Show("打开新文件前,是否在当位置作书签?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { CommonFunc.ActiveFile.BookMark = this.rtbCurrent.GetCharIndexFromPosition(this.rtbCurrent.PointToClient(Cursor.Position)); CommonFunc.ActiveFile.UpdateTime = DateTime.Now.ToString(Constants.FORMAT_FILEINFO_UPDATETIME); CommonFunc.Config.AddFile(CommonFunc.ActiveFile); } CommonFunc.ActiveFile = newFile; } CommonFunc.Config.AddFile(CommonFunc.ActiveFile); } catch { MessageBox.Show("打开文件时发生了错误。", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 从现有文件信息记录中删除一条记录 /// </summary> /// <param name="file">要删除的文件信息记录</param> public void RemoveFile(FileInformation file) { this.m_FileInfoList.Remove(file); m_Xml.RemoveNode(Constants.XPATH, file, "MD5"); }
/// <summary> /// 删除指定索引的文件信息对象(索引从0开始) /// </summary> /// <param name="index">索引值(索引从0开始)</param> public void RemoveFile(int index) { FileInformation file = new FileInformation(m_FileInfoList[index]); this.m_FileInfoList.RemoveAt(index); m_Xml.RemoveNode(Constants.XPATH, file, "MD5"); }
/// <summary> /// 获得路径相同的文件信息对象 /// </summary> /// <param name="file">作为标准的当前文件信息对象</param> /// <returns>回相符的信息对象,如果不存在则返回null</returns> public List<FileInformation> GetFileOnlyWithPath(FileInformation file) { List<FileInformation> result = new List<FileInformation>(); for (int i = 0; i < m_FileInfoList.Count; i++) { if (file.Path.Equals(m_FileInfoList[i].Path)) { result.Add(FileInfoList[i]); } } return result; }
/// <summary> /// 获得与当前文件信息相符的信息对象 /// </summary> /// <param name="file">作为标准的当前文件信息对象</param> /// <returns>返回相符的信息对象,如果不存在则返回null</returns> public FileInformation GetFile(FileInformation file) { FileInformation result = null; for (int i = 0; i < m_FileInfoList.Count; i++) { if (file.Path.Equals(m_FileInfoList[i].Path) && file.MD5.Equals(m_FileInfoList[i].MD5)) { result = m_FileInfoList[i]; break; } } return result; }
/// <summary> /// 配置文件中是否存在当前文本文件的信息 /// </summary> /// <param name="file">文件信息对象</param> /// <returns>如果存在返回true,否则返回false</returns> public bool ExistFile(FileInformation file) { bool result = false; result = (null != GetFile(file)); return result; }
/// <summary> /// 配置文件中是否存在当前文本文件的信息 /// </summary> /// <param name="filePath">文件路径</param> /// <returns>如果存在返回true,否则返回false</returns> public bool ExistFile(string filePath) { bool result = false; FileInformation file = new FileInformation(filePath); result = ExistFile(file); return result; }
/// <summary> /// 向配置文件中加入一个新的文件信息记录 /// </summary> /// <param name="file">要添加的文件信息记录</param> public void AddFile(FileInformation file) { FileInformation existFile = GetFile(file); if (null != existFile) { existFile.BookMark = file.BookMark; } else { this.m_FileInfoList.Add(file); } m_Xml.SetAllNodeValues(Constants.XPATH, m_FileInfoList, "MD5"); }