c#中未将对象引用到对象实例的问题
这段代码的作用是想打开一个浏览文件的对话框,选择文件上传,不知道哪里出错了,只要一点浏览这个图标就会出现未将对象引用到对象实例的错误。#region点击图片浏览priva...
这段代码的作用是想打开一个浏览文件的对话框,选择文件上传,不知道哪里出错了,只要一点浏览这个图标就会出现未将对象引用到对象实例的错误。
#region 点击图片浏览 private void button11_Click(object sender, EventArgs e)
{ openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); openFileDialog.Filter = "图片文件 (*.jpg,*.png,*.bmp,*.gif,*.jpeg)|"/*|*.jpg|*.png|*.bmp|*.gif|*.jpeg"*/; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK) { try { string path = this.openFileDialog.FileName; textBox3.Text = this.openFileDialog.FileName; pictureBox1.Image = Image.FromFile(path); pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } } #endregion 展开
#region 点击图片浏览 private void button11_Click(object sender, EventArgs e)
{ openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); openFileDialog.Filter = "图片文件 (*.jpg,*.png,*.bmp,*.gif,*.jpeg)|"/*|*.jpg|*.png|*.bmp|*.gif|*.jpeg"*/; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK) { try { string path = this.openFileDialog.FileName; textBox3.Text = this.openFileDialog.FileName; pictureBox1.Image = Image.FromFile(path); pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } } #endregion 展开
1个回答
展开全部
问题就出在这里 openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
你想给浏览对话框一个默认的打开目录,但这里报错,所以看看你的Environment.GetFolderPath(Environment.SpecialFolder.Personal);
这句话返回的到底是什么?是不是一个有效的存在的目录,如果不存在那就有问题了啊,或者你先把目录写死看报错不!比如openFileDialog.InitialDirectory=“D:\\新建文件夹”
你想给浏览对话框一个默认的打开目录,但这里报错,所以看看你的Environment.GetFolderPath(Environment.SpecialFolder.Personal);
这句话返回的到底是什么?是不是一个有效的存在的目录,如果不存在那就有问题了啊,或者你先把目录写死看报错不!比如openFileDialog.InitialDirectory=“D:\\新建文件夹”
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询