How to reliably launch an application?

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
 
J

Jim Hughes

Sounds like the long way to get there ... :)

How about using:

System.Diagnostics.Process.Start(tbFile.Text.Trim());

This will open the application or the application associated with the file
and open the file.
 
D

David Rose

Well...

I, um, thought that you had to... well, you know... I mean... Works like
that does it?

Boy, do I feel silly.

Thanks.



Jim Hughes said:
Sounds like the long way to get there ... :)

How about using:

System.Diagnostics.Process.Start(tbFile.Text.Trim());

This will open the application or the application associated with the file
and open the file.

David Rose said:
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
 
J

Jim Hughes

Maybe so... But I won't tell anyone! :)

But look at how much you learned!

David Rose said:
Well...

I, um, thought that you had to... well, you know... I mean... Works like
that does it?

Boy, do I feel silly.

Thanks.

<snip>
 
M

Mark Rae

Maybe so... But I won't tell anyone! :)

But look at how much you learned!

Ah yes, but if you can tell me how to *print* a PDF using a similar
technique, then I'll really be impressed... :)

By which, I mean without leaving the Adobe reader open on the taskbar...
 
C

cody

Ah yes, but if you can tell me how to *print* a PDF using a similar
technique, then I'll really be impressed... :)

By which, I mean without leaving the Adobe reader open on the taskbar...


Maybe you have to add a parameter like /print or similar to the argument
string.
 
J

Jason

Mark,
See if this will work for you.

System.Diagnostics.ProcessStartInfo psi = new
System.Diagnostics.ProcessStartInfo();
psi.UseShellExecute = true;
psi.Verb = "print";
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
psi.Arguments = "/p";
psi.FileName = @"C:\SomePDF.pdf";
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
p.WaitForInputIdle();
p.CloseMainWindow();

Jason Newell (MCAD)
Software Engineer
The Charles Machine Works, Inc.
 
M

Mark Rae

Jason,
See if this will work for you.

I've tried many different variants of similar code. It works, of course,
inasmuch as it prints the PDF document, but the main problem is that it
leaves the Adobe Reader window minimised on the taskbar in run mode though,
curiously, not in debug mode... In debug mode, the code halts for upwards of
30 seconds on the line
p.WaitForInputIdle();

but in run mode it seems to determine that the process is idle pretty much
immediately. It then tries to close the window but can't because it's still
busy.

Thanks for the reply.

Mark
 
J

Jason

Mark,
Here's a long shot for you. Have you ever seen
http://www.ghostscript.com/. I actually have a C# project that runs
ghostscript in batch mode using P/Invoke. Ghostcript has a ton of options,
as far as printing any file, or converting any file format to almost any
file format. It works very well for me. Not sure about your situation.
What I ended up using it for, was to convert the .pdf to an image and
dealing with the image from that point. ghostscript is a very powerful too.
Very difficult to get started with, but once you get the hang of it, it's
really cool. If this sounds like something you'd be interested in, I'd be
happy to share my C# ghostscript wrapper class with you. Talk to you later.

Jason Newell (MCAD)
Software Engineer
The Charles Machine Works, Inc.
 
B

Bill Woodruff

"I'd be happy to share my C# ghostscript wrapper class with you."

Jason, I would be very happy if you would share this with other folks, like me. Sounds like a great article for CodeProject if you
have the time.

best, Bill Woodruff
dotScience
Chiang Mai, Thailand
 
M

Mark Rae

Jason,
Here's a long shot for you. Have you ever seen
http://www.ghostscript.com/.

I've been advised about this before but have always resisted going down this
route because I prefer to keep things in the .NET world if possible.
However, it's beginning to look like I don't have much choice. I'll take a
bit of a closer look ASAP.
really cool. If this sounds like something you'd be interested in, I'd be
happy to share my C# ghostscript wrapper class with you. Talk to you
later.

I'd appreciate that - thanks.

Mark
 
J

Jason

Bill & Mark,
I will try to get the code uploaded tonight. I'm too busy at work right
at this second to do it now.

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top