Call system application to open a specified file.

Z

zlf

Hi,
In my program, client is allowed to select a image file from open file
dialog. And I am required to provide a preview button, when clicking it,
system program like paint should be called to open that file.

May you tell me how to implement that. Thx

zlf
 
G

Guest

you can do it couple of ways.

First using File select dialog, select the name of the file selected into a
string.

1. use pinvoke and call the api shellexecute with the bmp file selected.
here is the unmanaged example.
http://support.microsoft.com/default.aspx/kb/170918

2. launch mspaint.exe with process class in .NET
System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.StartInfo.FileName = "mspaint.exe";
// set windows dir
proc.StartInfo.WorkingDirectory = .....
// pass the bmp file name with full path
proc.StartInfo.Arguments =

proc.Start();

-VR
 
Z

zlf

Thank you, I still want to know whether this is way to call the default
assoicated program to open it.
E.g: If bmp is selected, maybe mspaint will be called. If .doc is selected,
microsoft word will be launched. Thx
 
J

Jeff Gaines

Thank you, I still want to know whether this is way to call the default
assoicated program to open it.
E.g: If bmp is selected, maybe mspaint will be called. If .doc is
selected, microsoft word will be launched. Thx

If you call Process.Start using just a document name the default
application (assuming there is one) will open. I use:

public static bool StartProcess(string strCommand, string strWD, out
string strErr)
{
bool blnOK = false;
ProcessStartInfo psi = new ProcessStartInfo();
strErr = "";

// Initialize the ProcessStartInfo structure
psi.FileName = strCommand;
psi.WorkingDirectory = strWD;

psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = false;
psi.UseShellExecute = true;
Process proc = new Process();
try
{
Process.Start(psi);
blnOK = true;
}
catch (Exception e)
{
strErr = e.Message;
blnOK = false;
}
return blnOK;
}
 
Z

zlf

It works! Thank you :)

Jeff Gaines said:
If you call Process.Start using just a document name the default
application (assuming there is one) will open. I use:

public static bool StartProcess(string strCommand, string strWD, out
string strErr)
{
bool blnOK = false;
ProcessStartInfo psi = new ProcessStartInfo();
strErr = "";

// Initialize the ProcessStartInfo structure
psi.FileName = strCommand;
psi.WorkingDirectory = strWD;

psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = false;
psi.UseShellExecute = true;
Process proc = new Process();
try
{
Process.Start(psi);
blnOK = true;
}
catch (Exception e)
{
strErr = e.Message;
blnOK = false;
}
return blnOK;
}
 

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