创建目录及子目录--记一次文件整理
昨天晚上F盘只剩下400M的空间了,好多软件只好在D,E盘胡乱存放。于是就有了再次整理下F盘文件的想法,正好昨天园子里朋友推荐了一款XMind的软件,可以现学现用。第二天早上7点钟起来开始,首先画了个图:
我想为每个具体的技术创建5个文件夹。这样一来创建文件夹成了一个重复性的工作。为了偷懒(结果证明花的时间更多了),就想用程序批量生成这个5个文件夹,我只需要输入对应的名字就OK了。想法很好,结果花了2个多小时才完成编码。
其实目录及子目录的创建非常简单,MSDN上有现成的代码可供使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; using System.IO; public class CreateSubTest { public static void Main() { // Create a reference to a directory. DirectoryInfo di = new DirectoryInfo( "TempDir" ); // Create the directory only if it does not already exist. if (di.Exists == false ) di.Create(); // Create a subdirectory in the directory just created. DirectoryInfo dis = di.CreateSubdirectory( "SubDir" ); // Process that directory as required. // ... // Delete the subdirectory. dis.Delete( true ); // Delete the directory. di.Delete( true ); } } |
已这个为基础,我开始进行设计。
碰到的问题一:
我想用“F:\WorkSpace\skill"为基础,直接分析出需要创建的目录和路径。用正则匹配出最后一个'\'是最简单的方法.
1 2 | Regex reg = new Regex( "\\" ); Match theMatch = reg.Match(args[0]); |
但是C#编译期始终报错---” System.ArgumentException: 正在分析“\”- \ 在模式末尾非法。“
本来想就这么算了,反正做完了,但是还是让我不爽,又继续找。这次把搜索定在文件路径的匹配上,我就不相信没人碰到过。
果然还是有的:http://hi.baidu.com/lindily/blog/item/c140a16e5e6f29d380cb4a2e.html
改正这样就可以了:
1 2 3 | Regex reg = new Regex( "\\\\" ,RegexOptions.RightToLeft); Match theMatch = reg.Match(args[0]); Console.WriteLine(theMatch.Index); |
后来想用Split方法来获取:string[] s = args[0].Split('\\'); 这样就可以了。
最后最后,我发现犯了个错误。。目录没有建立的时候,哪里来的F:\WorkSpace\skill?只需要F:\WorkSpace就可以了嘛,然后再根据自己需要输入需要创建的目录就可以了。于是这个问题化简了。
碰到的问题二:
1 2 | for ( int i=0;i<SubDirectoryNames.Length;i++) DirectoryInfo dis = di.CreateSubdirectory(SubDirectoryNames[i]); |
因为我是用文本在写,所以它这里报错我找一阵才找到。“嵌入的语句不能是声明或标记语句”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | using System; using System.IO; /* 小工具,为我自动创建文件夹及其子文件夹 books,tools,project,documnets,example */ public class CreateMyDirectoryList { static long directories; static string [] SubDirectoryNames={ "books" , "bools" , "project" , "documents" , "example" }; public static void Main() { Console.WriteLine( "please intput directory path..." ); string directoryPath = Console.ReadLine(); Console.WriteLine( "please intput directory name to create..." ); string directoryName= Console.ReadLine(); string directory = directoryPath+ "\\" +directoryName; DirectoryInfo di = new DirectoryInfo(directory); try { // Determine whether the directory exists. if (di.Exists) { // Indicate that it already exists. Console.WriteLine( "That path exists already." ); return ; } // Try to create the directory. di.Create(); // Create need subdirectories in the directory just created. DirectoryInfo dis; for ( int i=0;i<SubDirectoryNames.Length;i++) dis = di.CreateSubdirectory(SubDirectoryNames[i]); Console.WriteLine( "The directory was created successfully." ); } catch (Exception e) { Console.WriteLine( "The process failed: {0}" , e.ToString()); } finally { } } } |
over~~~以后再改进功能~
作 者:doku
出 处:http://www.cnblogs.com/kulong995/
关于作者:喜欢编程,喜欢美食,专注于.NET项目开发。
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?