473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to reliably launch an application?

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

Nov 16 '05 #1
13 4918
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" <Da********@rcn.com> wrote in message
news:eS**************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #2
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" <NO*********@Hotmail.com> wrote in message
news:#W**************@tk2msftngp13.phx.gbl...
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" <Da********@rcn.com> wrote in message
news:eS**************@tk2msftngp13.phx.gbl...
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


Nov 16 '05 #3
Maybe so... But I won't tell anyone! :)

But look at how much you learned!

"David Rose" <Da********@rcn.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
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" <NO*********@Hotmail.com> wrote in message
news:#W**************@tk2msftngp13.phx.gbl...
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.


<snip>
Nov 16 '05 #4
"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
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...
Nov 16 '05 #5
> 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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #6
"cody" <no****************@gmx.net> wrote in message
news:OR*************@TK2MSFTNGP10.phx.gbl...
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.


Nope...
Nov 16 '05 #7
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.

"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:ec**************@TK2MSFTNGP11.phx.gbl...
"Jim Hughes" <NO*********@Hotmail.com> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
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...

Nov 16 '05 #8
"Jason" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

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
Nov 16 '05 #9
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.

"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:u$**************@TK2MSFTNGP10.phx.gbl...
"Jason" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

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

Nov 16 '05 #10
"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
Nov 16 '05 #11
"Jason" <no****@nospam.com> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...

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
Nov 16 '05 #12
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

"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:eD**************@TK2MSFTNGP09.phx.gbl...
"Jason" <no****@nospam.com> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...

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

Nov 16 '05 #13
Mark,
http://www.cs.wisc.edu/~ghost/doc/gsapi.htm is probably as good of an
example as what I have. My code is very specific to my company and would
probably be difficult to interpret. If you have any questions, I'd be glad
to answer them.

Jason
"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:eD**************@TK2MSFTNGP09.phx.gbl...
"Jason" <no****@nospam.com> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...

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

Nov 16 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dutchy | last post by:
Dear reader, In an attempt to obtain the path to the quick-launch-folder in order to create a shortcut to my application-updates during installation , I thought to: 1- check if quick launch...
4
by: DFS | last post by:
I have two nightly scheduled jobs at a client site - one at 2:00am and the other at 5:00am Both jobs launch Access 2003, log into the default system workgroup, open a Access .mdb that collects...
1
by: Michael Howes | last post by:
I have a c# windows form that talks to a web service on a server that then can talk to multiple "agents" on different machines using web service calls. I want to be able to launch an application...
5
by: GrantS | last post by:
Hi I am trying to use ShellExecute to launch an application to display a certain file. The variation on the theme is that I need to be able to specify the application to launch and not simply...
4
by: Joe | last post by:
I created a CustomAction for this but I don't think I have it in the right place. I tried both Install and Commit but neither allow it to get to the final screen. Are there any examples of this...
5
by: cooltoriz | last post by:
Hello there, I am not asking how to impersonate a process within C# windows application. I already know that, in C# v2.0, you can easily achieve it using ProcessStartInfo. You can run a process...
7
by: dongarbage | last post by:
Hi there, I'm very new to activex controls and c# programming. I'm writing a c# application and I want it to be invoked when my users click a button on a web page. Its an application with a...
1
BezerkRogue
by: BezerkRogue | last post by:
I have created a VB Script to synchronize software versions and then launch an application on the system it is run against. The script runs and generates no errors but will not launch the second...
6
by: tempnode | last post by:
I have a problem that I can't seem to solve: I need to write a C++ app that will run off of a floppy. Basically, I will boot into DOS (from a floppy), and run my executable from the floppy. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.