创建目录及子目录--记一次文件整理

本文介绍了一个使用C#编写的简易工具,该工具能够帮助用户快速在一个指定目录下创建多个子文件夹,包括书籍、工具、项目等五个类别。作者分享了在实现过程中遇到的一些常见问题及解决方法,如通过正则表达式正确处理路径字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    昨天晚上F盘只剩下400M的空间了,好多软件只好在D,E盘胡乱存放。于是就有了再次整理下F盘文件的想法,正好昨天园子里朋友推荐了一款XMind的软件,可以现学现用。第二天早上7点钟起来开始,首先画了个图:

2010031109470330.jpg

我想为每个具体的技术创建5个文件夹。这样一来创建文件夹成了一个重复性的工作。为了偷懒(结果证明花的时间更多了),就想用程序批量生成这个5个文件夹,我只需要输入对应的名字就OK了。想法很好,结果花了2个多小时才完成编码。

其实目录及子目录的创建非常简单,MSDN上有现成的代码可供使用。

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"为基础,直接分析出需要创建的目录和路径。用正则匹配出最后一个'\'是最简单的方法.

 Regex reg = new Regex("\\");
 Match theMatch = reg.Match(args[0]);

但是C#编译期始终报错---” System.ArgumentException: 正在分析“\”- \ 在模式末尾非法。“

本来想就这么算了,反正做完了,但是还是让我不爽,又继续找。这次把搜索定在文件路径的匹配上,我就不相信没人碰到过。

果然还是有的:http://hi.baidu.com/lindily/blog/item/c140a16e5e6f29d380cb4a2e.html

改正这样就可以了:

 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就可以了嘛,然后再根据自己需要输入需要创建的目录就可以了。于是这个问题化简了。

碰到的问题二:

for(int i=0;i<SubDirectoryNames.Length;i++)
       DirectoryInfo   dis = di.CreateSubdirectory(SubDirectoryNames[i]);

因为我是用文本在写,所以它这里报错我找一阵才找到。“嵌入的语句不能是声明或标记语句”。

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~~~以后再改进功能~

转载于:https://www.cnblogs.com/kulong995/archive/2010/03/11/1683215.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值