Virtual Directory Creation
Guys, I开发者_如何学Go want to write a program in ASP.NET to create virtual directories on the hosting server. Please tell me how to program this?
To tackle this, you need to impersonate an administrator, or at least a user with sufficient rights. After that, use something like the code provided here (other examples available, try Google), or the code provided by Russ C in this thread.
Perhaps too obvious to mention, but I do it anyway: doing so is dangerous (!!). Make sure to safe-guard your page with more than just a password (i.e., add an ip-address filter to your firewall).
EDIT:
The question doesn't seem to be about virtual directories, but just directories. Assuming rights are set correctly, the following should work to create a directory under your current virtual path:
string serverVirtualPath = Server.MapPath("~");
DirectoryInfo dirInfo = new DirectoryInfo(serverVirtualPath);
dirInfo.CreateSubDirectory("MyImageSubDir"); // returns a DirectoryInfo object
It's different for IIS 6 and 7, and I don't know if this will work in ASP.Net or if it has to be done from a Winform/Console application (because of role security)...
If 6 use:
DirectoryEntry deIIS = new DirectoryEntry( "IIS://" + Environment.MachineName + "/W3SVC/1/Root" );
DirectoryEntry deNewVDir = deIIS.Children.Add( vdName, deIIS.SchemaClassName.ToString() );
deNewVDir.CommitChanges();
deIIS.CommitChanges();
For 7:
ServerManager iisManager = new ServerManager();
iisManager.Sites["Default Web Site"].Applications.Add("/myVirtualDirectory", installationDirectory);
iisManager.Sites["Default Web Site"].Start();
iisManager.CommitChanges();
精彩评论