hi all, how can i start any application from c sharp
i mean for example, if i have an openfiledialog and the user opened it and selected any file and opened it, i need this file to opened in its application whatever its extension and its default start application.
i have googled and found that Process.Start takes the file name and its application but i don't know what is the type of the file the user is going o open
thanks in advance for any replies.

Dani AI

Generated

Short, practical follow‑up that fills gaps left by the replies from , @ICode and .

The simplest working approach is to let the OS pick the program for a document (that’s what the OP reported). On modern .NET there’s one important detail to watch: ProcessStartInfo.UseShellExecute controls whether the shell is used to open documents. On .NET Framework its default was true; on .NET Core / .NET 5+ the default is false, so you must opt in (set UseShellExecute = true) to get the file‑association behavior. See the official notes on the behavior and the property for details. (learn.microsoft.com)

A minimal robust pattern (C#) is to build a ProcessStartInfo, enable shell execute, then start and handle failures:

var psi = new ProcessStartInfo {
    FileName = filePath,   // document or exe
    UseShellExecute = true,
    Verb = "open"          // optional; uses the registered action
};
try {
    Process.Start(psi);
} catch (System.ComponentModel.Win32Exception ex) {
    // handle common cases: no association / permission denied / user canceled elevation
    // fallback: open with a known program or show an explanatory message
}

Process.Start can throw Win32Exception for shell errors (for example Windows ERROR_NO_ASSOCIATION = 1155 if there is no registered app for the extension), so catch and provide a sensible fallback. Also remember that UseShellExecute=false is required when you need to redirect stdio. (learn.microsoft.com)

Quick troubleshooting checklist:

  • If code works locally but fails on a server or service, that environment may not have an interactive shell or client associations — starting a process on the client from server code won’t do what you expect.
  • If you need to force a particular app (media player, editor), set ProcessStartInfo.FileName to that app and pass the file path as Arguments (quote paths).
  • If you call shell actions from background threads and rely on shell verbs, respect STA requirements. (learn.microsoft.com)

These additions explain when the simple approach succeeds and what to change when it doesn’t.

Recommended Answers

All 4 Replies

Process.start() will run an application. But if you pass to it a document. like a .jpg it will open in whatever its default application is. for example passing "" would open your default web browser and navigate it to daniweb.

if you want to control the application that opens said doument, you can create a process object, and set its startinfo property to the document, and pass the program to run it in into its start method.

System.Diagnostics.Process.Start() will open anything(thats valid), in its default application.

but if u want to know the type of the file, u will have to get the extention of the file -- System.IO.Path.GetExtension(string Path)

.doc = document
.mp3 = ....
well u can figure out what im sayin

hi all, how can i start any application from c sharp
i mean for example, if i have an openfiledialog and the user opened it and selected any file and opened it, i need this file to opened in its application whatever its extension and its default start application.
i have googled and found that Process.Start takes the file name and its application but i don't know what is the type of the file the user is going o open
thanks in advance for any replies.

hi...
first of all in proces.start method you need to pass two parameter
like you want to open IE then
process.start("iexplorer.exe","www.google.com");
it will run internet explorer and default page is google...

now according to your requirement you need to set filters for you application....
like which kind of files you want user to play....
like if jpg,jpeg,png,bmp...etc then windowfax viewer....
if .wmv,mpg,mpeg,flv,3gp..etc then for instace vlc...

using flter you get the extension of file selected...and using if condition you can play that.....
ex.. if(path.getExtension(openfiledialog1.filename)==".mpg")
process.start("vlc.exe",openfiledialog1.filename);

then problem solved

Thanks alot fro reply
i tried System.Diagnostics.Process.Start(fileName); and it worked perfect

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.