C# 我如何纠正“错误”;DirectoryNotFoundException“;在asp.net中?
我正在使用asp.net webforms Framework 4开发一个网站,我想在我的DB Sql Server中导入一个Excel文件。但是当我上传这个文件时,我得到一个DirectoryNotFoundException。。。 我不明白,因为我的文件夹存在 这是我的代码隐藏:C# 我如何纠正“错误”;DirectoryNotFoundException“;在asp.net中?,c#,asp.net,C#,Asp.net,我正在使用asp.net webforms Framework 4开发一个网站,我想在我的DB Sql Server中导入一个Excel文件。但是当我上传这个文件时,我得到一个DirectoryNotFoundException。。。 我不明白,因为我的文件夹存在 这是我的代码隐藏: protected void btnUpload_Click(object sender, EventArgs e) { if (FileUpload1.HasFile)
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");
}
}
这是我的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>
您可以像这样将文件名放在appsetting中
<add key="FolderPath" value="~/Files/" />
bool isExists = System.IO.Directory.Exists(Server.MapPath(FolderPath));
if(!isExists)
System.IO.Directory.CreateDirectory(Server.MapPath(FolderPath));
bool isExists=System.IO.Directory.Exists(Server.MapPath(FolderPath));
如果(!isExists)
System.IO.Directory.CreateDirectory(Server.MapPath(FolderPath));
在配置中尝试以下操作:
<add key="FolderPath" value="~/Files/" />
检查该文件夹的安全设置……事实上,如果该文件夹不存在,我想创建“文件”文件夹。当我在我的项目中手动创建文件夹时,我的Directorynotfoundexception已消失。@user3737490我已更新代码,以检查目录是否存在,如果不创建它。@user3737490已更新代码,它是FolderPath的变量名