Wednesday, July 18, 2007

How to Launch application in Visual Studio 2005?

In VB6, I was using this ShellExecute API to launch an external application, such as opening a Word document.

I was searching around, and finally I found this method, and it's so simple.

Just use System.Diagnostics.Process.Start() to launch the application.

string filename = Application.StartupPath + "\\Tutorials\\DataGridViewTutorial01.doc";
System.Diagnostics.Process.Start(filename);

 

As for the Application.StartupPath, it actually refers to the "bin\Debug" folder. (It depends on the build configuration, it refers to "bin\Release" folder if the build configuration is set to "Release".) In order to get the filename correctly, I've made some changes on the document properties.



1. Add the document to the project.

2. Select the document and look for its properties.

3. There are three options for the "Copy to Output Directory" property: Do not copy, Copy always, and Copy if newer. You may select "Copy always" or "Copy if newer", so that whenever you build your project, the latest version will be copied to the output directory. And Application.StartupPath is actually referring to this output directory.


2 comments:

  1. thank you for the bit about how to get a resource to launch using an app. I was struggling to find how to get its path.

    Thanks,

    -Mike

    ReplyDelete
  2. Hi Mike,

    I'm glad that this method helped you. I struggled until I found the method too. ^_^

    ReplyDelete