c# - 程序在调用 InitializeComponent() 时崩溃

标签 c# winforms

好的。这个有点傻。

我正在为 Access 数据库编写前端代码(我知道,重新发明轮子)。这个前端很简单。个人可以添加新条目或搜索现有条目。这两个都工作正常。该程序是 self 更新的,但非常简单。它比较已执行程序的当前文件版本(不是程序集),并将自身与远程驱动器版本进行比较,以查看哪个版本更新。如果远程驱动器的可执行文件较新,我会生成一个批处理文件来更新脚本,方法是杀死当前程序,将较新的程序复制到与旧程序相同的目录,然后自动打开新程序。每次用户打开程序时都会进行此更新检查。

我不确定这是否相关,但我最近一直在编辑该过程以在 XP 和 7 上工作。因此,我需要修改它以适应文件路径(文档和设置 - XP)中的空格。一切都很好,花花公子。

突然(似乎),程序开始在我的测试 XP 机器上不断崩溃(在 2 台不同的 XP 机器上验证)。我的个人计算机运行 7,并且完全按预期运行,包括更新,一切正常。

XP 上的崩溃是在我的主页窗体的 InitializeComponent() 中发生的未处理异常。我的表单的快速布局如下:

  • BaseForm.cs - 主窗体/父窗体。 .exe 时启动的第一个窗体 打开。没有 GUI,只存在于调用其他子项。
  • Home.cs - child 的基础形式。包含主页 GUI 和按钮以调用其他形式,例如 添加/搜索条目。

我们不会进入其他表单,因为它会在加载 Home.cs 表单之前崩溃。

我已将其缩小到在 Home 的 InitializeComponent() 中崩溃,在以下构造函数中:

public Home(string DB)
    {
        InitializeComponent();
        database = DB;
        lblVersion.Text = "Version: " + FileVersionInfo.GetVersionInfo(Directory.GetCurrentDirectory() + "\\KnowledgeBase.exe").ProductVersion;
    }

并带有以下消息。抱歉有颗粒感...截图截图: 我的 rep 不到 10,不会打出错误:

enter image description here

这是我的 InitializeComponent()

private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Home));
        this.btnNewEntry = new System.Windows.Forms.Button();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.btnSearch = new System.Windows.Forms.Button();
        this.lblVersion = new System.Windows.Forms.Label();
        this.linklblChangelog = new System.Windows.Forms.LinkLabel();
        this.button1 = new System.Windows.Forms.Button();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // btnNewEntry
        // 
        this.btnNewEntry.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.btnNewEntry.Location = new System.Drawing.Point(391, 407);
        this.btnNewEntry.Name = "btnNewEntry";
        this.btnNewEntry.Size = new System.Drawing.Size(142, 43);
        this.btnNewEntry.TabIndex = 0;
        this.btnNewEntry.Text = "Add new entry";
        this.btnNewEntry.UseVisualStyleBackColor = true;
        this.btnNewEntry.Click += new System.EventHandler(this.btnNewEntry_Click);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label1.Location = new System.Drawing.Point(184, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(349, 31);
        this.label1.TabIndex = 1;
        this.label1.Text = "Nationwide Knowledgebase";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.label2.Location = new System.Drawing.Point(280, 41);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(147, 26);
        this.label2.TabIndex = 2;
        this.label2.Text = "RADS Edition";
        // 
        // btnSearch
        // 
        this.btnSearch.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.btnSearch.Location = new System.Drawing.Point(190, 407);
        this.btnSearch.Name = "btnSearch";
        this.btnSearch.Size = new System.Drawing.Size(142, 43);
        this.btnSearch.TabIndex = 3;
        this.btnSearch.Text = "Search database";
        this.btnSearch.UseVisualStyleBackColor = true;
        this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
        // 
        // lblVersion
        // 
        this.lblVersion.AutoSize = true;
        this.lblVersion.Location = new System.Drawing.Point(3, 485);
        this.lblVersion.Name = "lblVersion";
        this.lblVersion.Size = new System.Drawing.Size(64, 13);
        this.lblVersion.TabIndex = 4;
        this.lblVersion.Text = "Version: null";
        // 
        // linklblChangelog
        // 
        this.linklblChangelog.AutoSize = true;
        this.linklblChangelog.Location = new System.Drawing.Point(96, 485);
        this.linklblChangelog.Name = "linklblChangelog";
        this.linklblChangelog.Size = new System.Drawing.Size(58, 13);
        this.linklblChangelog.TabIndex = 5;
        this.linklblChangelog.TabStop = true;
        this.linklblChangelog.Text = "Changelog";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(640, 480);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 7;
        this.button1.Text = "test";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Visible = false;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // pictureBox1
        // 
        this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
        this.pictureBox1.Location = new System.Drawing.Point(232, 85);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(247, 256);
        this.pictureBox1.TabIndex = 8;
        this.pictureBox1.TabStop = false;
        // 
        // Home
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
        this.ClientSize = new System.Drawing.Size(727, 512);
        this.Controls.Add(this.pictureBox1);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.linklblChangelog);
        this.Controls.Add(this.lblVersion);
        this.Controls.Add(this.btnSearch);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.btnNewEntry);
        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        this.Name = "Home";
        this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
        this.Text = "Home";
        this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Home_FormClosed);
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

为什么它仅在 XP 上在 InitializeComponent() 上崩溃?为什么它刚刚开始这样做?该领域前几周的开发进展顺利。

我试过删除整个 Update() 过程,但这并没有改变任何东西。

感谢阅读。

更新:Jon Skeet 建议我使用的图标会导致程序崩溃。事实证明确实如此。删除图标可以让表单在 XP 上正常工作(同样,在 7 上从来都不是问题)。更有趣的是,这个问题等了 2 周才出现,因为我在开发中使用这个图标已经很长时间了。稍后我会分析它发生的原因,但与此同时我还有更多代码要编写。

谢谢大家

最佳答案

鉴于堆栈跟踪,我怀疑这是问题所在:

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

我会通过删除图标来确认这一点,看看是否能解决问题。假设是这样,请查看图标的格式 - 我想知道它是否以某种方式以 XP 无法处理的方式损坏,或者它使用的图像格式比 XP 支持的更新。

当然,如果该图标不重要,您可以将其完全省略 :) 尽管如此,我还是会尝试诊断它发生了什么...

关于c# - 程序在调用 InitializeComponent() 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17618217/

相关文章:

c# - 垃圾收集 - 一个有效但另一个无效,怎么会这样?

javascript - 从 Javascript 后面的代码获取 C# 结构列表的字段

c# - 从 WinForms 应用程序发送带有附件的电子邮件?

c# - 检测窗体何时关闭 C#

c# - 使用 Properties.Default.Settings 时,应用程序会在 namespace 中创建 "_"的文件夹

c# - 使用 block 吞噬异常

C# WPF - 如何始终从文本框中获取当前文本?

c# - 当我在程序启动后复制 DLL 时,运行时加载程序集失败

c# - 从 winform 使用 URI 启动应用程序

c# - 带有字典数据源的列表框显示完整的 KeyValuePair 而不仅仅是值