【发布时间】:2015-01-30 22:42:19
【问题描述】:
我在相关程序中打开文件时遇到了一个特殊问题。首先,我双击一个文件,单击“打开方式...”,然后单击进入程序项目文件中的 Debug 文件夹并运行可执行文件。这是为了模拟在与之关联的程序中打开文件,就好像该程序实际安装在我的计算机上一样。
这是 Program.cs 的完整代码:
namespace TriviaAuthor_v10
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmSplashScreen());
if (args.Length > 0)
Application.Run(new frmMain(args[0]));
else
Application.Run(new frmMain());
}
}
}
下面是主窗体的两个构造函数的代码:
public frmMain(string autoopenfilepath)
{
InitializeComponent();
filepath = autoopenfilepath;
OpenTheFile(filepath);
}
public frmMain()
{
InitializeComponent();
}
这是打开文件的代码:
private void OpenTheFile(string ThisFilePath)
{
// First we get the filename.
filename = Path.GetFileName(ThisFilePath);
FilenameSansExtension = Path.GetFileNameWithoutExtension(ThisFilePath);
// Create a file stream.
FileStream fs = new FileStream(ThisFilePath, FileMode.Open, FileAccess.Read);
// Create the writer for data.
BinaryReader br = new BinaryReader(fs);
GameInfo.GameTitle = br.ReadString();
GameInfo.GameAuthor = br.ReadString();
GameInfo.DateCreated = br.ReadString();
GameInfo.NumberOfQuestions = br.ReadInt32();
GameInfo.TitlePageImagePresent = br.ReadBoolean();
GameInfo.TitlePageImage = br.ReadString();
GameInfo.IntroScreenAudioPresent = br.ReadBoolean();
GameInfo.IntroScreenAudio = br.ReadString();
GameInfo.FinalScoreAudioPresent = br.ReadBoolean();
GameInfo.FinalScoreAudio = br.ReadString();
GameInfo.ActiveQuestion = br.ReadInt32();
if (GameInfo.NumberOfQuestions > 0)
{
for (int i = 0; i < GameInfo.NumberOfQuestions; i++)
{
clsQuestionClass Question = new clsQuestionClass();
Question.NewQuestion = br.ReadString();
Question.Points = br.ReadInt32();
Question.QuestionType = br.ReadInt32();
Question.QuestionImagePresent = br.ReadBoolean();
Question.QuestionImage = br.ReadString();
Question.QuestionAudioPresent = br.ReadBoolean();
Question.QuestionAudio = br.ReadString();
Question.IncludeTimer = br.ReadBoolean();
Question.TimerTime = br.ReadInt32();
Question.TickTock = br.ReadBoolean();
Question.AIsChecked = br.ReadBoolean();
Question.AnswerA = br.ReadString();
Question.AIsCorrect = br.ReadBoolean();
Question.BIsChecked = br.ReadBoolean();
Question.AnswerB = br.ReadString();
Question.BIsCorrect = br.ReadBoolean();
Question.CIsChecked = br.ReadBoolean();
Question.AnswerC = br.ReadString();
Question.CIsCorrect = br.ReadBoolean();
Question.DIsChecked = br.ReadBoolean();
Question.AnswerD = br.ReadString();
Question.DIsCorrect = br.ReadBoolean();
Question.TrueOrFalse = br.ReadBoolean();
Question.FillInBlankAnswer = br.ReadString();
Question.AnswerResponseImagePresent = br.ReadBoolean();
Question.AnswerResponseImage = br.ReadString(); ;
Question.CorrectAnswerResponse = br.ReadString();
Question.IncorrectAnswerResponse = br.ReadString();
Question.CorrectAnswerResponseAudioPresent = br.ReadBoolean();
Question.CorrectAnswerResponseAudio = br.ReadString();
Question.IncorrectAnswerResponseAudioPresent = br.ReadBoolean();
Question.IncorrectAnswerResponseAudio = br.ReadString();
Questions.Add(Question);
Questions.Count();
}
}
fs.Close();
br.Close();
QuestionIndex = GameInfo.ActiveQuestion;
LoadGameIntoGameGUI(Questions[QuestionIndex]);
this.Text = "Trivia Author v1.0 - " + FilenameSansExtension;
ProjectNeedsSaving = false;
saveAsToolStripMenuItem.Enabled = closeprojecttoolStripMenuItem1.Enabled = exportgametoolStripMenuItem.Enabled =
printToolStripMenuItem.Enabled = printPreviewToolStripMenuItem.Enabled = tsbtnProjectClose.Visible =
ProjectIsOpen = saveToolStripMenuItem.Enabled = tsbtnSaveProject.Enabled = btnShowProjectReview.Enabled = true;
UpdateGameSummary();
}
注意:“OpenTheFile(string ThisFilePath)”用于使用 OpenFileDialog 打开文件,以及当我尝试通过双击打开文件时。
所以问题来了:当我在 Visual Studio 2013 中运行程序然后打开文件(使用 OpenFileDialog)时,文件打开时没有问题。但是,当我尝试通过双击该文件并使用程序的 Debug 文件夹中的可执行文件打开它时,我看到程序的启动画面,然后程序中止。它
在我看来,文件的路径正在正确地中继到“OpenTheFile()”。而且由于程序在 Visual Studio 之外运行,我没有收到任何错误消息,甚至来自操作系统。
【问题讨论】:
-
AutoOpenFile方法有什么作用? -
当您单步执行代码时会发生什么...您是否看到任何立即突出的内容会引起对潜在问题的注意...?您是否尝试将
FileShare.Read添加到此行FileStream fs = new FileStream(ThisFilePath, FileMode.Open, FileAccess.Read); -
您可以使用
trycatch和AutoOpenFile(filepath);并获取有关异常的信息(进入文件或消息框)。 -
对不起,古法。我已经更新它,“AutoOpenFile()”现在是“OpenTheFile()”。
-
对不起,古法。我已对其进行了更新,因此“AutoOpenFile()”现在是“OpenTheFile()”。 MethodMan:添加 FileShare.Read 没有帮助。 Nadia:我试过 try-catch,但看起来程序在 MessageBox 有机会显示之前就崩溃了。
标签: c# winforms double-click file-association