在ASP.NET中将文件从一个目录复制到另一个目录时出现错误

nwnhqdif  于 5个月前  发布在  .NET
关注(0)|答案(1)|浏览(77)
string tempKYCFolderPath = "~/Attachments/KYC/KYCTemp/";
string zipfolder = tempKYCFolderPath + "Userdata/";

DirectoryInfo tempPoposalFolder = new DirectoryInfo(HostingEnvironment.MapPath(zipfolder));

if (!tempPoposalFolder.Exists)
{
    tempPoposalFolder.Create();
}

string tempUserFolderPath = Path.Combine(tempPoposalFolder.FullName, dr["User Id"] + "_" + dr["Name"].ToString().Trim());
string fileNameAppend = "kyc_" + dr["User Id"].ToString();

DirectoryInfo tempUserFolder = new DirectoryInfo(tempUserFolderPath);

if (!tempUserFolder.Exists)
{
    tempUserFolder.Create();
}

if (dr["PassportFile"] != DBNull.Value)
{
    FileInfo file = new FileInfo(HostingEnvironment.MapPath(Path.Combine(tempKYCFolderPath, dr["PassportFile"].ToString())));

    if (file != null && file.Exists)
    {
        file.CopyTo(HostingEnvironment.MapPath(Path.Combine(tempUserFolderPath , fileNameAppend + "PassportFile" + file.Extension)), true);
    }
}

字符串
我试图创建一个文件夹,并在另一个文件夹内,然后从一个位置复制文件,并希望将其粘贴到第二个文件夹内,但我得到一个错误
System.ArgumentException
HResult=0x80070057
Message=此处不允许使用相对虚拟路径“C:/eureeca/solution_Ap3/KYC/KYCTemp/Userdata/7_aseel amas amassi/kyc_7PassportFile.jpg”。
来源=App_Web_jtqcblog
StackTrace:
在Crowd_funding_pages_Admin_UserNiNList.ExporttoCsv(String userid)在c:\eureeca\solution_Ap3\Crowd-funding-pages\Admin\UserNiNList.aspx.cs:行139
此异常最初在此调用堆栈中引发:
[外部代码]
UserNiNList.aspx.cs中的Crowd_funding_pages_Admin_UserNiNList.ExporttoCsv(字符串)”

lskq00tm

lskq00tm1#

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                string tempKYCFolderPath = Path.Combine(baseDirectory, "Attachments", "KYC", "KYCTemp");
string zipfolder = Path.Combine(tempKYCFolderPath, "Userdata-" + DateTime.Now.ToString("dd-MM-yyyy"));
 Directory.CreateDirectory(zipfolder);
                DirectoryInfo tempPoposalFolder = new DirectoryInfo(zipfolder);

字符串
就像如果你不知道项目存储在你的PC中的位置,那么你使用第一行来获取目录,这将给予你项目的物理路径,所以不需要转换它,直接使用它

相关问题