/// <summary>判断文件名输入控件中的文件名与路径是否存在</summary> /// <param name="fileInfoBox">文件名输入控件</param> /// <param name="fileInfo">[out]输出参数,输出文件信息。文件不存在时输出null</param> /// <returns>文件名与路径是否存在</returns> private bool FileExist(FileInfoBox fileInfoBox, out FileInfo fileInfo) { fileInfo = null; if (fileInfoBox == null) { return(false); } string foldPath = fileInfoBox.FilePath; if (foldPath.EndsWith("\\")) { foldPath.Substring(0, foldPath.Length - 1); } if (!Directory.Exists(foldPath)) { return(false); } foldPath += "\\"; string fullPath = foldPath + fileInfoBox.FileName; if (File.Exists(fullPath)) { fileInfo = new FileInfo(fullPath); return(true); } return(false); }
/// <summary>响应自定义控件的选择文件事件</summary> /// <param name="sender"></param> /// <param name="e"></param> private void SelectPath(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = @"所有文件|*.*"; dlg.RestoreDirectory = true; dlg.FilterIndex = 1; dlg.CheckPathExists = true; dlg.Multiselect = false; if (dlg.ShowDialog() == DialogResult.OK) { string fileName = dlg.SafeFileName; string foldPath = dlg.FileName.Substring(0, dlg.FileName.LastIndexOf('\\')); Button btnSelectPath = sender as Button; System.Diagnostics.Debug.Assert(btnSelectPath != null); FileInfoBox fileInfoBox = btnSelectPath.Parent as FileInfoBox; System.Diagnostics.Debug.Assert(fileInfoBox != null); fileInfoBox.FileName = fileName; fileInfoBox.FilePath = foldPath; if (string.IsNullOrWhiteSpace(FileSetName)) { FileSetName = fileName; } } }
/// <summary>添加输入行</summary> /// <param name="fullPath">文件完整路径</param> private void AddBox(string fullPath = null) { if (_countBox >= 10) { return; } Height += _heightBox + HeightDiv; int top = TopFirstBox + _countBox * (_heightBox + HeightDiv) + HeightDiv; var fileInfoBox = new FileInfoBox(++_countBox); fileInfoBox.Left = LeftBox; fileInfoBox.Top = top; if (fullPath != null && fullPath.Contains('\\')) { fileInfoBox.FullPath = fullPath; } fileInfoBox.ButtonSelectPathClick += SelectPath; fileInfoBox.ButtonAddClick += AddBox; fileInfoBox.ButtonDeleteClick += DeleteBox; _fileInfoBoxs[_countBox - 1] = fileInfoBox; Controls.Add(fileInfoBox); AdjustFrmHeight(); Invalidate(); }
/// <summary>响应自定义控件的删除行事件</summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteBox(object sender, EventArgs e) { Button btnDelete = sender as Button; System.Diagnostics.Debug.Assert(btnDelete != null); FileInfoBox fileInfoBox = btnDelete.Parent as FileInfoBox; System.Diagnostics.Debug.Assert(fileInfoBox != null); DeleteBox(fileInfoBox.Num); }
/// <summary>检查用户输入项</summary> /// <returns>输入是否合格</returns> private bool CheckUserInput() { if (string.IsNullOrWhiteSpace(FileSetName)) { MessageBox.Show(@"请输入文件集名称!"); return(false); } int validLines = _countBox; List <string> list = new List <string>(_countBox); for (int i = 0; i < _countBox; i++) { FileInfoBox fileInfoBox = _fileInfoBoxs[i]; if (string.IsNullOrWhiteSpace(FileSetName) || string.IsNullOrWhiteSpace(FileSetName)) { validLines--; continue; } FileInfo fileInfo; if (!FileExist(fileInfoBox, out fileInfo) || fileInfo == null) { validLines--; continue; } if (list.Contains(fileInfo.FullName)) { validLines--; fileInfoBox.Clear(); } else { //移除只读属性 if (File.GetAttributes(fileInfoBox.FullPath).ToString().IndexOf("ReadOnly", StringComparison.Ordinal) != -1) { File.SetAttributes(fileInfoBox.FullPath, FileAttributes.Normal); } //加入可同步列表 list.Add(fileInfo.FullName); } } if (validLines < 2) { MessageBox.Show(@"请至少输入两个完整的待同步文件信息!"); return(false); } return(true); }
/// <summary>得到用户输入的文件信息列表</summary> private void GetFileInfoList() { if (ListFileInfo == null) { ListFileInfo = new List <FileInfoLite>(MaxBoxCount); } ListFileInfo.Clear(); for (int i = 0; i < _countBox; i++) { FileInfoBox fileInfoBox = _fileInfoBoxs[i]; FileInfo fileInfo; if (FileExist(fileInfoBox, out fileInfo) && fileInfo != null) { ListFileInfo.Add(new FileInfoLite(fileInfo)); } } }
/// <summary>窗体初始化</summary> /// <param name="fileSetName"></param> private void FrmInti(string fileSetName) { if (fileSetName == null) { fileSetName = string.Empty; } FileSetName = fileSetName; Screen currentScreen = Screen.FromControl(this); _screenHeight = currentScreen.Bounds.Height; var fileInfoBox = new FileInfoBox(0); _widthBox = fileInfoBox.Width; _heightBox = fileInfoBox.Height; Width = _widthBox + 2 * LeftBox + Padding.Left + Padding.Right; _fileInfoBoxs = new FileInfoBox[MaxBoxCount]; }