繁体   English   中英

我如何将文件保存在asp.net中Application文件夹外部IIS的文件夹中

[英]How can i save files in folder within the IIS of outside of the Application folder in asp.net

在我的Web应用程序中,我有一些要保存在应用程序中的文件,它正在创建用于保存文件的文件夹,但是我需要将这些文件保存在应用程序之外和IIS内。我该怎么做? 在应用程序文件夹中,我们使用以下代码

Server.MapPath(Path) 

用于在IIS中保存如何编写?

谢谢

您需要创建一个虚拟目录,该目录将文件夹指向外部。 转到IIS,右键单击您的网站。 单击菜单中的“添加虚拟目录”。为目录指定别名,选择所需的文件夹即可。 它将外部文件夹视为内部文件夹,并以相同的方式工作。 检查此链接如何:在IIS 7.0中创建和配置虚拟目录

免责声明:但是托管到iis即发布后,您将必须执行此操作。 在开发环境中使用Visual Studio时(即调试它)将仅存储在内部目录中

编辑:用于创建虚拟目录,这是代码。 我尚未测试其有效性。

static void CreateVDir(string metabasePath, string vDirName, string physicalPath)
{
  //  metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
  //    for example "IIS://localhost/W3SVC/1/Root" 
  //  vDirName is of the form "<name>", for example, "MyNewVDir"
  //  physicalPath is of the form "<drive>:\<path>", for example,"C:\Inetpub\Wwwroot"


  try
  {
    DirectoryEntry site = new DirectoryEntry(metabasePath);
   string className = site.SchemaClassName.ToString();
  if ((className.EndsWith("Server")) || (className.EndsWith("VirtualDir")))
  {
  DirectoryEntries vdirs = site.Children;
  DirectoryEntry newVDir = vdirs.Add(vDirName, (className.Replace("Service", "VirtualDir")));
  newVDir.Properties["Path"][0] = physicalPath;
  newVDir.Properties["AccessScript"][0] = true;
  // These properties are necessary for an application to be created.
  newVDir.Properties["AppFriendlyName"][0] = vDirName;
  newVDir.Properties["AppIsolated"][0] = "1";
  newVDir.Properties["AppRoot"][0] = "/LM" + metabasePath.Substring(metabasePath.IndexOf("/", ("IIS://".Length)));

  newVDir.CommitChanges();


}
else

  }
 catch (Exception ex)
 {

 }
}

通常,您不能在根目录路径之外创建文件夹,即如果您的应用程序位于C:\\inetpub\\testapp ,则只能在testapp内部创建一个文件夹。 出于安全原因,此限制是在Web服务器不应允许访问根文件夹之上的任何内容的情况下。

此外,不建议在根文件夹中写入任何文件夹/文件,因为写入根文件夹会使appdomain在一定次数的写入(默认为15)后回收,从而导致会话丢失。 在这里查看我的答案

但是,有一种解决方法

将服务器的路径添加到web.config,然后在代码中获取它。在web.config的appsettings部分中使用类似下面的内容

<add key="logfilesPath" value="C:\inetpub\MyAppLogs" />

创建一个以上路径的文件夹,然后将“ Users组添加到您的文件夹中,并为该组授予完全权限(读/写)。 (添加许可非常重要)

在您的代码中,您可以按以下方式获取

string loggerPath = (ConfigurationManager.AppSettings["logfilesPath"]);

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM