/// <summary> /// 列出目录的文件路径或者目录 /// </summary> /// <param name="filePath">指定文件路径</param> /// <param name="isDir">是否列出目录,默认为false,返回文件列表</param> /// <returns></returns> public List <string> GetFileOrDirList(string filePath, bool isDir = false) { List <string> list = new List <string>(); try { var array = isDir ? DirectoryUtil.GetDirectories(filePath) : DirectoryUtil.GetFileNames(filePath); if (array != null) { list.AddRange(array); } } catch (Exception ex) { LogTextHelper.Error("GetFileOrDirList列出目录的文件路径出错:" + ex.Message); } return(list); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void simpleButton2_Click(object sender, EventArgs e) { string path = FileDialogHelper.OpenDir(); if (!string.IsNullOrEmpty(path)) { string[] dirs = DirectoryUtil.GetDirectories(path, "*", true); // 判断是否是子目录 foreach (var dir in dirs) { if (dir.Length > 250) { MessageDxUtil.ShowTips("目录超过限制长度[" + dir + "]"); continue; } if (DirectoryUtil.IsEmptyDirectory(dir)) { string[] tmp = dir.Split('_'); try { if ((dir + "\\" + tmp[tmp.Length - 1] + ".txt").Length > 250) { FileUtil.WriteText(dir + "\\1.txt", dir, Encoding.UTF8); } else { FileUtil.WriteText(dir + "\\" + tmp[tmp.Length - 1] + ".txt", dir, Encoding.UTF8); } } catch (Exception ex) { MessageDxUtil.ShowTips(ex.Message); } } } } MessageDxUtil.ShowTips("操作完成"); }
private static bool CheckPermissions() { String user = String.Format(@"{0}\{1}", Environment.UserDomainName, Environment.UserName); AuthorizationRuleCollection collection = Directory.GetAccessControl(Path.Combine(ConstantUtil.ApplicationExecutionPath(), "DtPad.exe")).GetAccessRules(true, true, typeof(NTAccount)); if (collection.Cast <FileSystemAccessRule>().Any(rule => rule.IdentityReference.Value.ToLower() == user.ToLower() && rule.AccessControlType == AccessControlType.Allow && rule.FileSystemRights == FileSystemRights.FullControl)) { return(true); } //foreach (FileSystemAccessRule rule in collection) //{ // if (rule.IdentityReference.Value.ToLower() == user.ToLower() && rule.AccessControlType == AccessControlType.Allow && rule.FileSystemRights == FileSystemRights.FullControl) // { // return true; // } //} if (!SystemUtil.IsUserAdministrator()) { WindowManager.ShowAlertBox(null, "DtPad need to be executed (only once!) as Administrator to set permissions." + Environment.NewLine + Environment.NewLine + "Program will now close. Please, restart it as Administrator."); return(false); } DirectoryUtil.SetDirPermissions(ConstantUtil.ApplicationExecutionPath()); foreach (String directory in DirectoryUtil.GetDirectories(ConstantUtil.ApplicationExecutionPath(), "*", SearchOption.AllDirectories)) { DirectoryUtil.SetDirPermissions(directory); } foreach (String file in FileUtil.GetFiles(ConstantUtil.ApplicationExecutionPath(), "*.*", SearchOption.AllDirectories)) { FileUtil.SetFilePermissions(file); } return(true); }
private void simpleButton4_Click(object sender, EventArgs e) { string path = FileDialogHelper.OpenDir(); string recordFileName = "\\" + "缺少文件.txt"; if (FileUtil.FileIsExist(path + recordFileName)) { FileUtil.DeleteFile(path + recordFileName); } FileUtil.CreateFile(path + recordFileName); if (!string.IsNullOrEmpty(path)) { string[] dirs = DirectoryUtil.GetDirectories(path); Dictionary <string, bool> a = new Dictionary <string, bool>(); // 判断是否是子目录 foreach (var d in dirs) { string[] subdirs = DirectoryUtil.GetDirectories(d, "*", true); string[] tmpdsname = d.Split('\\'); string dsname = tmpdsname[tmpdsname.Length - 1].Split('$')[0]; foreach (var dir in subdirs) { string[] tmpdirsname = dir.Split('\\'); string dirname = tmpdirsname[tmpdirsname.Length - 1].Split('$')[0]; a[dsname + dirname] = false; if (dirname.Substring(0, 1) == "第" && dirname.Substring(dirname.Length - 1, 1) == "章") { continue; } Int32 readCount = 0; foreach (var dir2 in subdirs) { string[] tmpdir2sname = dir2.Split('\\'); string dir2name = tmpdir2sname[tmpdir2sname.Length - 1].Split('$')[0]; if (dir2name != dirname && dirname.Contains(dir2name) && dirname.IndexOf(dir2name) == 0) { a[dsname + dirname] = true; break; } if (dir2name != dirname && dir2name.Contains(dirname) && dir2name.IndexOf(dirname) == 0) { a[dsname + dirname] = false; break; } // 已经遍历玩了,则还没有找到就是没有子目录 readCount++; if (subdirs.Length == readCount) { a[dsname + dirname] = true; break; } } } } foreach (var d in dirs) { string[] subdirs = DirectoryUtil.GetDirectories(d, "*", true); string[] tmpdsname = d.Split('\\'); string dsname = tmpdsname[tmpdsname.Length - 1].Split('$')[0]; foreach (var dir in subdirs) { string[] tmpdirsname = dir.Split('\\'); string dirname = tmpdirsname[tmpdirsname.Length - 1].Split('$')[0]; if (dirname.Substring(0, 1) == "第" && dirname.Substring(dirname.Length - 1, 1) == "章") { continue; } // 是子目录 if (a[dsname + dirname]) { if (dir.Length > 250) { MessageDxUtil.ShowTips("目录超过限制长度[" + dir + "]"); continue; } if (DirectoryUtil.IsEmptyDirectory(dir)) { FileUtil.AppendText(path + recordFileName, string.Format("{0}\r\n", dir), Encoding.UTF8); } } } } MessageDxUtil.ShowTips("操作完成"); } }
private void simpleButton5_Click(object sender, EventArgs e) { string copyToPath = txtCopytoPath.Text.Trim(); string sourcePath = txtSourcePath.Text.Trim(); if (string.IsNullOrEmpty(copyToPath)) { MessageDxUtil.ShowError("复制到对应文件夹未配置"); return; } if (string.IsNullOrEmpty(sourcePath)) { MessageDxUtil.ShowError("拷贝源文件夹未配置"); return; } // 读取文件夹保存到字典 字段值为 第几部分_索引名字,拷贝全路径 Dictionary <string, string> dic = new Dictionary <string, string>(); string[] allDirs = DirectoryUtil.GetDirectories(copyToPath, "*", true); foreach (var dir in allDirs) { // 如果路径超长了 就不往下走了 if (dir.Length > 245) { continue; } DirectoryInfo dirinfo = new DirectoryInfo(dir); // 没找到 if (dirinfo.Name.Split('$').Length <= 1) { continue; } // 合法路径 if (dirinfo.Parent.Name.Split('$')[0].Contains("部分") && dirinfo.Parent.Name.Split('$')[0].Substring(0, 1) == "第") { dic[dirinfo.Parent.Name.Split('$')[0] + "_" + dirinfo.Name.Split('$')[1]] = dir; } } Int32 copyCount = 0; string[] allFiles = DirectoryUtil.GetAllFileNames(sourcePath); foreach (var file in allFiles) { FileInfo fileInfo = new FileInfo(file); // 没找到 if (fileInfo.Name.Split(' ').Length <= 1) { continue; } string indexStr = fileInfo.Name.Split(' ')[1]; string pattern = @"第\w+部分"; Regex regex = new Regex(pattern); if (regex.IsMatch(fileInfo.DirectoryName)) { if (dic.ContainsKey(regex.Match(fileInfo.DirectoryName).Value + "_" + fileInfo.Name.Split(' ')[1].Replace(fileInfo.Extension, ""))) { copyCount++; File.Move(fileInfo.FullName, dic[regex.Match(fileInfo.DirectoryName).Value + "_" + fileInfo.Name.Split(' ')[1].Replace(fileInfo.Extension, "")] + "\\" + fileInfo.Name); } } else { continue; } } MessageDxUtil.ShowTips(string.Format("操作成功, 目录下一共{0}个文件,成功拷贝{1}个文件", allFiles.Length, copyCount)); }
private void simpleButton1_Click(object sender, System.EventArgs e) { string path = FileDialogHelper.OpenDir(); if (!string.IsNullOrEmpty(path)) { string[] dirs = DirectoryUtil.GetDirectories(path); Dictionary <string, bool> a = new Dictionary <string, bool>(); // 判断是否是子目录 foreach (var d in dirs) { string[] subdirs = DirectoryUtil.GetDirectories(d, "*", true); string[] tmpdsname = d.Split('\\'); string dsname = tmpdsname[tmpdsname.Length - 1].Split('$')[0]; foreach (var dir in subdirs) { string[] tmpdirsname = dir.Split('\\'); string dirname = tmpdirsname[tmpdirsname.Length - 1].Split('$')[0]; a[dsname + dirname] = false; if (dirname.Substring(0, 1) == "第" && dirname.Substring(dirname.Length - 1, 1) == "章") { continue; } Int32 readCount = 0; foreach (var dir2 in subdirs) { string[] tmpdir2sname = dir2.Split('\\'); string dir2name = tmpdir2sname[tmpdir2sname.Length - 1].Split('$')[0]; if (dir2name != dirname && dirname.Contains(dir2name) && dirname.IndexOf(dir2name) == 0) { a[dsname + dirname] = true; break; } if (dir2name != dirname && dir2name.Contains(dirname) && dir2name.IndexOf(dirname) == 0) { a[dsname + dirname] = false; break; } // 已经遍历玩了,则还没有找到就是没有子目录 readCount++; if (subdirs.Length == readCount) { a[dsname + dirname] = true; break; } } } } foreach (var d in dirs) { string[] subdirs = DirectoryUtil.GetDirectories(d, "*", true); string[] tmpdsname = d.Split('\\'); string dsname = tmpdsname[tmpdsname.Length - 1].Split('$')[0]; foreach (var dir in subdirs) { string[] tmpdirsname = dir.Split('\\'); string dirname = tmpdirsname[tmpdirsname.Length - 1].Split('$')[0]; if (dirname.Substring(0, 1) == "第" && dirname.Substring(dirname.Length - 1, 1) == "章") { continue; } // 是子目录 if (a[dsname + dirname]) { if (dir.Length > 250) { MessageDxUtil.ShowTips("目录超过限制长度[" + dir + "]"); continue; } if ((dir + "\\" + tmpdirsname[tmpdirsname.Length - 1].Split('$')[1] + ".txt").Length > 250) { FileUtil.WriteText(dir + "\\1.txt", tmpdirsname[tmpdirsname.Length - 1].Split('$')[1], Encoding.UTF8); } else { FileUtil.WriteText(dir + "\\" + tmpdirsname[tmpdirsname.Length - 1].Split('$')[1] + ".txt", tmpdirsname[tmpdirsname.Length - 1].Split('$')[1], Encoding.UTF8); } } } } MessageDxUtil.ShowTips("操作完成"); } }