简体   繁体   English

如何更正asp.net中的“ DirectoryNotFoundException”?

[英]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.

相关问题 托管 asp.net 应用程序 DirectoryNotFoundException - Hosted asp.net application DirectoryNotFoundException 如何重现DirectoryNotFoundException? - How do I reproduce a DirectoryNotFoundException? ASP.NET Core Web API - 如何解决 DirectoryNotFoundException:Extensions.FileProviders.PhysicalFileProvider - ASP.NET Core Web API - How to resolve DirectoryNotFoundException: Extensions.FileProviders.PhysicalFileProvider 如何在ASP.net中做到这一点? - How do I do this in ASP.net? 如何使用MVC ASP.Net返回单元测试正确的视图? - How do I Unit Test the correct view is returned with MVC ASP.Net? 如何为ASP.NET TextBox获取正确的ClientID? - How do I get the correct ClientID for my ASP.NET TextBox? ASP.Net 如何访问列表中的正确项目 - ASP.Net How can I access the correct item in the list 如何使用asp.net从本地文件夹中的数据列表中删除图像-错误(system.io.directorynotfoundexception无法找到路径的一部分) - How to delete an image from datalist from a local folder with asp.net - Error (system.io.directorynotfoundexception could not find a part of the path) 如何在asp.NET中限制asp:Repeater? - How do I Limit asp:Repeater in asp.NET? 如何在ASP.NET MVC中进行注册? - How do I do a registration in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM