Question

I have mafe a simple object using fileInfo class as:

DirectoryInfo myDirectory = new DirectoryInfo(@"d:\Books");
FileInfo[] files = myDirectory.GetFiles();

foreach (FileInfo file in files)
{
    try
    {
        file.OpenRead();
        break;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

The first file in the directory(Books) is PDF. The code throw no exception. Still the particular file doesn't open. what code i am missing or i am doing any error. Thanks for any assistence.

Était-ce utile?

La solution

You should use Process.Start to open files in default application, if you open pdf file, it will open in Adobe Reader if Adobe Reader is default application for pdf:

 Process.Start(file.FullName);

FileInfo.OpenRead returns a read-only FileStream object, not for opening file.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top