[英]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.