如何从打开该文件的特定文件夹广告中搜索.pdf文件 [英] how to search .pdf file from the particular folder ad opening that file

查看:62
本文介绍了如何从打开该文件的特定文件夹广告中搜索.pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我搜索了很多次以获得这个想法,但没有得到。所以请给我一些建议或示例代码

解决方案

搜索a特定文件夹中的pdf文件:



1.在变量中定义文件夹的路径:



string folderPath =你的文件夹路径;

  string  [] fileGroup = Directory.GetFiles(folderPath ,  * .pdf); 





在这个文件Grouparray中你将获得该文件夹中存在的所有pdf文件。



如果需要任何特定的,那么使用foreach循环和比较它:



  foreach 字符串 fileName  fileGroup中)
{

}


你应该分开你的要求分为两部分:

- 获取特定文件夹中的PDF文件列表

- 使用默认系统处理程序打开特定文件



对于第一个,你必须在System.IO命名空间中使用对象。

 使用 System.IO; 

/// < 摘要 >
/// 检索特定文件夹中具有特定扩展名的文件列表。
/// < / summary >
/// < param < span class =code-summarycomment> name =path > 目录路径在其中搜索文件。< / param > ;
/// < param name =extension <跨度class =code-summarycomment>> 要搜索的文件扩展名。< / param >
/// < 返回 > IEnumerable文件列表,其中< paramref name =extension / < span class =code-summarycomment>>
/// < paramref name =path / > < / returns >
public static IEnumerable< FileInfo> GetFiles(字符串路径,字符串扩展名){
DirectoryInfo di = new DirectoryInfo(path);
if (di.Exists){
string search = string .Format( *。{0},延期);
FileInfo [] files = di.GetFiles(search);
foreach (FileInfo fi in files){
yield return fi;
}
}
}





因此,要获取C:\\中的所有PDF文件\\ Test,你会写:

 IEnumerable< FileInfo> pdfFiles = GetFiles( @  C:\ Test  pdf); 





然后你想要打开获得的文件;这样:

  foreach (FileInfo fi  in  pdfFiles){
ProcessStartInfo psi = new ProcessStartInfo(fi.FileName);
psi.UseShellExecute = true ;
Process.Start(psi);
}





希望这会有所帮助。祝你好运!


i am new in C# and i search lots of times for get this idea but not get .so please give me some advice or sample code

解决方案

Search a pdf file in a particular folder:

1. Define the path of the folder in a variable:

string folderPath= "Your folder path";

string [] fileGroup= Directory.GetFiles(folderPath, "*.pdf");



In this file Grouparray you will get all the pdf files existing in that folder.

If needed any specific then use foreach loop and compare it :

foreach(string fileName in fileGroup)
{
   
}


You should split your requirement in two parts:
- getting a list of PDF files in a specific folder
- opening a specific file with default system handler

For the first one, you have to use object in System.IO namespace.

using System.IO;

/// <summary>
/// Retrieves a list of files with a specific extension in a specific folder.
/// </summary>
/// <param name="path">Directory path in which to search for files.</param>
/// <param name="extension">File extension to search for.</param>
/// <returns>An IEnumerable list of files with <paramref name="extension" /> in
/// <paramref name="path" />.</returns>
public static IEnumerable<FileInfo> GetFiles(string path, string extension) {
   DirectoryInfo di = new DirectoryInfo(path);
   if (di.Exists) {
      string search = string.Format("*.{0}", extension);
      FileInfo[] files = di.GetFiles(search);
      foreach (FileInfo fi in files) {
         yield return fi;
      }
   }
}



So, to get all PDF files in "C:\Test" folder, you would write:

IEnumerable<FileInfo> pdfFiles = GetFiles(@"C:\Test", "pdf");



Then you want to open the file(s) obtained; this way:

foreach (FileInfo fi in pdfFiles) {
   ProcessStartInfo psi = new ProcessStartInfo(fi.FileName);
   psi.UseShellExecute = true;
   Process.Start(psi);
}



Hope this helps. Good luck!


这篇关于如何从打开该文件的特定文件夹广告中搜索.pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆