如何更正“directorynotfoundexception”?在asp.net

本文关键字:asp net 何更正 directorynotfoundexception | 更新日期: 2023-09-27 18:08:24

我正在与asp.net webforms Framework 4网站工作,我想在我的DB Sql Server导入一个Excel文件。但是当我上传这个文件时,我得到一个DirectoryNotFoundException…

下面是我的代码:

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");
            }
        }

这是我的网的一部分。配置:

<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>

如何更正“directorynotfoundexception”?在asp.net

您可以像这样将文件名放入appsetting

         <add key="FolderPath" value="~/Files/" />
         bool isExists = System.IO.Directory.Exists(Server.MapPath(FolderPath));
         if(!isExists)
         System.IO.Directory.CreateDirectory(Server.MapPath(FolderPath));

在config中试试:

<add key="FolderPath" value="~/Files/" />