Fork me on GitHub

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

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

我想为每个具体的技术创建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~~~以后再改进功能~

posted @ 2010-03-11 10:36  idoku  阅读(649)  评论(0编辑  收藏  举报