C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
ShellExecute Posted by jk on 20 Aug 2009 at 8:27 AM
Hi - in .NET Framework, I need to do something similar to the ShellExecute in the Win32 API. What I need to do is open a file with whatever program its file type is associated with in Windows.

I found the Shell command in .NET but it appears that you have to provide it with the EXE, which is not what I need to do.

I assume there has to be something more elegant in .NET Framework than using the Win32 API ShellExecute.

Any help would be much appreciated.
Thanks.
Report
Re: ShellExecute Posted by DataDink on 20 Aug 2009 at 1:04 PM
All file associations are stored in the registry - I believe shellexecute is simply looking up these associations and sending the appropriate application a command-line.

If you don't want to do this I have also just sent the file to "explorer.exe" because explorer.exe will for the most part launch the appropriate association.
Report
Re: ShellExecute Posted by jk on 24 Aug 2009 at 8:07 AM
Good suggestion about using Explorer, thanks!
Report
Re: ShellExecute Posted by jk on 17 Sept 2009 at 1:31 PM
It turns out that using Explorer isn't working so well for users.

The filetype I'm opening in code is .jpg and on a user's machine it's associated with the Windows Picture and Fax Viewer.

However the following line of C# code always opens the .jpgs with Internet Explorer, and not the app that .jpg is associated with:

Interaction.Shell(theExe + " " + imagePath + filName, AppWinStyle.MaximizedFocus, false, -1);

The variable theExe is a string containing the path to the Windows Explorer executable on that computer. I've got a method that produces that, the core of which is:

string searchStr;
foreach (DriveInfo drvInf in DriveInfo.GetDrives())
{
//Have to use Fixed drive type to avoid situation in Citrix where
//we were getting the drives from the computer that logged in
//to Citrix (in addition to the Citrix machine's drives).
if (drvInf.DriveType == DriveType.Fixed)
{
searchStr = drvInf.Name;
searchStr = searchStr + @"Windows\explorer.exe";
FileInfo filInf = new FileInfo(searchStr);
if (filInf.Exists)
{
return searchStr;
}
}
}

--Any ideas on how I can get it to really use the associated file type?
Thanks.
Report
Re: ShellExecute Posted by jk on 18 Sept 2009 at 1:01 PM
I figured out a much better way to do this without using ShellExecute.

There's a class called Process under the System.Diagnostics namespace that's very useful. Kind of a strange place to put it since you'd think System.Diagnostics would be all about error handling.

Anyway, here's an example of how you can use StartInfo and then Start to open a file with whatever application it's type is associated with:

System.Diagnostics.Process myTestProcess = new System.Diagnostics.Process();

myTestProcess.StartInfo.FileName = @"c:\files\jpg\1.jpg";
myTestProcess.Start();




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.