首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过单击关联文件打开C# Winforms应用程序

通过单击关联文件打开C# Winforms应用程序
EN

Stack Overflow用户
提问于 2021-01-15 12:49:39
回答 1查看 107关注 0票数 0

我正在开发一个小应用程序,它可以接受两种不同类型的文件(*.miz和*.5js)。有两个函数可以解析这些文件,当从按钮单击事件触发时,它们已经正常工作了(参见下面)。此外,解决方案包含此项目和一个安装项目,以创建应用程序的安装程序。

代码语言:javascript
复制
public void button1_Click(object sender, EventArgs e)
    {
        button1.BackColor = Color.Red;

        openFileDialog1.InitialDirectory = "";
        openFileDialog1.Title = "Load mission file";
        openFileDialog1.ShowDialog();
        textBox4.Text = openFileDialog1.FileName;

        processMizFile(openFileDialog1.FileName);

        button1.BackColor = SystemColors.Control;
    }

代码语言:javascript
复制
        private void button6_Click(object sender, EventArgs e)
    {
        button6.BackColor = Color.Red;

        openFileDialog2.InitialDirectory = "";
        openFileDialog2.Title = "Load standalone Datacards";
        openFileDialog2.ShowDialog();

        loadDatacard(openFileDialog2.FileName);

        button6.BackColor = SystemColors.Control;
    }

现在,我试图在启动应用程序时调用这些函数,方法是双击其中一个文件类型(通过Windows属性对话框与我的应用程序关联)。为此,我运行以下代码:

代码语言:javascript
复制
public Form1()
    {
        LoadFont();
        InitializeComponent();


        string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();//to display program version in the form
        label29.Text += version;

        string[] cmdl = Environment.GetCommandLineArgs();

        using (StreamWriter sw = File.CreateText(Application.UserAppDataPath + @"\log.txt"))
        {
            if (cmdl.Length > 1)
            {
                sw.WriteLine("Argument 0 - Exe path: " + cmdl[0]);
                sw.WriteLine("Argument 1: " + cmdl[1]);
                sw.WriteLine("File type: " + Path.GetExtension(cmdl[1]).ToUpper());
            }
            else
            {
                sw.WriteLine("Argument 0 - Exe path: " + cmdl[0]);
                sw.WriteLine("No further arguments provided");
            }
        }

        if (cmdl.Length > 1)
        {
            if (Path.GetExtension(cmdl[1]).ToUpper() == ".MIZ")
            {
                processMizFile(cmdl[1]);
                using (StreamWriter sw2 = File.CreateText(Application.UserAppDataPath + @"\log2.txt"))
                {
                    sw2.WriteLine("MIZ file loaded!");
                }
            }
            else if (Path.GetExtension(cmdl[1]).ToUpper() == ".5JS")
            {
                loadDatacard(cmdl[1]);
                using (StreamWriter sw2 = File.CreateText(Application.UserAppDataPath + @"\log2.txt"))
                {
                    sw2.WriteLine("5Js file loaded!");
                }
            }
        }
    }

流器部分可以有一些跟踪,因为我不确定如何调试,否则。

现在,问题是在安装了Visual的同一台膝上型计算机上,所有这些都能正常工作。但是,它不能在另一台计算机上工作:

  1. 版本构建

代码语言:javascript
复制
- If I just double click one of the associated files(on the Desktop), the splash screen flashes quickly, and goes away. No log file is written
代码语言:javascript
复制
- If I double click an associated file in the same path as the installed executable, it works fine
代码语言:javascript
复制
- If I launch the app from command line, passing a path as argument, I get:

log.txt: Argument 0 - Exe path: DatacardGenerator.exe Argument 1: C:\Users\Username\OneDrive\Desktop\Datacards.5js File type: .5JS

log3.txt:写入“已创建的路径以提取dir:”,并将右Appdata文件夹写入临时提取

代码语言:javascript
复制
using (StreamWriter sw3 = File.AppendText(Application.UserAppDataPath + @"\log3.txt"))
        {
            sw3.WriteLine("Created path to extract dir: " + extract5Js);
        }

        if (Directory.Exists(extract5Js))
        {
            System.IO.Directory.Delete(extract5Js, true);
        }
        using (StreamWriter sw3 = File.AppendText(Application.UserAppDataPath + @"\log3.txt"))
        {
            sw3.WriteLine("Proceeding to unzip...");
        }
        
        ZipFile.ExtractToDirectory(path5jscard, extract5Js);

那么,当我的应用程序由外部文件启动时,我怎么知道在启动过程中会发生什么呢?我已经在调试模式下将命令行参数设置为测试,但考虑到目标计算机上的不同结果,我不确定它们是否带有完整路径(如果与exe位于同一个目录中的文件不能正常工作)

谢谢支持!)

P.S:我不直接在Main()中处理命令行参数,因为我使用了两种不同的形式: Form2是一个启动屏幕,Form1是主程序。为此,我用了一个我不久前发现的例子,就是这样:

代码语言:javascript
复制
namespace WindowsFormsApp2{    
static class Program{
    static private List<PrivateFontCollection> _fontCollections;
    

    [STAThread]
    static void Main(string[] args)
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);

        CultureInfo culture;
        culture = CultureInfo.CreateSpecificCulture("en-US");
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;


        //Dispose all used PrivateFontCollections when exiting
        Application.ApplicationExit += delegate {
            if (_fontCollections != null)
            {
                foreach (var fc in _fontCollections) if (fc != null) fc.Dispose();
                _fontCollections = null;
            }
            string appPath = Application.UserAppDataPath;
            string parentPath = System.IO.Directory.GetParent(appPath).ToString();
            
            System.IO.Directory.Delete(parentPath, true);
        };

        //Application.Run(new Form1());
        new MyApp().Run(args);
    }

}
public class MyApp : WindowsFormsApplicationBase
{
    
    protected override void OnCreateSplashScreen()
    {
        this.SplashScreen = new Form2();
    }
    protected override void OnCreateMainForm()
    {
        // Do your initialization here
        //...
        // Then create the main form, the splash screen will automatically close
        this.MainForm = new Form1();
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2021-01-19 08:48:12

解决了!

我只需在Main中声明一个全局变量,就可以从我的表单中访问它,比如:

代码语言:javascript
复制
public static string[] cmdlArgs;
    

    [STAThread]
    static void Main(string[] args)
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);

        CultureInfo culture;
        culture = CultureInfo.CreateSpecificCulture("en-US");
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        cmdlArgs = Environment.GetCommandLineArgs();

然后可以使用命名空间访问它:

代码语言:javascript
复制
Program.cmdlArgs
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65736431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档