user3737490
user3737490

Reputation: 27

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. But when I upload this file, I get a DirectoryNotFoundException... i Don't get it because my folder exists...

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 :

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

Upvotes: 0

Views: 920

Answers (2)

Devesh
Devesh

Reputation: 4560

You can put the file name in the appsetting like this

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

Upvotes: 1

ttaaoossuu
ttaaoossuu

Reputation: 7894

Try this in config:

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

Upvotes: 1

Related Questions