简体   繁体   English

从 C# 中的文件夹和子文件夹中获取文件名

[英]Get file names from folder and sub-folders in C#

I am trying to get name and location of all files from main folder and sub-folders and insert them in database, below is my current implementation and working fine but does not include those files that are in sub-folders, please help fix code below.我正在尝试从主文件夹和子文件夹中获取所有文件的名称和位置并将它们插入数据库,下面是我当前的实现并且工作正常,但不包括子文件夹中的那些文件,请帮助修复下面的代码. thanks谢谢

        static void Main(string[] args)
        {
            string VarDirectoryPath = "\\\\share\\folder\\";
            SqlConnection SQLConnection = new SqlConnection();
            SQLConnection.ConnectionString = SQLConnection.ConnectionString = "connectionString";
            string[] files = Directory.GetFiles(VarDirectoryPath);
            SqlCommand SqlCmd = new SqlCommand();
            SqlCmd.Connection = SQLConnection;
            SQLConnection.Open();
            foreach (string filename in files)
            {

                FileInfo file = new FileInfo(filename);

                SqlCmd.CommandText = "Insert into dbo.FileInformation(";
                SqlCmd.CommandText += "[FolderPath],[FileName],[LastWriteTime],[CreateTime],[FileSizeinKB])";
                SqlCmd.CommandText += " Values('"
                                  + VarDirectoryPath + "','"
                                  + file.Name + "','"
                                  + file.LastWriteTime + "','"
                                 + file.CreationTime
                                 + "','" + file.Length / 1024
                                 + "')";
                SqlCmd.ExecuteNonQuery();
            }
            SQLConnection.Close();
        }
    }
}

You can use您可以使用

string[] files = Directory.GetFiles(VarDirectoryPath,"*.*",SearchOption.AllDirectories)

for more details you can read msdn-Directory.GetFiles有关更多详细信息,您可以阅读msdn-Directory.GetFiles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为C#Intellisense生成XML文件,为子文件夹损坏 - Generating XML file for C# Intellisense, broken for sub-folders 如何获取azure文件共享存储c#中子文件夹内的所有文件? - How to get all files which is inside sub-folders in azure file share storage c#? 如何将文件从文件夹和子文件夹传输到 C# 中由文件名模式创建的特定文件夹(File2specific Folders) - How to transfer files form folders and sub-folders to specific folders created by file name pattern in C# (File2specific Folders) C# 在以日期命名的多个子文件夹中获取具有多个通配符的文件 - C# get files with multiple wildcard within multiple sub-folders named after dates 忽略特定子文件夹的 C# 编译警告? - Ignore C# compile warnings for specific sub-folders? 从特定结构的子文件夹中获取文件? - Get files from specifically structured sub-folders? 复制文件夹但忽略一些子文件夹 - Copy a folder but ignoring some sub-folders 从资源子文件夹获取文件名 - Get file names from Resources sub folder 根据文件夹 C# Winforms 的内容检查和列出子文件夹 - Check and list sub-folders based on contents of folders C# Winforms 在 ASP.Net C# 中从 Gridview 控件的主文件夹中获取子文件夹和文件名 - Get Sub Folders and Filenames From Main Folder in Gridview Control in ASP.Net C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM