473,653 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_RO OT, 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.EventArg s e)
{
string fName = tbFile.Text.Tri m();

if ( !File.Exists(fN ame) )
{
MessageBox.Show (this, "File " + fName + " does not exist", "File IO
Error");
return;
}

string ReaderPath = String.Empty;
try
{
RegistryKey key = Registry.Classe sRoot;
RegistryKey keyPDF = key.OpenSubKey( ".pdf", false);
string [] classNames = keyPDF.GetSubKe yNames();

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(keyP dfApp);
}
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.Spli t(new char[] {'\"'});

// launch the reader with the .pdf file in the command line
System.Diagnost ics.Process.Sta rt(path[1], fName);
}
catch ( Exception ex )
{
MessageBox.Show (ex.Message);
}
}
// returns the command string or String.Empty
private string GetCommand(Regi stryKey key)
{
string [] names = key.GetSubKeyNa mes();

if ( names == null )
return String.Empty;

foreach ( string name in names )
{
RegistryKey subkey = key.OpenSubKey( name, false);

string [] substrings = (subkey.Name).S plit(new char [] {'\\'});

// see if this is the key ending in "command"
if ( substrings[substrings.Leng th-1] == "command" )
{
// return the default value
return ((string)subkey .GetValue(""));
}

// not found - recurse the subkey
string command = GetCommand(subk ey);
if ( command != String.Empty )
return command;
}

return String.Empty;
}

Thanks for any help.

Alfredo

Nov 16 '05 #1
13 4937
Sounds like the long way to get there ... :)

How about using:

System.Diagnost ics.Process.Sta rt(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******** ******@tk2msftn gp13.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_RO OT, 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.EventArg s e)
{
string fName = tbFile.Text.Tri m();

if ( !File.Exists(fN ame) )
{
MessageBox.Show (this, "File " + fName + " does not exist", "File IO
Error");
return;
}

string ReaderPath = String.Empty;
try
{
RegistryKey key = Registry.Classe sRoot;
RegistryKey keyPDF = key.OpenSubKey( ".pdf", false);
string [] classNames = keyPDF.GetSubKe yNames();

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(keyP dfApp);
}
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.Spli t(new char[] {'\"'});

// launch the reader with the .pdf file in the command line
System.Diagnost ics.Process.Sta rt(path[1], fName);
}
catch ( Exception ex )
{
MessageBox.Show (ex.Message);
}
}
// returns the command string or String.Empty
private string GetCommand(Regi stryKey key)
{
string [] names = key.GetSubKeyNa mes();

if ( names == null )
return String.Empty;

foreach ( string name in names )
{
RegistryKey subkey = key.OpenSubKey( name, false);

string [] substrings = (subkey.Name).S plit(new char [] {'\\'});

// see if this is the key ending in "command"
if ( substrings[substrings.Leng th-1] == "command" )
{
// return the default value
return ((string)subkey .GetValue(""));
}

// not found - recurse the subkey
string command = GetCommand(subk ey);
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*********@Ho tmail.com> wrote in message
news:#W******** ******@tk2msftn gp13.phx.gbl...
Sounds like the long way to get there ... :)

How about using:

System.Diagnost ics.Process.Sta rt(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******** ******@tk2msftn gp13.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_RO OT, 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.EventArg s e)
{
string fName = tbFile.Text.Tri m();

if ( !File.Exists(fN ame) )
{
MessageBox.Show (this, "File " + fName + " does not exist", "File IO
Error");
return;
}

string ReaderPath = String.Empty;
try
{
RegistryKey key = Registry.Classe sRoot;
RegistryKey keyPDF = key.OpenSubKey( ".pdf", false);
string [] classNames = keyPDF.GetSubKe yNames();

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(keyP dfApp);
}
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.Spli t(new char[] {'\"'});

// launch the reader with the .pdf file in the command line
System.Diagnost ics.Process.Sta rt(path[1], fName);
}
catch ( Exception ex )
{
MessageBox.Show (ex.Message);
}
}
// returns the command string or String.Empty
private string GetCommand(Regi stryKey key)
{
string [] names = key.GetSubKeyNa mes();

if ( names == null )
return String.Empty;

foreach ( string name in names )
{
RegistryKey subkey = key.OpenSubKey( name, false);

string [] substrings = (subkey.Name).S plit(new char [] {'\\'});

// see if this is the key ending in "command"
if ( substrings[substrings.Leng th-1] == "command" )
{
// return the default value
return ((string)subkey .GetValue(""));
}

// not found - recurse the subkey
string command = GetCommand(subk ey);
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******** ******@tk2msftn gp13.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*********@Ho tmail.com> wrote in message
news:#W******** ******@tk2msftn gp13.phx.gbl...
Sounds like the long way to get there ... :)

How about using:

System.Diagnost ics.Process.Sta rt(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*********@Ho tmail.com> wrote in message
news:u6******** ******@TK2MSFTN GP11.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******** *****@TK2MSFTNG P10.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.Diagnost ics.ProcessStar tInfo psi = new
System.Diagnost ics.ProcessStar tInfo();
psi.UseShellExe cute = true;
psi.Verb = "print";
psi.WindowStyle = System.Diagnost ics.ProcessWind owStyle.Normal;
psi.Arguments = "/p";
psi.FileName = @"C:\SomePDF.pd f";
System.Diagnost ics.Process p = System.Diagnost ics.Process.Sta rt(psi);
p.WaitForInputI dle();
p.CloseMainWind ow();

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

"Mark Rae" <ma**@markrae.c o.uk> wrote in message
news:ec******** ******@TK2MSFTN GP11.phx.gbl...
"Jim Hughes" <NO*********@Ho tmail.com> wrote in message
news:u6******** ******@TK2MSFTN GP11.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******** ********@TK2MSF TNGP12.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.WaitForInputI dle();


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.c o.uk> wrote in message
news:u$******** ******@TK2MSFTN GP10.phx.gbl...
"Jason" <no****@nospam. com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.WaitForInputI dle();
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

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

Similar topics

8
5370
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 is used by the user 2- check if a link to my application is there 3- if so, obtain the path (ANY PATH) to the quick-launch-folder from existing button(s)
4
5033
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 data, then shuts down. Except for the scheduled time and file paths, the job settings are identical. The machine is password-protected, so when each job was created the password was entered.
1
5908
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 of the users choice on the remove, "agent" machine. I've discovered that Process.Start() won't work because it runs under asp.net and it will launch any "process" but if the application has a windows UI it will not show.
5
32727
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 pass the file name (which will then result in the application associated with the file extension to launch). I want to prevent the application registered in the system as being associated with the file extension from opeing the file.
4
20861
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 anywhere? Thanks, Joe
5
20074
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 or call external program as of different user. The problem of that design is that the mother application is still running as current user. I know that less privilege is more secure.
7
4664
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 GUI so the app would come up and be running independent of the web browser (i.e. not embeded in it). Also note, that this application is one that is already installed
1
1649
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 application. The application name is dynamic and is selected from table. WScript.Echo verifies that it is calling the correct application and the path statement is correct. Also the application does reside where I am calling it from. The code...
6
2623
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. The executable will crunch some data and then launch another application.
0
8370
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8704
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.