简体   繁体   English

文件名未传递给main

[英]Filename not being passed to main

I have an app and I have a file type associated with it. 我有一个应用程序,我有一个与之关联的文件类型。 When I doubleclick the file it opens my app but the filename/path never gets passed to my app. 当我双击该文件时,它会打开我的应用程序,但文件名/路径永远不会传递给我的应用程序。 It does work if I drag the file over the icon, however. 但是,如果我将文件拖到图标上,它确实有效。 Here is the main(): 这是main():

    static void Main(string[] filenames)
    {
        Form1 form = null;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        form = new Form1();
        if (filenames != null && filenames.Length > 0)
            form.FileName = filenames[0];
        Application.Run(form);
    }

filenames.length comes up 0 every time (unless I drag/drop the file onto the app) filenames.length每次都出现0(除非我将文件拖放到应用程序中)

Does your file association contain a %1 or "%L" as part of the command line? 您的文件关联是否包含%1或“%L”作为命令行的一部分? This is required to pass the file name to your application. 这是将文件名传递给应用程序所必需的。 ("%L" means the full, long file name, %1 is the shortened 8.3 file name.) (“%L”表示完整的长文件名,%1是缩短的8.3文件名。)

The reason your application does receive the file name when you drop the file on the application icon , is because Windows doesn't use a file association here. 将文件放在应用程序图标上时,应用程序确实收到文件名的原因是Windows在此处不使用文件关联。 In fact, it will work for any program, with and without file association. 事实上,它适用于任何程序,无论是否有文件关联。 In these cases it guesses that you probably want to start the application with the file's name as an argument. 在这些情况下,它猜测您可能希望以文件名作为参数启动应用程序。

You should try 你应该试试

AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData

is also is a string[] 也是一个字符串[]

You need to have %1 or %L in file association settings to "tell" Windows Explorer to pass filename to your application when double-clicked on the file. 您需要在文件关联设置中使用%1或%L来“告诉”Windows资源管理器在双击文件时将文件名传递给您的应用程序。 An example: 一个例子:
http://hypography.com/forums/tutorials-and-how-tos/15876-windows-file-association-tutorial.html http://hypography.com/forums/tutorials-and-how-tos/15876-windows-file-association-tutorial.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM