问题描述
背景:我正在使用 C# 和 OpenFileDialog & 开发一个 WinForms 应用程序.FileBrowserDialog 将 1) 在指定源目录的文件名中搜索特定字符串 2) 将文件复制到统一目录 3) 将多个文件从 excel 转换为 csv 文件,然后 3) 将所有生成的 csv 文件转换为 1 个大 csv使用命令行可执行文件
Background: I'm developing a WinForms application using C# with an OpenFileDialog & FileBrowserDialog that will 1) search for a specific string in the filenames of a specified source directory 2) copy files to consolidated directory 3) convert multiple files from excel to csv files, and then 3) convert all the generated csv files into 1 big csv file using a command line executable
示例:MSDN 提供了一个代码示例,其中列出了c:\"中以字母c"开头的所有目录和文件.在 http://msdn.microsoft.com/en-us/library/ms143448.aspx 所以我基于我的代码......
Example: MSDN provides a code example that lists all of the directories and files that begin with the letter "c" in "c:\". at http://msdn.microsoft.com/en-us/library/ms143448.aspx so I based my code on that...
问题:代码不会将任何文件复制到合并文件夹中,所以我很确定搜索不起作用.
Problem: The code doesn't copy any files to the consolidated folder so I'm pretty sure the search doesn't work.
我应该在这里改变什么?它不起作用:
What should I change on here? It doesn't work :
string files = "*.xlsx";
void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, files))
{
// Is this the file we are looking for?
// check excel files for corp name in the filename.
if (f.Contains(m_sc.get_Corp()))
{
// check if thread is cancelled
if (m_EventStop.WaitOne(0, true))
{
// clean-up operations may be placed here
// ...
// inform main thread that this thread stopped
m_EventStopped.Set();
return;
}
else
{
string path = sDir;
string searchPattern = m_sc.get_Corp();
// A file has been found in this directory
DirectoryInfo di = new DirectoryInfo(path);
DirectoryInfo[] directories = di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);
foreach (FileInfo file in files)
{
try
{
// Copy each selected xlsx files into the specified TargetFolder
System.IO.File.Copy(FileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(FileName));
Log("File" + FileName + " has been copied to " + consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
// Convert each selected XLSX File to CSV Using the command prompt code...
}
}
}
}
推荐答案
您发布的代码进行了两个单独的搜索循环:
The code you've posted does two separate search loops:
首先:
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, files))
{
// Is this the file we are looking for?
// check excel files for corp name in the filename.
if (f.Contains(m_sc.get_Corp()))
{
然后它也这样做:字符串路径 = sDir;string searchPattern = m_sc.get_Corp();
then within that it also does: string path = sDir; string searchPattern = m_sc.get_Corp();
// A file has been found in this directory
DirectoryInfo di = new DirectoryInfo(path);
DirectoryInfo[] directories = di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);
foreach (FileInfo file in files)
{
在第一个中您正在寻找与 m_sc.get_Corp();
匹配的文件,在第二个中您正在寻找目录...
In the first one you are looking for files matching m_sc.get_Corp();
, in the second one you are lookinf for directories...
事实上……你的代码(伪代码?)毫无意义……
In fact... your code (pseudo-code?) makes no sense...
试试:
- 慢慢来
- 自己整理代码
- 如果你慢慢地重写它并将其分解成更小的块,你可能会发现你做错了什么.
这篇关于C# - 使用 SearchOption 在目录中搜索匹配的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!