[英]How do I correct the “DirectoryNotFoundException” in asp.net?
I'm working on a website with asp.net webforms Framework 4 and i want to import in my DB Sql Server a Excel File. 我正在使用asp.net webforms Framework 4的网站工作,并且想在我的DB Sql Server中导入一个Excel文件。 But when I upload this file, I get a DirectoryNotFoundException... i Don't get it because my folder exists...
但是当我上传此文件时,我得到了DirectoryNotFoundException ...我没有得到它,因为我的文件夹存在...
This is my code behind : 这是我的代码背后:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + FileName);
FileUpload1.SaveAs(FilePath);
GetExcelSheets(FilePath, Extension, "Yes");
}
}
And this is a part of my web.config : 这是我的web.config的一部分:
<appSettings>
<add key="FolderPath" value="Files/" />
</appSettings>
<connectionStrings>
<!--Ce qui va nous permettre d'importer des fichiers excel aec le format 2003 et 2007 dans SQL Server-->
<add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>
<!--Ce qui va nous permettre de se connecter à la base de données-->
<add name="connexionBase" providerName="System.Data.SqlClient" connectionString="server=localhost;database=projetDGCS;uid=sa;pwd=dgcs9876" />
</connectionStrings>
You can put the file name in the appsetting like this 您可以像这样将文件名放在appsetting中
<add key="FolderPath" value="~/Files/" />
bool isExists = System.IO.Directory.Exists(Server.MapPath(FolderPath));
if(!isExists)
System.IO.Directory.CreateDirectory(Server.MapPath(FolderPath));
在配置中尝试:
<add key="FolderPath" value="~/Files/" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.