Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I launch files in C#

Tags:

c#

.net

shell

People also ask

How can I open file in C?

Opening a file is performed using the fopen() function defined in the stdio. h header file. The syntax for opening a file in standard I/O is: ptr = fopen("fileopen","mode");

Which file opens automatically in C?

When a C program starts its execution the program automatically opens three standard streams named stdin , stdout , and stderr . These are attached for every C program.

How do you open and edit a file in C?

Syntax of fopen() function As, we have to open a file for writing, hence this mode will take a string value "r+". Searches for the file and opens the file(if the file is found). If the file is not found, NULL is returned and no new file is created. Allows you to read, write and modify the content of file..


Use:

System.Diagnostics.Process.Start(filePath);

It will use the default program that would be opened as if you just clicked on it. Admittedly it doesn't let you choose the program that will run... but assuming that you want to mimic the behaviour that would be used if the user were to double-click on the file, this should work just fine.


It really sounds like you're looking more for this:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "<whatever>";
proc.Start();

Assuming that you just want to launch files which already have some associated applications (eg: *.txt is associated with notepad), Use System.Diagnostics.Process.

e.g. :

 using System.Diagnostics;
    Process p = new Process();
    ProcessStartInfo pi = new ProcessStartInfo();
    pi.UseShellExecute = true;
    pi.FileName = @"MY_FILE_WITH_FULL_PATH.jpg";
    p.StartInfo = pi;

    try
    {
        p.Start();
    }
    catch (Exception Ex)
    {
        //MessageBox.Show(Ex.Message);
    }

Note: In my PC, the pic opens in Windows Picture & Fax Viewer since that is the default application for *.jpg files.