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