有一个程序,要求按一个按钮后,从电脑中选择一个图片,并且把它复制到目标文件夹中。
目标文件夹是: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有问题。
请问如何使用相对路径来复制图片?

解决方案 »

  1.   

    CS程序里不存在虚拟目录
    你的目标文件夹”Resources“必须放在生成的目录里...才能操作该文件夹.
      

  2.   

    DirectoryInfo info = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
    string DestPath = info.Parent.Parent.FullName +"\\Resources\\photo\\student\\(1)班\\" + PhotoFile;
      

  3.   

     string DestPath = AppDomain.CurrentDomain.BaseDirectory + "\\..\\..\\..\\Resources\\photo\\student\\(1)班\\" + PhotoFile; DestPath =System.IO.Path.GetFullPath(DestPath);
      

  4.   

    你可以把\Resources\photo\student\(1)班 放在与程序生成的.exe文件同一个目录下,然后用DestPath = Application.StartupPath + "\\Resources\\photo\\student\\(1)班\\" + PhotoFile; 试一下,我是这么做的。否则就只能写绝对路径了。