Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

Search a pdf file in a particular folder:

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

string folderPath= "Your folder path";
C#
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 :

C#
foreach(string fileName in fileGroup)
{
   
}
 
Share this answer
 
v3
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.
C#
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:
C#
IEnumerable<FileInfo> pdfFiles = GetFiles(@"C:\Test", "pdf");


Then you want to open the file(s) obtained; this way:
C#
foreach (FileInfo fi in pdfFiles) {
   ProcessStartInfo psi = new ProcessStartInfo(fi.FileName);
   psi.UseShellExecute = true;
   Process.Start(psi);
}


Hope this helps. Good luck!
 
Share this answer
 
v3
Comments
dhaval082 23-Apr-14 8:24am    
i have required the code for windows application.how to use this code
phil.o 23-Apr-14 8:28am    
Here is not a code-ordering site. I gave you some directions, you have to do some work on your side to understand and apply it to your requirements. You do not seem to have tried anything at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900