注册 登录
编程论坛 ASP.NET技术论坛

如何循环指定文件夹中的所有文件夹?急!!

hhy420459674 发布于 2010-12-21 15:46, 862 次点击
我指定一个文件夹:E:\heal
该文件夹下面有很多个文件夹,每个文件夹下面就是文件
我想要实现的是:循环E:\heal下面所有的文件夹,判断每个文件夹下面是否有文件,如果有,则上传这个文件到我的数据库,然后删除该文件,请问   我该怎么做呢?才接触文件夹、文件这类东西,希望各位帮帮忙!
谢谢!
4 回复
#2
dadongzicool2010-12-21 18:13
顶一个   
#3
cccool2010-12-21 22:34
这个应该对你有帮助。
程序代码:
/// <summary>
   
/// 拷贝目录(包含子目录及所有文件)
   
/// </summary>
   
/// <param name="sourceDirName">源文件目录名</param>
   
/// <param name="destDirName">目标文件目录名</param>
   
/// <param name="copySubDirs">是否拷贝子目录</param>
    internal static void DirectoryCopy(
        string sourceDirName, string destDirName, bool copySubDirs)
    {
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        DirectoryInfo[] dirs = dir.GetDirectories();

        // If the source directory does not exist, throw an exception.
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        // If the destination directory does not exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the file contents of the directory to copy.
        FileInfo[] files = dir.GetFiles();

        foreach (FileInfo file in files)
        {
            // Create the path to the new copy of the file.
            string temppath = (destDirName, file.Name);

            // Copy the file.
            file.CopyTo(temppath, false);
        }

        // If copySubDirs is true, copy the subdirectories.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                // Create the subdirectory.
                string temppath = (destDirName, subdir.Name);

                // Copy the subdirectories.
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
把上面程序改改就可以了。

[ 本帖最后由 cccool 于 2010-12-21 22:37 编辑 ]
#4
hhy4204596742010-12-22 11:02
回复 3楼 cccool
谢谢,解决了!
不过我有另外一个问题:
我有个.eml 的文件,我想把他存进数据库中,请问我该如何存呢,最好有代码哈,谢谢!
也就是说:如何将.eml的文件保存到数据库中呢
#5
何事惊慌2011-01-17 09:24
和保持图片的过程应该是
一样的,直接存为2进制
1