好的。这个有点傻。
我正在为 Access 数据库编写前端(我知道,重新发明轮子)。这个前端很简单。个人可以添加新条目,也可以搜索现有条目。两者都工作正常。该程序是自我更新的,但非常简单。它比较所执行程序的当前文件版本(不是程序集),并将自身与远程驱动器版本进行引用以查看哪个版本更新。如果远程驱动器的可执行文件较新,我会生成一个批处理文件,通过终止当前程序、将较新的程序复制到与旧程序相同的目录并自动打开新程序来更新脚本。每次用户打开程序时都会进行此更新检查。
我不确定这是否相关,但我最近一直在编辑该进程以在 XP 和 7 上工作。因此,我需要修改它以适应文件路径中的空格(文档和设置 - XP)。一切都很好。
突然(看起来),程序开始在我的测试 XP 机器上持续崩溃(在 2 台不同的 XP 机器上进行了验证)。我的个人机器正在运行 7,并且完全按照预期运行,包括更新,都很好。
XP 上的崩溃是我的 Home 窗体的 InitializeComponent() 处未处理的异常。我的表单的快速布局如下:
我们不会进入其他表单,因为它在加载 Home.cs 表单之前崩溃。
我已将其范围缩小到在 Home 的 InitializeComponent() 中崩溃,在以下构造函数中:
public Home(string DB)
{
InitializeComponent();
database = DB;
lblVersion.Text = "Version: " + FileVersionInfo.GetVersionInfo(Directory.GetCurrentDirectory() + "\\KnowledgeBase.exe").ProductVersion;
}
Run Code Online (Sandbox Code Playgroud)
并带有以下消息。抱歉出现颗粒感...屏幕截图的屏幕截图:我的代表数少于 10,并且不会输入错误:

这是我的 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();
}
Run Code Online (Sandbox Code Playgroud)
为什么它仅在 XP 上的 InitializeComponent() 上崩溃?为什么它刚刚开始这样做?该领域最初几周的开发进展顺利。
我尝试删除整个 Update() 过程,但这并没有改变任何东西。
谢谢阅读。
更新:乔恩·斯基特(Jon Skeet)建议我使用的图标导致程序崩溃。事实证明确实如此。删除图标可以让表单在 XP 上正常工作(同样,在 7 上从来都不是问题)。更有趣的是,这个问题等了两周才显现出来,因为我在开发中使用这个图标已经很长时间了。我稍后会解释它发生的原因,但同时我还有更多代码要编写。
感谢大家。
鉴于堆栈跟踪,我怀疑这就是问题所在:
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
Run Code Online (Sandbox Code Playgroud)
我会通过删除图标来确认这一点,看看是否可以解决问题。假设确实如此,请查看图标的格式 - 我想知道它是否以某种方式以 XP 无法处理的方式损坏,或者它使用了比 XP 支持的某些更新的图像格式。
当然,如果图标不重要,您可以完全忽略它:)不过,我会尝试诊断它发生了什么......