在编程方面我仍然是初学者,这是我按照 C# 教程完成的一个小应用程序。
private void viewImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
string openedfile = "";
openfd.Title = "Insert a text file";
openfd.InitialDirectory = "C:";
openfd.FileName = "";
openfd.Filter = "text files|*.txt|word documents|*.doc|allfiles|*.*";
if (openfd.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation canceled");
}
if (openfd.ShowDialog() != DialogResult.Cancel)
{
openedfile = openfd.FileName;
richTextBox1.LoadFile(openedfile,RichTextBoxStreamType.PlainText);
}
在执行此操作时,我注意到如果我更改同一个应用程序的代码顺序只需 2 行 -
string openedfile = "";
openedfile = openfd.FileName;
像下面这样调试时会抛出这样的错误 - 空路径名是不合法的。
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openfd.Title = "Insert a text file";
openfd.InitialDirectory = "C:";
openfd.FileName = "";
openfd.Filter = "text files|*.txt|word documents|*.doc|allfiles|*.*";
**string openedfile = "";
openedfile = openfd.FileName;**
if (openfd.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation canceled");
}
if (openfd.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.LoadFile(openedfile,RichTextBoxStreamType.PlainText);
}
没有办法理解这些类型情况下的错误。对这样的应用程序进行编码的具体顺序是什么?