PDA

View Full Version : Tạo Virtual Directory bằng C# Code



hatred
07-08-2007, 21:48
This is my code, chỉ là ví dụ:



System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot( );

string sError;

vr.Create("IIS://localhost/W3SVC/1/Root",@"C:\Demos\WebServices\","WebServices",out sError);


Code trên có thể tạo được Virtual Directory, nhưng mình gặp nhiều vấn đề.

1. Grant access như thế nào trên folder cài để có thể HttpRequest được.
2. Trên máy mình có 2 .NET framework 1.1 và 2.0, làm sao mình mặc định là dùng 2.0 được.

Bạn nào biết thì trả lời nhé.
Thanks.

vFork
07-08-2007, 23:18
Bạn thử chỉnh qua properties của DirectoryEntry

DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT");

DirectoryEntries vdirs = site.Children;
DirectoryEntry newVDir = vdirs.Add("Test", "IIsWebVirtualDir");
newVDir.Properties["Path"][0] = "C:\\Apps";
newVDir.Properties["AccessScript"][0] = true;
// These properties are necessary for an application to be created.

newVDir.Properties["AppFriendlyName"][0] = "Test";
newVDir.Properties["AppIsolated"][0] = "1";
newVDir.Properties["AppRoot"][0] = "/LM/W3SVC/1/ROOT/Test";

newVDir.CommitChanges();

hatred
08-08-2007, 17:08
Thanks vFork, I found the better way to manage as below:



private void CreateVirtualDirectory(string virtualDirectoryName, string physicalPath)
{
string IISRoot = @"IIS://localhost/W3SVC/1/Root";

IISVirtualRoot virtualRoot = new IISVirtualRoot();
string error = string.Empty;

virtualRoot.Create(IISRoot, physicalPath, virtualDirectoryName, out error);
if (!string.IsNullOrEmpty(error.Trim()))
{
DirectoryEntry directoryEntry = new DirectoryEntry(IISRoot + virtualDirectoryName);
if (directoryEntry != null)
{
directoryEntry.Properties["AuthFlags"].Value = 21;
directoryEntry.CommitChanges();
}
}
}