How to Launch Files in C#

Open file with associated application

Just write

System.Diagnostics.Process.Start(@"file path");

example

System.Diagnostics.Process.Start(@"C:\foo.jpg");
System.Diagnostics.Process.Start(@"C:\foo.doc");
System.Diagnostics.Process.Start(@"C:\foo.dxf");
...

And shell will run associated program reading it from the registry, like usual double click does.

C# Click event to open file dialogue, launch files in photoshop and run an action

Provided you use .NET 4 or later, you can declare PhotoshopInst as dynamic:

dynamic PhotoshopInst = Activator.CreateInstance(PhotoshopType);

Then you will be able to call methods you know the underlying type supports without the compiler complaining.

PhotoshopInst.Open(fl);

Opening a File from Windows Application C#

Try this:

System.Diagnostics.Process.Start("yourFilePath");

How can I run an EXE file from my C# code?

using System.Diagnostics;

class Program
{
static void Main()
{
Process.Start("C:\\");
}
}

If your application needs cmd arguments, use something like this:

using System.Diagnostics;

class Program
{
static void Main()
{
LaunchCommandLineApp();
}

/// <summary>
/// Launch the application with some options set.
/// </summary>
static void LaunchCommandLineApp()
{
// For the example
const string ex1 = "C:\\";
const string ex2 = "C:\\Dir";

// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
}

c# open file with default application and parameters

If you want the file to be opened with the default application, I mean without specifying Acrobat or Reader, you can't open the file in the specified page.

On the other hand, if you are Ok with specifying Acrobat or Reader, keep reading:


You can do it without telling the full Acrobat path, like this:

using Process myProcess = new Process();    
myProcess.StartInfo.FileName = "acroRd32.exe"; //not the full application path
myProcess.StartInfo.Arguments = "/A \"page=2=OpenActions\" C:\\example.pdf";
myProcess.Start();

If you don't want the pdf to open with Reader but with Acrobat, chage the second line like this:

myProcess.StartInfo.FileName = "Acrobat.exe";

You can query the registry to identify the default application to open pdf files and then define FileName on your process's StartInfo accordingly.

Follow this question for details on doing that: Finding the default application for opening a particular file type on Windows

Opening a folder in explorer and selecting a file

Use this method:

Process.Start(String, String)

First argument is an application (explorer.exe), second method argument are arguments of the application you run.

For example:

in CMD:

explorer.exe -p

in C#:

Process.Start("explorer.exe", "-p")

Open file location

If openFileDialog_View is an OpenFileDialog then you'll just get a dialog prompting a user to open a file. I assume you want to actually open the location in explorer.

You would do this:

if (File.Exists(filePath))
{
Process.Start("explorer.exe", filePath);
}

To select a file explorer.exe takes a /select argument like this:

explorer.exe /select, <filelist>

I got this from an SO post: Opening a folder in explorer and selecting a file

So your code would be:

if (File.Exists(filePath))
{
Process.Start("explorer.exe", "/select, " + filePath);
}


Related Topics



Leave a reply



Submit