Call system application to open a specified file.

  • Thread starter Thread starter zlf
  • Start date Start date
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
 
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
 
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
 
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;
}
 
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;
}
 
Back
Top