C#-使用SearchOption在目录中搜索匹配的文件名
背景:我正在使用C#开发一个WinForms应用程序,带有OpenFileDialog和FileBrowserDialog,它将1)在指定源目录的文件名中搜索特定字符串2)将文件复制到合并目录3)将多个文件从excel转换为csv文件,然后3)使用命令行可执行文件将所有生成的csv文件转换为一个大的csv文件 示例:MSDN提供了一个代码示例,其中列出了“c:\”中以字母“c”开头的所有目录和文件。所以我的代码就是基于这个 问题:代码没有将任何文件复制到合并文件夹中,所以我很确定搜索不起作用 我应该在这里换什么?它不起作用:C#-使用SearchOption在目录中搜索匹配的文件名,c#,winforms,search,process,C#,Winforms,Search,Process,背景:我正在使用C#开发一个WinForms应用程序,带有OpenFileDialog和FileBrowserDialog,它将1)在指定源目录的文件名中搜索特定字符串2)将文件复制到合并目录3)将多个文件从excel转换为csv文件,然后3)使用命令行可执行文件将所有生成的csv文件转换为一个大的csv文件 示例:MSDN提供了一个代码示例,其中列出了“c:\”中以字母“c”开头的所有目录和文件。所以我的代码就是基于这个 问题:代码没有将任何文件复制到合并文件夹中,所以我很确定搜索不起作用 我
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...
}
}
}
}
您发布的代码执行两个单独的搜索循环: 第一:
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()
在第一个示例中,您正在查找与m_sc.get_Corp()匹配的文件代码>,在第二个目录中查找目录
事实上。。。你的代码(伪代码?)毫无意义
尝试:
- 慢慢来
- 自己整理代码
- 如果你慢慢地重写它并把它分成小块,你可能会发现你做错了什么
您发布的代码执行两个单独的搜索循环:
第一:
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()
在第一个示例中,您正在查找与m_sc.get_Corp()匹配的文件代码>,在第二个目录中查找目录
事实上。。。你的代码(伪代码?)毫无意义
尝试:
- 慢慢来
- 自己整理代码
- 如果你慢慢地重写它并把它分成小块,你可能会发现你做错了什么
试着清理一下,下面是一些代码,它们会让你走上正轨,我已经排除了CSV转换和合并,希望你会明白这一点
private void YourFileRoutine(string sourceDirectoryPath, string consolidatedDirectoryPath)
{
var excelFiles = new DirectoryInfo(sourceDirectoryPath).GetFiles().Where(x => x.Extension == ".xlsx");
//Copy all Excel Files to consolidated Directory
foreach (var excelFile in excelFiles)
{
FileInfo copiedFile = excelFile.CopyTo(String.Concat(consolidatedDirectoryPath, excelFile.Name)); // Make sure consolidatedDirectoryPath as a "\" maybe use Path.Combine()?
// ConvertToCSV( Do your CSV conversion here, the Path will be = Path.GetFullPath(copiedFile);
}
// Merge CSV's
var csvFiles = new DirectoryInfo(consolidatedDirectoryPath).GetFiles().Where(x => x.Extension == ".csv");
// SomeMergeMethod that iterates through this FileInfo collection?
}
尝试清理一下,下面是一些代码,将把你的路径,我已经排除了CSV转换和合并,希望你会得到的想法
private void YourFileRoutine(string sourceDirectoryPath, string consolidatedDirectoryPath)
{
var excelFiles = new DirectoryInfo(sourceDirectoryPath).GetFiles().Where(x => x.Extension == ".xlsx");
//Copy all Excel Files to consolidated Directory
foreach (var excelFile in excelFiles)
{
FileInfo copiedFile = excelFile.CopyTo(String.Concat(consolidatedDirectoryPath, excelFile.Name)); // Make sure consolidatedDirectoryPath as a "\" maybe use Path.Combine()?
// ConvertToCSV( Do your CSV conversion here, the Path will be = Path.GetFullPath(copiedFile);
}
// Merge CSV's
var csvFiles = new DirectoryInfo(consolidatedDirectoryPath).GetFiles().Where(x => x.Extension == ".csv");
// SomeMergeMethod that iterates through this FileInfo collection?
}
@JoshM,我删除了另外一半的代码,这样代码更容易分析。代码没有将任何文件复制到合并文件夹,所以我很确定搜索不起作用。你调试过吗?这将是查看搜索是否返回任何内容的第一步…您有foreach(FileInfo file in files),这意味着files是FileInfo类型,但您有string files=“.xlsx”,或者键入者需要更多有关这方面的信息。如果有错误,你会得到什么错误。什么是无效的,代码中的什么位置?此外,您不需要两个循环并递归调用DirSearch方法。改为使用:string[]filesNames=Directory.GetFiles(sDir、files、SearchOption.AllDirectories);foreach(filenames中的字符串d){@JoshM,我删除了一半的其他代码,因此代码更易于分析。代码不会将任何文件复制到合并文件夹中,因此我非常确定搜索不起作用。您是否调试了它?这将是查看搜索是否返回任何内容的第一步…您已经完成了foreach(FileInfo file in files)这意味着files是FileInfo类型,但您有string files=“.xlsx”或者键入错误需要更多信息。如果出现错误,您会得到什么错误。什么不起作用以及代码中的位置?此外,您不需要两个循环并递归调用DirSearch方法。请改用:string[]filesNames=Directory.GetFiles(sDir,files,SearchOption.AllDirectories);foreach(filesNames中的字符串d){+1用于清理混乱…最近这里有太多垃圾代码(垃圾=不太干净和重复的大块代码,做20件事的方法,等等)@Stuart,+1谢谢你的回复。这开始有点太多的代码需要筛选,所以我一直在变得草率和犯错误。打得好。我想我会删除其中的一些评论。你建议我如何缩短csv转换和整合的代码行数?我实际上只是调试它以查看结果问题。如果我正在调试这段代码,我只会在每一个内部循环的第一行上放置一个断点,然后在第一行上放置一个断点……你很快就会发现调用了哪几行代码,监视窗口会告诉你每一行上发生了什么。一旦你发现一行代码没有达到预期的效果,你就可以执行JU不要创建一个新的较小的函数来运行那一点点代码——要么自己用这个较小的函数来解决问题——要么带着一个特定的问题回到这里。希望这能有所帮助:)斯图尔特(一个气囊!)@斯图尔特,尽管我已将multiselect设置为true,但它仍然不起作用。你知道为什么会这样吗?“multiselect不起作用”对我来说没有任何意义。你的代码中的哪一位是多重选择?什么是“不工作”意味着什么?它会引发异常吗?它返回null吗?请给我们一些工作!+1来清理混乱…最近这里有太多垃圾代码(垃圾=不是很干净和重复的大块代码,方法做20件事,等等)@Stuart,+1谢谢你的回复。这开始有点太多的代码需要筛选,所以我一直在变得草率和犯错误。打得好。我想我会删除其中的一些评论。你建议我如何缩短csv转换和整合的代码行数?我实际上只是调试它以查看结果问题。如果我正在调试这段代码,我只会在每一个内部循环的第一行上放置一个断点,然后在第一行上放置一个断点……您很快就会发现调用了哪些代码行,监视窗口会告诉您