【问题标题】:C# Application doesn't seem to run on a similar computer to mineC# 应用程序似乎无法在与我类似的计算机上运行
【发布时间】:2012-04-13 23:31:25
【问题描述】:

不幸的是,我还没有在搜索部分或任何其他编码论坛中找到我正在寻找的遮阳篷,所以我将把我的问题留在这里等待结束。

我开发了一个非常简单的 C# 应用程序,它是 Windows 的启动项目之一 - 迷宫项目,它使用带有标签的简单面板和简单的鼠标事件来触发指针位置的放置以重新开始。

我已经成功发布了我的应用程序,它可以在我的计算机和其他一些计算机上顺利运行,但由于某些奇怪的原因,它根本无法加载到我朋友的笔记本电脑上。

我们都共享相同的操作系统(Windows 7),我们都有 x64 版本,框架似乎相同,但即使进程显示在任务管理器中,它也不会加载,即使在安装成功。

所以,程序确实运行了,但似乎无法加载,也没有抛出任何异常或错误进行分析。

因此我的问题是,我的程序与其他计算机完全兼容的要求是什么?

感谢您的关注,我在这件事上花了很多时间,但似乎找不到正确的遮阳篷。

我还将显示我的表单代码以供进一步分析:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace Labirinto
    {
        public partial class frmLabirinto : Form
        {
            // Toca um som sempre que o utilizador bater numa parede
            System.Media.SoundPlayer startSoundPlayer = new System.Media.SoundPlayer(@"C:\Users\Ricardo Borges\Documents\Visual Studio 2010\Projects\Labirinto\Labirinto\Resources\doh.wav");
            // Toca um som sempre que o utilizador chegar ao final do labirinto
            System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"C:\Users\Ricardo Borges\Documents\Visual Studio 2010\Projects\Labirinto\Labirinto\Resources\tada.wav");


    public frmLabirinto()
    {
        InitializeComponent();
        MoveToStart();
    }

    private void frmLabirinto_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// O método permite que o ponteiro do rato volte ao ponto inicial
    /// </summary>
    private void MoveToStart()
    {
        startSoundPlayer.Play(); //Toca o som de reinicio do jogo
        Point startingPoint = panel1.Location; //ponto inicial
        startingPoint.Offset(10, 10); //localizacao do ponto inicial
        Cursor.Position = PointToScreen(startingPoint); //coloca o cursor no local inicial
    }

    private void finishLabel_MouseEnter(object sender, EventArgs e)
    {
        finishSoundPlayer.Play(); //Toca o som de fim de jogo
        // Congratula o utilizador através de uma mensagem no ecrã
        MessageBox.Show("Parabéns, encontrou a saída do labirinto");
        Close();
    }

    private void wall_MouseEnter(object sender, EventArgs e)
    {
        MoveToStart(); //recoloca o ponteiro no ponto inicial ao embater numa parede
    }


}

}

【问题讨论】:

  • startSoundPlayer 和 finishSoundPlayer 的值如何被硬编码.. 确保文件存在于您的好友系统中。
  • @Parv:+1。我什至没有检查代码!
  • 那么,如果程序被编译成exe,那是绝对必要的吗?那我应该如何以更合适的方式添加引用呢?
  • “好吧,如果程序被编译成 exe 是绝对必要的” - 该评论没有意义。如果机器上不存在该路径,那么您当然需要对其进行参数化,或者将任何资源编译到您的可执行文件中。
  • 您是否检查过受影响机器上的事件查看器以查看它是否有基于应用程序故障的条目?

标签: c# windows compatibility requirements


【解决方案1】:

我感觉初始化两个 SoundPlayer 对象时使用的硬编码值会导致错误。例如,如果运行应用程序的机器上没有名为“Ricardo Borges”的用户怎么办?

System.Media.SoundPlayer startSoundPlayer = new System.Media.SoundPlayer(@"C:\Users\Ricardo Borges\Documents\Visual Studio 2010\Projects\Labirinto\Labirinto\Resources\doh.wav"); 
System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"C:\Users\Ricardo Borges\Documents\Visual Studio 2010\Projects\Labirinto\Labirinto\Resources\tada.wav");

Based on the MSDN documentation 用于 SoundPlayer 对象 '如果路径或 URL 无效,SoundPlayer 仍将被构造,但随后对加载或播放方法的调用将失败'。

MoveToStart 函数的第一行包含以下行:

startSoundPlayer.Play(); 

参考 MSDN 的 SoundPlayer.Play 方法,它可以根据错误原因抛出三种不同异常之一 - FileNotFoundException 似乎是罪魁祸首。

您能否确认指定位置和实际文件都存在于有问题的机器上?

【讨论】:

  • 不,但实际上让我感到困惑的是,相同的构建也适用于其他机器,它们也不会有相同的路径。但我理解你的所有观点,我很感激你的努力。我如何才能真正替换该引用,以便在发布/构建时继续使用项目所需的其余文件?
  • @vuk77 您需要向应用程序添加一些日志记录(System.Diagnostics),特别是在它加载和初始化数据的区域。如果您可以捕获错误,那么它将帮助我们缩小原因。
【解决方案2】:

您是否尝试过使用Fusion Log viewer 来诊断任何程序集加载错误?

Using Fusion Log Viewer to Debug Obscure Loader Errors

如果这不是问题,请将日志记录代码添加到您的应用程序中,并检查您的代码中是否存在任何“吞噬”错误的 try/catch 块。

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2022-11-24
    • 2020-10-07
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2020-09-24
    相关资源
    最近更新 更多