//移动种子文件到子文件夹下 private int MoveTorrentFileToSubDir(string pathname) { DirectoryInfo filedir = new DirectoryInfo(pathname); foreach (DirectoryInfo NextFolder in filedir.GetDirectories()) { if (NextFolder.Name.Length == 1) { foreach (FileInfo fileChild in NextFolder.GetFiles("*.torrent")) { try { Application.DoEvents(); //让系统在百忙之中来响应其他事件 string fc = fileChild.ToString(); int finddot = fc.IndexOf('.'); string hashname = fc.Substring(0, finddot); string pathname1 = Path.Combine(pathname, hashname.Substring(hashname.Length - 2, 2).ToUpper()); if (!Directory.Exists(pathname1)) { Directory.CreateDirectory(pathname1); } string filename1 = Path.Combine(fileChild.DirectoryName, fc); string filename2 = Path.Combine(pathname1, fc); File.Move(filename1, filename2); } catch (Exception ex) { H31Debug.PrintLn(ex.Message); } } } } return(0); }
public static List <FileModel> List(string path) { List <FileModel> res = new List <FileModel>(); DirectoryInfo TheFolder = new DirectoryInfo(path); foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories()) { FileModel model = new FileModel() { Name = NextFolder.Name, CreationTime = NextFolder.CreationTime, LastWriteTime = NextFolder.LastWriteTime, LastAccessTime = NextFolder.LastAccessTime, Length = NextFolder.GetFiles().Length, FileType = "文件夹" }; res.Add(model); } foreach (FileInfo NextFile in TheFolder.GetFiles()) { FileModel model = new FileModel() { Name = NextFile.Name, CreationTime = NextFile.CreationTime, LastWriteTime = NextFile.LastWriteTime, LastAccessTime = NextFile.LastAccessTime, Length = 0, FileType = "文件" }; res.Add(model); } return(res); }
//显示子文件夹及文件 private void button2_Click(object sender, EventArgs e) { //int index = listBox1.SelectedIndex; if (listBox1.SelectedItems.Count != 1) { if (MessageBox.Show("未选中文件夹", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading) == DialogResult.OK) { return; } } else { string str = listBox1.SelectedItem.ToString(); //str =this.textBox1.Text+ '\\' + str; //显示所有子文件夹和文件 listBox1.Items.Clear(); // MessageBox.Show(str); DirectoryInfo theFolder = new DirectoryInfo(@str); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { this.listBox1.Items.Add(NextFolder.Name); FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { this.listBox1.Items.Add(str + '\\' + NextFolder.Name); } } } }
private void button2_Click(object sender, EventArgs e) { String dir = textBox1.Text; DirectoryInfo theFolder = new DirectoryInfo(dir); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); this.listBox1.Items.Clear(); FileInfo[] fileInfo = theFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) { //遍历文件 String PathName = dir + "\\" + NextFile.Name; this.listBox1.Items.Add(PathName); } if (this.checkBox1.Checked == true) { //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { FileInfo[] ChildfileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in ChildfileInfo) { //遍历文件 String PathName = dir + "\\" + NextFolder.Name + "\\" + NextFile.Name; this.listBox1.Items.Add(PathName); } } } }
static void getScenes(DirectoryInfo curr, List <FileInfo> lst) { foreach (DirectoryInfo NextFolder in curr.GetDirectories()) { if (NextFolder.FullName.IndexOf("StreamingAssets") > -1 || NextFolder.FullName.IndexOf("thirdparty") > -1) { continue; } foreach (FileInfo NextFile in NextFolder.GetFiles()) { // string filename = System.IO.Path.GetFileName(fullPath);//文件名 “Default.aspx” string extension = System.IO.Path.GetExtension(NextFile.Name); //扩展名 “.aspx” string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(NextFile.Name); // 没有扩展名的文件名 “Default” if (currentScene == true && fileNameWithoutExtension == System.IO.Path.GetFileNameWithoutExtension(EditorApplication.currentScene) || currentScene == false) { if (extension == ".unity") { // Debug.Log (NextFile.FullName); lst.Add(NextFile); } } } getScenes(NextFolder, lst); } }
/// <summary> /// 检查本地是否有未索引的 /// </summary> public void Check() { string localdir = ConfigValue; //获得本地的文件夹们 DirectoryInfo theFolder = new DirectoryInfo(localdir); if (theFolder.Exists == false) { return; } log.Info("开始检查共享文件夹同步"); foreach (FileInfo NextFile in theFolder.GetFiles()) { //加入检查队列 QShareChk.Enqueue(NextFile.FullName); } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { //空文件夹删之 if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空share文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectoryforreg(NextFolder.FullName); //调用文件夹删除检查 } } flag = false; }
/// <summary> /// 只能对某个文件夹进行遍历 /// </summary> public static void ScanDirectoryInfo() { DirectoryInfo theFolder = new DirectoryInfo(@"D:\temp"); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo)//遍历文件夹 { FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { if (Path.GetExtension(NextFile.ToString()) == ".exe") { temppath.Add(NextFile.FullName); Console.WriteLine(NextFile.FullName); count++; } } } #region MyRegion //for (int i = 0; i < count; i++) //{ // string temp = temppath[i].ToString(); // int len = temp.Length; // finallypath.Add(temppath[i].ToString().Substring(0, len - 4)); // lastpath.Add(finallypath[i] + ".jpg"); //} //for (int i = 0; i < count; i++) //{ // File.Copy(temppath[i].ToString(), lastpath[i].ToString()); // File.Delete(temppath[i].ToString()); //} //MessageBox.Show("转换完成!"); #endregion }
private void processUtorrent() { DirectoryInfo TheFolder = new DirectoryInfo("D:\\VuzeMove"); if (!TheFolder.Exists) { return; } bool hasIncomplete = false; foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories("*", SearchOption.TopDirectoryOnly)) { hasIncomplete = false; FileInfo[] fileInfos = NextFolder.GetFiles("*", SearchOption.AllDirectories); foreach (FileInfo f in fileInfos) { if (f.Extension == ".bt!" || f.Extension == ".!ut" || f.Extension == ".bc!" || f.Extension == ".az!" || f.Extension == ".td" || f.Extension == ".tdl" || f.Extension == ".xltd") { hasIncomplete = true; break; } } if (!hasIncomplete) { moveList.Add(new MyFileInfo(null, NextFolder.FullName)); } } }
///<summary> ///遍历获取文件夹下的文件名 ///<\summary> /// private List <string> getFile(string SourcePath) { List <String> list = new List <string>(); //遍历文件夹 DirectoryInfo theFolder = new DirectoryInfo(SourcePath); FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly); foreach (FileInfo NextFile in thefileInfo) //遍历文件 //list.Add(NextFile.FullName); { list.Add(NextFile.Name); } //遍历子文件夹 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { //list.Add(NextFolder.ToString()); FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories); foreach (FileInfo NextFile in fileInfo) //遍历文件 { list.Add(NextFile.Name); } //list.Add(NextFile.FullName); } return(list); }
/// <summary> /// 递归检查本地文件夹 /// </summary> /// <param name="strdir">文件夹路径</param> private void CheckDirectoryforreg(string strdir) { DirectoryInfo theFolder = new DirectoryInfo(strdir); //遍历文件 foreach (FileInfo NextFile in theFolder.GetFiles()) { //加入检查队列 QShareChk.Enqueue(NextFile.FullName); } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { //空文件夹删之 if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空share文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectoryforreg(NextFolder.FullName); //递归检查 } } }
/// <summary> /// 根据后缀查找文件 /// </summary> public static string FindFileByEndType(string path, string _type, out string _fileName) { _fileName = ""; try { DirectoryInfo theFolder = new DirectoryInfo(path); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { //Debug.Log(NextFile.Name); if (NextFile.Name.EndsWith(_type)) { _fileName = NextFile.Name; return(PathRespace(NextFile.FullName)); } } //递归 return(FindFilePath(theFolder.FullName, _type)); } } catch (System.Exception ex) { Debug.LogError("[ZipUtility.FindFileByEndType]: " + ex.ToString()); //return lst; } return(""); }
static void getPrefabs(DirectoryInfo curr, List <FileInfo> lst) { foreach (string path in refPrefabs) { FileInfo NextFile = new FileInfo(path); lst.Add(NextFile); } return; foreach (DirectoryInfo NextFolder in curr.GetDirectories()) { if (NextFolder.FullName.IndexOf("thirdparty") > -1) { continue; } foreach (FileInfo NextFile in NextFolder.GetFiles()) { // string filename = System.IO.Path.GetFileName(fullPath);//文件名 “Default.aspx” string extension = System.IO.Path.GetExtension(NextFile.Name); //扩展名 “.aspx” // string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(NextFile.Name);// 没有扩展名的文件名 “Default” if (extension == ".prefab") { // Debug.Log (NextFile.FullName); lst.Add(NextFile); } } getPrefabs(NextFolder, lst); } }
/// <summary> /// 检查本地是否有未索引的文件 /// </summary> public void Check() { string localdir = ConfigValue; //获得本地的文件夹们 DirectoryInfo theFolder = new DirectoryInfo(localdir + Host + @"\"); if (theFolder.Exists == false) { return; } log.Info("Ftp文件检查:" + Host + @" " + User); foreach (FileInfo NextFile in theFolder.GetFiles()) { //将文件加入检查队列 QFtpChk.Enqueue(NextFile.FullName); } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { //空文件夹删除 if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空Ftp文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectory(NextFolder.FullName); } } flag = false; }
/// <summary> /// 检查本地文件夹 /// </summary> /// <param name="strdir">文件夹路径</param> private void CheckDirectory(string strdir) { DirectoryInfo theFolder = new DirectoryInfo(strdir); //遍历文件 foreach (FileInfo NextFile in theFolder.GetFiles()) { //将文件加入检查队列 QFtpChk.Enqueue(NextFile.FullName); } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { //若是空文件夹,删除 log.Info("删除空Ftp文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectory(NextFolder.FullName); //递归检查 } } }
public void ListFiles(string sSourcePath) { DirectoryInfo theFolder = new DirectoryInfo(sSourcePath); FileInfo[] thefileInfo = theFolder.GetFiles("*", SearchOption.TopDirectoryOnly); foreach (FileInfo NextFile in thefileInfo) { string fileName = NextFile.FullName.Replace(sSourcePath + @"\", ""); if (!lstModuleAdd.Items.Contains(fileName)) { lstModules.Items.Add(fileName); } } DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { FileInfo[] fileInfo = NextFolder.GetFiles("*", SearchOption.AllDirectories); foreach (FileInfo NextFile in fileInfo) { string fileName = NextFile.FullName.Replace(sSourcePath + @"\", ""); if (!lstModuleAdd.Items.Contains(fileName)) { lstModules.Items.Add(fileName); } } } }
//通过指定的根目录返回文件列表 public List <MyFile> GetFilesByPath(string path) { List <MyFile> myfile = new List <MyFile>(); DirectoryInfo theFolder = new DirectoryInfo(path); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { // this.listBox1.Items.Add(NextFolder.Name); FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { MyFile fi = new MyFile(); fi.Name = NextFile.Name; fi.Path = path + "\\" + NextFile.Name + ".txt"; fi.Content = ""; if (File.Exists(fi.Path)) { List <string[]> list = new List <string[]>(); // 打开文件时 一定要注意编码 也许你的那个文件并不是GBK编码的 using (StreamReader sr = new StreamReader(fi.Path, System.Text.Encoding.GetEncoding("GBK"))) { while (!sr.EndOfStream) //读到结尾退出 { fi.Content = fi.Content + sr.ReadLine(); } } } myfile.Add(fi); } } return(myfile); }
/// <summary> /// 递归获取文件夹下的所有文件 /// </summary> /// <param name="filePath">文件路径</param> public static List <string> GetDirectoryFiles(string filePath) { if (!Directory.Exists(filePath)) { return(null); } List <String> list = new List <string>(); //遍历文件夹 DirectoryInfo theFolder = new DirectoryInfo(filePath); FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly); foreach (FileInfo NextFile in thefileInfo) //遍历文件 { list.Add(NextFile.FullName); } //遍历子文件夹 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories); foreach (FileInfo NextFile in fileInfo) //遍历文件 { list.Add(NextFile.FullName); } } return(list); }
private void process(string folderPath) { DirectoryInfo TheFolder = new DirectoryInfo(folderPath); if (TheFolder.Exists) { foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories("*", SearchOption.TopDirectoryOnly)) { try { int torrentNumber = 0; bool hasIncomplete = false; bool hasBigFile = false; FileInfo[] fileInfos = NextFolder.GetFiles("*", SearchOption.AllDirectories); double folderLength = 0; foreach (FileInfo f in fileInfos) { if (f.Length / 1024 / 1024 > 60 && (f.Extension == ".bt!" || f.Extension == ".!ut" || f.Extension == ".bc!" || f.Extension == ".az!" || f.Extension == ".td" || f.Extension == ".tdl" || f.Extension == ".xltd")) { hasIncomplete = true; continue; } else if (f.Length / 1024 / 1024 > 35) { hasBigFile = true; } if (f.Extension.ToLower() == ".torrent") { torrentNumber++; } folderLength += f.Length / 1024 / 1024; } if (!hasIncomplete && hasBigFile) { if (torrentNumber > 1) { moveList1.Add(new MyFileInfo(null, NextFolder.FullName)); } else { moveList.Add(new MyFileInfo(null, NextFolder.FullName)); } totalLength += folderLength; } } catch (Exception e) { MessageBox.Show(e.Message); } } foreach (FileInfo f in TheFolder.GetFiles("*", SearchOption.TopDirectoryOnly)) { if (f.Length / 1024 / 1024 > 60 && (f.Extension != ".bt!" && f.Extension != ".!ut" && f.Extension != ".bc!" && f.Extension != ".az!" && f.Extension != ".td" && f.Extension != ".tdl" && f.Extension != ".xltd")) { singleFileList.Add(f.FullName); } } } }
/// <summary> /// 获取播放文件 /// </summary> /// <param name="filepath"></param> private void GetSelFileName(string filepath, string starttime, string endtime) { try { dicVideoInfo.Clear(); DirectoryInfo theFolder = new DirectoryInfo(filepath);// DirectoryInfo 公开用于创建、移动和枚举目录和子目录的实例方法。 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 long curfile = 0; long start = 0; long end = 0; foreach (DirectoryInfo NextFolder in dirInfo) {//文件夹只有年月日 没有时分秒 string folderName = NextFolder.Name; if (PageValidate.IsNumber(folderName)) { curfile = long.Parse(folderName); } else { continue; } start = long.Parse(starttime.Substring(0, 8)); end = long.Parse(endtime.Substring(0, 8)); if (curfile >= start && curfile <= end) { FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { //文件名有年月日 时分秒 string filename = NextFile.Name; string[] splitfilename = filename.Split('.'); if (splitfilename.Length == 2) { if (PageValidate.IsNumber(splitfilename[0])) { curfile = long.Parse(splitfilename[0]); start = long.Parse(starttime); end = long.Parse(endtime); if (curfile >= start && curfile <= end) { if (!dicVideoInfo.ContainsKey(NextFile.Name)) { dicVideoInfo.Add(NextFile.Name, filepath + NextFolder.Name + "\\" + NextFile.Name); } } } } } } } } catch (Exception ex) { if (frmMain.blDebug) { MessageUtil.ShowTips(ex.Message); } } }
/// <summary> /// 遍历文件夹所有目录和子目录(需要配置返回文件类型 例: <add key="QueryRailPassword" value="*.MP3|*.JPG|*.GIF|*.PNG|*.PDF"></add>) /// </summary> /// <param name="sSourcePath">源目录</param> /// <returns>返回路径的List_string</returns> public List <string> FindFile2(string sSourcePath) { Decrypt_out decrypt_out = new Helper.Decrypt.Decrypt_out(); string imgtype = decrypt_out.Decrypt_out_Str("imgtype"); //string imgtype = "*.MP3|*.JPG|*.GIF|*.PNG|*.PDF"; string[] ImageType = imgtype.Split('|'); ///_______________________________________________________________________ List <String> list = new List <string>(); //遍历文件夹 DirectoryInfo theFolder = new DirectoryInfo(sSourcePath); FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly); foreach (FileInfo NextFile in thefileInfo) //遍历文件 { for (int i = 0; i < ImageType.Length; i++) { if (NextFile.Extension == ImageType[i]) { list.Add(NextFile.FullName); continue; } } } //遍历子文件夹 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { //list.Add(NextFolder.ToString()); FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories); foreach (FileInfo NextFile in fileInfo) //遍历文件 { for (int i = 0; i < ImageType.Length; i++) { if (NextFile.Extension == ImageType[i] && NextFile.CreationTime.Date == DateTime.Now.Date) { list.Add(NextFile.FullName); continue; } } } } return(list); }
private void Form1_Load(object sender, EventArgs e) { ////实例化TreeNode类 TreeNode(string text,int imageIndex,int selectImageIndex) // TreeNode rootNode = new TreeNode("我的电脑", // IconIndexes.MyComputer, IconIndexes.MyComputer); //载入显示 选择显示 // rootNode.Tag = "我的电脑"; //树节点数据 // rootNode.Text = "我的电脑"; //树节点标签内容 // this.tvMenu.Nodes.Add(rootNode); //树中添加根目录 // //显示MyDocuments(我的文档)结点 // var myDocuments = Environment.GetFolderPath //获取计算机我的文档文件夹 // (Environment.SpecialFolder.MyDocuments); // TreeNode DocNode = new TreeNode(myDocuments); // DocNode.Tag = "我的文档"; //设置结点名称 // DocNode.Text = "我的文档"; // DocNode.ImageIndex = IconIndexes.MyDocuments; //设置获取结点显示图片 // DocNode.SelectedImageIndex = IconIndexes.MyDocuments; //设置选择显示图片 // rootNode.Nodes.Add(DocNode); //rootNode目录下加载节点 // DocNode.Nodes.Add(""); // //循环遍历计算机所有逻辑驱动器名称(盘符) // foreach (string drive in Environment.GetLogicalDrives()) // { // //实例化DriveInfo对象 命名空间System.IO // var dir = new DriveInfo(drive); // switch (dir.DriveType) //判断驱动器类型 // { // case DriveType.Fixed: //仅取固定磁盘盘符 Removable-U盘 // { // //Split仅获取盘符字母 // TreeNode tNode = new TreeNode(dir.Name.Split(':')[0]); // tNode.Name = dir.Name; // tNode.Tag = tNode.Name; // tNode.ImageIndex = IconIndexes.FixedDrive; //获取结点显示图片 // tNode.SelectedImageIndex = IconIndexes.FixedDrive; //选择显示图片 // tvMenu.Nodes.Add(tNode); //加载驱动节点 // tNode.Nodes.Add(""); // } // break; // } // } // rootNode.Expand(); DirectoryInfo theFolder = new DirectoryInfo(@"E:\"); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { // this.listBox1.Items.Add(NextFolder.Name); FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件,获取文件名 { this.tvMenu.Nodes.Add(NextFile.Name); } } }
public List <string> FindFile2(string sSourcePath) { List <String> list = new List <string>(); //遍历文件夹 DirectoryInfo theFolder = new DirectoryInfo(sSourcePath); FileInfo[] thefileInfo = theFolder.GetFiles("*.exe*", SearchOption.TopDirectoryOnly); foreach (FileInfo NextFile in thefileInfo) //遍历文件 { list.Add(NextFile.FullName); } //遍历子文件夹 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); int i = 1; foreach (DirectoryInfo NextFolder in dirInfo) { //list.Add(NextFolder.ToString()); FileInfo[] fileInfo = NextFolder.GetFiles("*.exe*", SearchOption.AllDirectories); TreeNode tn = tree_file.Nodes.Add(NextFolder.Name); tn.ImageIndex = i; tn.Checked = true; foreach (FileInfo NextFile in fileInfo) //遍历文件 { if (i == 9) { if (NextFile.Name.Contains("SAP")) { list.Add(NextFile.FullName); TreeNode cn = new TreeNode(); cn.Text = NextFile.Name; cn.Tag = NextFile.FullName; cn.Checked = true; cn.ImageIndex = 0; tn.Nodes.Add(cn); } } else { list.Add(NextFile.FullName); TreeNode cn = new TreeNode(); cn.Text = NextFile.Name; cn.Tag = NextFile.FullName; cn.Checked = true; cn.ImageIndex = -1; tn.Nodes.Add(cn); } } i++; } tree_file.ExpandAll(); return(list); }
/// <summary> /// 检查文件夹是否含有要被删的 /// </summary> /// <param name="strdir">文件夹本地路径</param> /// <param name="session">远程Session</param> /// <param name="Host">Ftp地址</param> private void CheckDirectory(string strdir, Session session, string Host) { DirectoryInfo theFolder = new DirectoryInfo(strdir); string localdir = ConfigValue; //获得本地的文件夹们 //遍历文件 foreach (FileInfo NextFile in theFolder.GetFiles()) { string name, remote, finalstr; string filexten = Path.GetExtension(NextFile.Name); //扩展名检查 if (filexten == ".rntf") { name = NextFile.FullName; remote = name.Replace(localdir + Host, ""); finalstr = remote.Replace(@"\", @"/"); finalstr = finalstr.Replace(".rntf", ""); } else { name = NextFile.FullName; remote = name.Replace(localdir + Host, ""); finalstr = remote.Replace(@"\", @"/"); } //统一文件名和路径名,到远程服务器检测是否存在 if (session.FileExists(finalstr)) { continue; //存在就不用管 } else { //不存在删除本地文件 File.Delete(name); log.Info("删除Ftp文件" + name); QFtpDel.Enqueue(name); } } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { //若文件夹已经是空文件夹,删除该文件夹 if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空Ftp文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectory(NextFolder.FullName, session, Host); //递归检查 } } }
public static List <string> movePic(string path) { List <string> picList = new List <string>(); ArrayList moveList = new ArrayList(); DirectoryInfo TheFolder = new DirectoryInfo(path); bool isPicFolder; foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories("*", SearchOption.TopDirectoryOnly)) { isPicFolder = true; FileInfo[] fileInfos = NextFolder.GetFiles("*", SearchOption.AllDirectories); foreach (FileInfo fileInfo in fileInfos) { if (fileInfo.Length / 1024 / 2024 > 25) { isPicFolder = false; break; } } if (isPicFolder) { moveList.Add(NextFolder); } } foreach (DirectoryInfo info in moveList) { string dateDirectory = path.Split(new string[] { "\\" }, StringSplitOptions.None)[2]; string indexDirectory = path.Split(new string[] { "\\" }, StringSplitOptions.None)[1]; string newPath = Path.Combine(Path.Combine(Path.GetPathRoot(path), "pic" + indexDirectory), dateDirectory).ToString(); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } newPath = Path.Combine(newPath, info.ToString()); picList.Add(newPath); while (true) { try { Directory.Move(info.FullName, newPath); break; } catch (Exception e) { newPath += "1"; } } } return(picList); }
public List <string> FindFile2(string sSourcePath) { int i = 0; this.Cursor = Cursors.WaitCursor; DataTable dt1 = new DataTable(); dt1.Columns.Add("lujing1"); DataTable dt2 = new DataTable(); dt2.Columns.Add("lujing2"); List <String> list = new List <string>(); //遍历文件夹 DirectoryInfo theFolder = new DirectoryInfo(sSourcePath); FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly); foreach (FileInfo NextFile in thefileInfo) //遍历文件 { list.Add(NextFile.FullName); } //遍历子文件夹 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { //list.Add(NextFolder.ToString()); FileInfo[] fileInfo = NextFolder.GetFiles("*.jpg", SearchOption.AllDirectories); foreach (FileInfo NextFile in fileInfo) //遍历文件 //list.Add(NextFile.FullName); { //listBox1.Items.Add(NextFile.FullName); dt1.Rows.Add(""); dataGridView1.DataSource = dt1; dataGridView1.Rows[i].Cells[0].Value = NextFile.Directory; dt2.Rows.Add(""); dataGridView2.DataSource = dt2; dataGridView2.Rows[i].Cells[0].Value = NextFile.FullName; i++; } } dataGridView1.Columns[0].Width = 700; dataGridView2.Columns[0].Width = 700; return(list); }
/// <summary> /// 循环将图片放到各期目录 /// </summary> /// <param name="dir"></param> private void CopyAllImageToMonthlyFolder(string dir) { DirectoryInfo theFolder = new DirectoryInfo(dir); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); foreach (DirectoryInfo NextFolder in dirInfo) { FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) { File.Copy(NextFile.FullName, dir + NextFile.Name, true); } } foreach (DirectoryInfo NextFolder in dirInfo) { Directory.Delete(NextFolder.FullName, true); } }
private void DeleteXLS() { while (!isClose) { try { string path = Server.MapPath("~/"); DirectoryInfo TheFolder = new DirectoryInfo(path); foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories()) { if (NextFolder.FullName == (path + "DataMonitor") || NextFolder.FullName == (path + "DataQuery") || NextFolder.FullName == (path + "DataReport") || NextFolder.FullName == (path + "GlobalView") || NextFolder.FullName == (path + "SystemAdmin") || NextFolder.FullName == (path + "WaterRight") || NextFolder.FullName == (path + "WebAdmin")) { FileInfo[] files = NextFolder.GetFiles(); foreach (FileInfo file in files) { if (file.Extension == ".xls") { string fn = file.Directory.Name + "/" + file.Name; try { file.Delete(); myLogger.Info("删除文件:" + fn + ",成功!"); } catch { myLogger.Info("删除文件:" + fn + ",失败!"); } } } } } } catch { } //间隔2小时 Thread.Sleep(2 * 60 * 60 * 1000); } }
public static void GetFileNames(string path) { DirectoryInfo di = new DirectoryInfo(path); //遍历文件 当前path下的文件 //foreach (FileInfo NextFile in di.GetFiles()) //{ // fileInfos.Add(NextFile); //} //遍历文件夹 foreach (DirectoryInfo NextFolder in di.GetDirectories()) { foreach (FileInfo NextFile in NextFolder.GetFiles()) { fileInfos.Add(NextFile); } GetFileNames(NextFolder.FullName); } }
private string RandomPic() { List <string> filenames = new List <string>(); DirectoryInfo TheFolder = new DirectoryInfo(ConfigurationManager.AppSettings["randompicpath"].ToString()); FileInfo[] files = TheFolder.GetFiles(); DirectoryInfo[] dirInfo = TheFolder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { filenames.Add("img\\1\\" + NextFolder.Name + "\\" + NextFile.Name); } } Random r = new Random(); return(filenames.ElementAt(r.Next(filenames.Count))); }
//刷新按钮的时间 //重新刷新播放器目录 private void button13_Click(object sender, EventArgs e) { //利用指定位置的文件夹创建实例 // 文件夹是怎么创建的 DirectoryInfo theFolder = new DirectoryInfo(@"C:\surveillance video"); //返回当前目录下的所有目录 DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { //遍历其中的每一个文件夹 // this.listBox1.Items.Add(NextFolder.Name); //获得子目录中的文件 FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 { this.listBox3.Items.Add(@"C:\surveillance video" + "\\" + NextFolder.Name + "\\" + NextFile.Name); } } }