0

In my web application, i have some files those are saving within application it's creating a folder for saving files but i need to save those file outside of the application and inside of IIS.how can i do this? With in application Folder we are using below code

Server.MapPath(Path) 

For Saving in IIS How can i Write?

Thank you

2 Answers2

0

you need to create a virtual directory that points ti the folder outside. Go to IIS right click on your website. click on Add Virtual directry from the menu.Give an alias for the directory select your desired folder and you are done. it will consider this outside folder as an internal folder and work the same way. check this link How to: Create and Configure Virtual Directories in IIS 7.0

Disclaimer: but you will have to do this after hosting to iis i.e publishing. while using visual studio in dev environment i.e debugging it will store in internal directories only

Edit: for creating virtual directories this is the code. I have not tested its validity.

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

 }
}
Sujit.Warrier
  • 2,815
  • 2
  • 28
  • 47
  • is it possible to create directory Dynamically ? and programmatically how can i access the Directory ? –  Oct 07 '16 at 07:25
  • i think you cant. sorry ignore earlier comment – Sujit.Warrier Oct 07 '16 at 07:29
  • Thank you #Mysterio11, Here how can i access Virtual Directory ? if i am trying with Server.Mappath, but it is accessing application path . –  Oct 07 '16 at 07:41
0

Normally you can not create a folder outside the root path i.e. if you have your application in say C:\inetpub\testapp you can only create a folder inside testapp. This restriction is for security reason where a web server is not supposed to allow access to anything above root folder.

Moreover it's not recommended to write any folders/files in the root folder as writing to root folder cause appdomain to recycle after certain number of writes (default is 15) causing session loss. See my answer here.

However there is a workaround

Add a path of your server to web.config and then fetch it in your code.Use something like below in the appsettings section of web.config

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

Create a folder of above path and add Users group to your folder and give that group full permission (read/write). (Adding permission is very important)

In your code you can fetch as below

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

Hope this helps

Community
  • 1
  • 1
Ravi A.
  • 2,163
  • 2
  • 18
  • 26