[問題] 找不到檔案?
最近有個問題一直想不出來,想請版上強力的大大可以幫助我^^"
我的問題是,按下listbox裡面的item之後,我的picturebox會顯示出我所按下的檔案
但是我按下listbox的檔案之後,一直出現FileNotFoundException的例外
後來有爬文但還是搞不太懂,應該是相對路徑的問題。
程式碼:
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ddDialog = new OpenFileDialog();
string assemblyLocation = Assembly.GetExecutingAssembly().Location;
string dd_relativePath = Path.Combine(assemblyLocation, "../../../../Images");
string contentPath = Path.GetFileName(dd_relativePath);
ddDialog.InitialDirectory = contentPath;
ddDialog.Title = "pic_load";
ddDialog.Filter = "Picture Files (*.bmp;*.jpg)|*.bmp;*.jpg|" +
"BMP Files (*.bmp)|*.bmp|" +
"JPG Files (*.jpg)|*.jpg|" +
"All Files (*.*)|*.*";
if (ddDialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(ddDialog.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
listBox2.Items.Add(Path.GetFileName(ddDialog.FileName));
System.IO.Path.GetFileName(ddDialog.FileName);
}
}
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((ListBox)sender).SelectedItem.ToString());
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
} 上面這段程式碼會出現FileNotFoundException的例外。
如果是相對路徑的問題,那是我所指定的位置出錯嗎?
"../../../../Images" ← ../ 與 ./ 的差別在那?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.204.77.217
※ 編輯: party100046 來自: 123.204.77.217 (08/06 12:48)
※ 編輯: party100046 來自: 123.204.77.217 (08/06 14:08)
→
08/06 18:21, , 1F
08/06 18:21, 1F
→
08/06 18:21, , 2F
08/06 18:21, 2F
→
08/06 18:21, , 3F
08/06 18:21, 3F
推
08/06 19:00, , 4F
08/06 19:00, 4F
→
08/06 19:25, , 5F
08/06 19:25, 5F
→
08/06 19:25, , 6F
08/06 19:25, 6F
→
08/07 16:45, , 7F
08/07 16:45, 7F
→
08/07 16:46, , 8F
08/07 16:46, 8F
→
08/07 16:46, , 9F
08/07 16:46, 9F
→
08/07 16:47, , 10F
08/07 16:47, 10F
→
08/07 16:47, , 11F
08/07 16:47, 11F
→
08/08 12:25, , 12F
08/08 12:25, 12F
→
08/08 16:37, , 13F
08/08 16:37, 13F