有一个程序,要求按一个按钮后,从电脑中选择一个图片,并且把它复制到目标文件夹中。
目标文件夹是:p1\p1\Resources\photo\student\(1)班
debug的文件夹是:p1\p1\bin\Debug
使用以下程序: private void buttonPic_Click(object sender, EventArgs e)
{
OpenFileDialog PicLoadDlg = new OpenFileDialog();
PicLoadDlg.Title = "选择照片";
PicLoadDlg.Filter = "JPEG图像文件(*.jpg)|*.jpg";
if (PicLoadDlg.ShowDialog() == DialogResult.OK)
{
string SrcPach = PicLoadDlg.FileName;
string Guid = System.Guid.NewGuid().ToString().ToUpper();
string ExtName = System.IO.Path.GetExtension(PicLoadDlg.FileName);
string PhotoFile =Guid + ExtName;
string DestPath = AppDomain.CurrentDomain.BaseDirectory + "\\..\\..\\..\\Resources\\photo\\student\\(1)班\\" + PhotoFile;
System.IO.File.Copy(SrcPach, DestPath);
}
}程序运行后,在System.IO.File.Copy处老是出错,DestPath有问题。
请问如何使用相对路径来复制图片?
目标文件夹是:p1\p1\Resources\photo\student\(1)班
debug的文件夹是:p1\p1\bin\Debug
使用以下程序: private void buttonPic_Click(object sender, EventArgs e)
{
OpenFileDialog PicLoadDlg = new OpenFileDialog();
PicLoadDlg.Title = "选择照片";
PicLoadDlg.Filter = "JPEG图像文件(*.jpg)|*.jpg";
if (PicLoadDlg.ShowDialog() == DialogResult.OK)
{
string SrcPach = PicLoadDlg.FileName;
string Guid = System.Guid.NewGuid().ToString().ToUpper();
string ExtName = System.IO.Path.GetExtension(PicLoadDlg.FileName);
string PhotoFile =Guid + ExtName;
string DestPath = AppDomain.CurrentDomain.BaseDirectory + "\\..\\..\\..\\Resources\\photo\\student\\(1)班\\" + PhotoFile;
System.IO.File.Copy(SrcPach, DestPath);
}
}程序运行后,在System.IO.File.Copy处老是出错,DestPath有问题。
请问如何使用相对路径来复制图片?
你的目标文件夹”Resources“必须放在生成的目录里...才能操作该文件夹.
string DestPath = info.Parent.Parent.FullName +"\\Resources\\photo\\student\\(1)班\\" + PhotoFile;