D
David Rose
Hi,
I am trying to launch pdf files (in this case). The test application takes
the path to the pdf from a TextBox when the user clicks a button. I have
written code that works on my computer, but I have doubts that my method is
universal.
Basically, in HKEY_CLASSES_ROOT, I look up the .pdf extension and get the
human readable class id. Then I look that up, recursing through the tree
until I get to "command" and then return that value. This seems to work for
the Adobe Acrobat Reader...
There must be some general method that Windows uses to find the application
when the user double clicks on a file. Or, is there something in the .NET
Framework to accomplish this?
Here is my code:
private void OnViewFileClick(object sender, System.EventArgs e)
{
string fName = tbFile.Text.Trim();
if ( !File.Exists(fName) )
{
MessageBox.Show(this, "File " + fName + " does not exist", "File IO
Error");
return;
}
string ReaderPath = String.Empty;
try
{
RegistryKey key = Registry.ClassesRoot;
RegistryKey keyPDF = key.OpenSubKey(".pdf", false);
string [] classNames = keyPDF.GetSubKeyNames();
if ( classNames == null )
{
MessageBox.Show(this, "No application associated with the .pdf
extension");
return;
}
// this seems like it would not get all cases...
RegistryKey keyPdfApp = key.OpenSubKey(classNames[0], false);
ReaderPath = GetCommand(keyPdfApp);
}
catch
{
MessageBox.Show(this, "Error: could not find Adobe Acrobat Reader",
"Launch Error");
}
try
{
// break out the command string from the ReaderPath string
string [] path = ReaderPath.Split(new char[] {'\"'});
// launch the reader with the .pdf file in the command line
System.Diagnostics.Process.Start(path[1], fName);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
// returns the command string or String.Empty
private string GetCommand(RegistryKey key)
{
string [] names = key.GetSubKeyNames();
if ( names == null )
return String.Empty;
foreach ( string name in names )
{
RegistryKey subkey = key.OpenSubKey(name, false);
string [] substrings = (subkey.Name).Split(new char [] {'\\'});
// see if this is the key ending in "command"
if ( substrings[substrings.Length-1] == "command" )
{
// return the default value
return ((string)subkey.GetValue(""));
}
// not found - recurse the subkey
string command = GetCommand(subkey);
if ( command != String.Empty )
return command;
}
return String.Empty;
}
Thanks for any help.
Alfredo
I am trying to launch pdf files (in this case). The test application takes
the path to the pdf from a TextBox when the user clicks a button. I have
written code that works on my computer, but I have doubts that my method is
universal.
Basically, in HKEY_CLASSES_ROOT, I look up the .pdf extension and get the
human readable class id. Then I look that up, recursing through the tree
until I get to "command" and then return that value. This seems to work for
the Adobe Acrobat Reader...
There must be some general method that Windows uses to find the application
when the user double clicks on a file. Or, is there something in the .NET
Framework to accomplish this?
Here is my code:
private void OnViewFileClick(object sender, System.EventArgs e)
{
string fName = tbFile.Text.Trim();
if ( !File.Exists(fName) )
{
MessageBox.Show(this, "File " + fName + " does not exist", "File IO
Error");
return;
}
string ReaderPath = String.Empty;
try
{
RegistryKey key = Registry.ClassesRoot;
RegistryKey keyPDF = key.OpenSubKey(".pdf", false);
string [] classNames = keyPDF.GetSubKeyNames();
if ( classNames == null )
{
MessageBox.Show(this, "No application associated with the .pdf
extension");
return;
}
// this seems like it would not get all cases...
RegistryKey keyPdfApp = key.OpenSubKey(classNames[0], false);
ReaderPath = GetCommand(keyPdfApp);
}
catch
{
MessageBox.Show(this, "Error: could not find Adobe Acrobat Reader",
"Launch Error");
}
try
{
// break out the command string from the ReaderPath string
string [] path = ReaderPath.Split(new char[] {'\"'});
// launch the reader with the .pdf file in the command line
System.Diagnostics.Process.Start(path[1], fName);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
// returns the command string or String.Empty
private string GetCommand(RegistryKey key)
{
string [] names = key.GetSubKeyNames();
if ( names == null )
return String.Empty;
foreach ( string name in names )
{
RegistryKey subkey = key.OpenSubKey(name, false);
string [] substrings = (subkey.Name).Split(new char [] {'\\'});
// see if this is the key ending in "command"
if ( substrings[substrings.Length-1] == "command" )
{
// return the default value
return ((string)subkey.GetValue(""));
}
// not found - recurse the subkey
string command = GetCommand(subkey);
if ( command != String.Empty )
return command;
}
return String.Empty;
}
Thanks for any help.
Alfredo