У меня особая проблема с открытием файла в связанной с ним программе. Сначала я дважды щелкаю файл, нажимаю «Открыть с помощью…», затем щелкаю по папке «Отладка» в файле проекта моей программы и запускаю исполняемый файл. Это имитирует открытие файла в связанной с ним программе, как если бы эта программа была действительно установлена на моем компьютере.
Вот весь код из 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), файл открывается без проблем. Но когда я пытаюсь открыть файл, дважды щелкнув его и открыв с помощью исполняемого файла в папке отладки программы, я вижу заставку программы, а затем программа прерывается. Это
мне кажется, что путь к файлу правильно передается в «OpenTheFile ()». А поскольку программа работает вне Visual Studio, я не получаю сообщений об ошибках даже от операционной системы.
AutoOpenFile
? - person Guffa   schedule 31.01.2015FileShare.Read
к этой строкеFileStream fs = new FileStream(ThisFilePath, FileMode.Open, FileAccess.Read);
- person MethodMan   schedule 31.01.2015try
catch
сAutoOpenFile(filepath);
и получить информацию об исключении (в файл или окно сообщения). - person Nadia Chibrikova   schedule 31.01.2015