我正在搜索在 IIS 中创建,修改和删除Virtual directory entry
的解决方案。
我使用以下代码: 获取IIS服务器root virtuakl目录
function GetIISWebServer(WebServerIndex: integer = 1): Variant;
var
BindContext: IBindCtx;
Eaten: longint;
Moniker: IMoniker;
ServerRootDispatch: IDispatch;
MonikerString: WideString;
const
WebServiceMoniker = 'IIS://localhost/W3SVC';
begin
MonikerString := WebServiceMoniker + '/' + IntToStr(WebServerIndex);
OleCheck(CreateBindCtx(0, BindContext));
OleCheck(MkParseDisplayName(BindContext, PWideChar(MonikerString), Eaten, Moniker));
OleCheck(Moniker.BindToObject(BindContext, nil, IDispatch, ServerRootDispatch));
if (ServerRootDispatch = nil) then
raise Exception.Create('Unable to retrieve Server Root object using the Moniker "' + MonikerString + '"');
Result := ServerRootDispatch;
end;
function GetIISWebServerRootVirtualDir(WebServerIndex: Integer = 1): Variant;
begin
Result := GetIISWebServer(WebServerIndex).GetObject('IisWebVirtualDir', 'Root');
end;
检查虚拟目录是否已存在
function VirtualDirectoryExists(VirtualDirectoryName : String) : Boolean;
var
BindContext: IBindCtx;
Eaten: longint;
Moniker: IMoniker;
ObjectDispatch: IDispatch;
VariantObject: Variant;
WebVirtualDir: Variant;
begin
OleCheck(CreateBindCtx(0, BindContext));
try
OleCheck(MkParseDisplayName(BindContext, 'IIS://localhost/W3SVC', Eaten, Moniker));
except
on E : Exception do
begin
ShowMessage(E.Message);
Result := False;
Exit;
end;
end;
OleCheck(Moniker.BindToObject(BindContext, nil, IDispatch, ObjectDispatch));
VariantObject := ObjectDispatch;
try
WebVirtualDir := VariantObject.GetObject('IisWebVirtualDir', '1/Root/' + VirtualDirectoryName);
Result := True;
except
Result := False;
end;
end;
删除虚拟目录
function RemoveVirtualDirectory(VirtualDirectoryName, AppId : String; RemoveApp : Boolean) : Boolean;
var
ServerRoot : Variant;
Cat : ICOMAdminCatalog;
Apps : ICatalogCollection;
TmpComp : ICatalogObject;
LTest: Variant;
Idx : Integer;
TmpStr: String;
begin
Result := True;
// check if virtual derectory exist
if VirtualDirectoryExists(VirtualDirectoryName) then
begin
ServerRoot := GetIISWebServerRootVirtualDir;
try
ServerRoot.Delete('IisWebVirtualDir', VirtualDirectoryName);
except
Result := False;
end;
if RemoveApp then
begin
Cat := CoCOMAdminCatalog.Create;
Apps := Cat.GetCollection('Applications') as ICatalogCollection;
Apps.Populate;
Idx := 0;
TmpComp := GetCatalogObjectByName(Apps, AppId);
if TmpComp <> nil then
begin
while Idx < Apps.Count do
begin
if Apps.Item[Idx] = TmpComp then
begin
TmpComp.Value['Deleteable'] := True;
Apps.SaveChanges;
Apps.Remove(Idx);
Apps.SaveChanges;
end
else
Inc(Idx);
end;
end;
end;
end;
end;
但问题是当我搜索并删除虚拟目录时,它将Web应用程序图标转换为文件夹,但不会将其物理删除。
因此我无法重新创建相同的目录。
在创建虚拟目录时,我收到错误Can not create a file which is already present at the specified path
任何人都可以帮我找到相同的原因。 以及如何使用delphi删除和创建虚拟目录。
我在Windows 7操作系统中使用IIS 7.5。
任何类型的建议或帮助我。
提前谢谢。