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

后端 未结 3 2021
傲寒
傲寒 2020-12-11 09:41

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 ex

3条回答
  •  猫巷女王i
    2020-12-11 10:04

    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
    

提交回复
热议问题