Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path+filename of file that was opened with my application

Tags:

c#

file

launching

I'm an amateur at c# and I've been unable to locate the answer to this. Perhaps I am not aware of the correct terms to use.

When a video file is dragged onto my exe application, I would like the application to know that it was launched with a file and be able to know the path and filename of that file. This way, the user does not have to use the file>open menu.

Hope that makes sense. Thanks

like image 348
Quantum_Kernel Avatar asked Oct 27 '25 20:10

Quantum_Kernel


1 Answers

You can check the command line arguments which were used to launch the application. If your application was started by dropping a file on the .exe file, there will be a single command line argument with the path of the file.

string[] args = System.Environment.GetCommandLineArgs();
if(args.Length == 1)
{
    // make sure it is a file and not some other command-line argument
    if(System.IO.File.Exists(args[0])
    {
        string filePath = args[0];
        // open file etc.
    }
}

As your question title states, you want the path and the file name. You can get the file name using:

System.IO.Path.GetFileName(filePath); // returns file.ext
like image 121
helb Avatar answered Oct 30 '25 12:10

helb