Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Working with IIS

Posted on 2006-05-04
13
Medium Priority
?
354 Views
Last Modified: 2010-04-16
I have been able to work out how to do a number of things with IIS using Delphi 2006 but there are still a few things I am looking to do, as follows:

1. Obtain a list of all virtual directories and it's associated disk folder for the default website or another "named" website
2. A way of re-naming a virtual folder rather than having to do it by making updates to METABASE.XML
3. Having obtained the list of virtual folders get a list of all files associated with that virtual directory

Most important items are 1 and 2, 3 would be nice but not necessary but if you can answer 1 or 2 (1 and 2 even better) then the points are yours.

Thanks,

Graham
0
Comment
Question by:Graham9295
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 34

Expert Comment

by:Dave_Dietz
ID: 16749965
As far as doing this directly in Delphi I have no idea since I've never even touched Delphi.

Can Delphi make use of ADSI and/or WMI?  If so it can use either to querey and/or edit the configuration of IIS without having to work directly with the Metabase.xml file.

Please see the following link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/dc37602d-f02b-497f-9b88-afeba9d8d2bc.asp

Dave Dietz

0
 
LVL 28

Expert Comment

by:2266180
ID: 16756244
regarding the WMI part Dave mentioned, here is one way of dealing with WMI from delphi: http://www.ciuly.com/delphi/wmi/index.html (a few examples) and http://www.ciuly.com/delphi/wmi/settingUpWmi/index.html on how to setup delphi for WMI.
of course, I only recommend using WMI if:
- there is no other solution, or
- the solution is too complicated/too much code, or
- you will be doing a lot of stuff that can be done with WMI, or
- you just want to learn/see/etc a WMI solution
(same for ADSI, though the two resemble, ADSI seems to be a preffered way among developers. I don't know why. I personally prefer WMI :D
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EddieShipman
ID: 16760803
ciuly,
  I couldn't see a way to do what the OP asked using WMI. only ADSI.
0
 
LVL 28

Expert Comment

by:2266180
ID: 16761313
hm... well, I'll look into it in a few hours after I get home and let you (all) know if it is at all possible. Though as I read on msdn, it should be.
0
 
LVL 28

Expert Comment

by:2266180
ID: 16762746
bad luck. I have winxp sp1 which comes with IIS 5.1 and according to msdn: IIS 5.1 and earlier: The IIS WMI provider is not available.
so I'll look into this tomrrow at work. sorry for the delay.
0
 
LVL 28

Expert Comment

by:2266180
ID: 16768314
it's a little tricky to be doing this during work time, but slowly it is getting along :D
so here is solution for issue #1 (it has that format and information layout becaue I will probably be using the same app to do the rest of the issues and will need that info): http://www.ciuly.com/delphi/wmi/virtualDirectoriesIIS/index.html

for #2, I assume you want to change the "Starting point" property which is displayed from the IIS management console at "virtual directory", right? so that yourdomain/old_virt_dir will become yourdomain/new_virt_dir . is this correct? I looked a little on how to do this but couldn't find a fast solution. Might be left for tomorrow.

for #3, isn't that the same with the files/dirs which are in the path given by #1?
0
 
LVL 1

Author Comment

by:Graham9295
ID: 16771890
All,

Thanks for the input and there is something for me to get my teeth into albeit on a slight different track than I have been going and most likely should have mentioned in the first place. The route I have been going is the .NET route and using the routines that "Directory.Services" brings to the table. For example I have managed to work, with many visits to MSDN and odd bits of VB.NET & C# code, out how to create a virtual folder (see below) but have got stuck on the enumaration of the folders and especially on re-naming them. To answer the specific questions from ciuly, yes yourdoman/old_virt_dir would be come yourdomain/new_virt_dir and for #3 then yes I would expect it to be so assuming that the solution to #1 "exposes" the files and/or directories for the virtual directory.

No laughing now, important part of the code for creating a virtual directory

procedure TMainForm.CreateVirtualDirectory(sVirtualDirectoryName, sVirtualDirectoryPath: String);
var
  Schema, IISRootPath : string;
  RootDirectortEntry, NewVirtualDirectory : DirectoryEntry;
begin
  Schema := 'IIsWebVirtualDir';
  IISRootPath := '/W3SVC/1/Root';

  RootDirectortEntry := DirectoryEntry.Create('IIS://localhost' + IISRootPath );
  try
    RootDirectortEntry.RefreshCache;

    NewVirtualDirectory := RootDirectortEntry.Children.Add( sVirtualDirectoryName, Schema );
    NewVirtualDirectory.Properties['Path'].Insert( 0, sVirtualDirectoryPath );
    NewVirtualDirectory.Properties['DefaultDoc'].Insert(0, GetDefaultDocumentsList);
    NewVirtualDirectory.Properties['AnonymousUserName'].Insert(0, sComputerName + '\' + aeUserName.Text);
    NewVirtualDirectory.Properties['AnonymousUserPass'].Insert(0, aePassword.Text);
    NewVirtualDirectory.CommitChanges;
    RootDirectortEntry.CommitChanges;

    // Create an Application if it is a Web Virtual Directory
    // So that is realises that it should sit off the root.
    NewVirtualDirectory.Invoke( 'AppCreate' , [ true ] );

    // Save all changes
    NewVirtualDirectory.CommitChanges;
    RootDirectortEntry.CommitChanges;

    // Close gracefully
    NewVirtualDirectory.Close;
    RootDirectortEntry.Close;

    // Update Statusbar for User
    sbInfo.SimpleText := 'Virtual Directory : ' + sVirtualDirectoryName + '(' + sVirtualDirectoryPath + ') created.';
    Inc(iCreated);
  except on Ex : Exception do
    begin
      if Ex.Message = 'Cannot create a file when that file already exists.' then
        Application.MessageBox('"' + sVirtualDirectoryName + '" already exists', 'Virtual Directory Creation Error', MB_ICONEXCLAMATION)
        else Application.MessageBox('Error Text: ' + Ex.Message + Chr(13) + Chr(10) + 'Error Number: ' + IntToStr(Ex.HResult), 'Virtual Directory Creation Error', MB_ICONEXCLAMATION)
    end;
  end;
end;

0
 
LVL 28

Expert Comment

by:2266180
ID: 16772667
regarding the:  and for #3 then yes I would expect it to be so assuming that the solution to #1 "exposes" the files and/or directories for the virtual directory.
- #1 exposes "path". I guess you didn't run the example to see it :). so since you have the path (like c:\my_server_root_dir) you can just use a plain simple findfirst/findnext or in the .net way using the File/Directory classes (dunnon the names in delphi, but I guess they would be similar to C# )

does your code work?
the reason I am asking this is because at work, when I was getting the IISRoot, it was in the form: /W3SVC/XXX/Root, where XXX was a pretty big number (10 digits or so), unique per website.

I never worked in delphi.net, just C#, so even though you code looks fine to me, it could have errors (if not other case, then when there are more than 1 websites).

I will have to tell you though that testing against error messages is a bad idea. if you or someone else happens to run this on a localized windows, then it won't work ;)
0
 
LVL 1

Author Comment

by:Graham9295
ID: 16774903
You are right I haven't yet run your example, it was getting late by the time I had the chance to have a look (I'm in the UK) but I will be doing so over the weekend. As for my code yes it does work, I have happily been able to create virtual directories although I have never seen the same big numbers that you have, only 1 for the first web site, 2 for the second etc although for my current requirements I am only concerned with a configuration that only has the default web site with the virtual directories below that. You are of course 100% correct about the testing against error messages, the code is a long way away from being "production" and I know that is one bit I need to sort out but for testing against my local environment it will do for now.

Thanks.
0
 
LVL 1

Author Comment

by:Graham9295
ID: 17245522
I did try the examples above but unfortunately none of them did what I wanted to do but over the last few weeks I have continued to work on this and I have found a solution to two of my questions and a partial answer to one of them.

The rough and ready code sample below will answer question 1 and the result of that will let me answer question three because I can just do a simple search on disk to get the files:

procedure TMainForm.ListVirDirs;
Var
  ienum : IEnumerator;
  deRootDirectoryEntry : DirectoryEntry;
  Child : DirectoryEntry;
  sChildName : String;
  sChildPath : String;
  iCount : Integer;
begin
  slVirDirs.Clear;
  deRootDirectoryEntry := DirectoryEntry.Create('IIS://localhost' + IISRootPath);
  try
    deRootDirectoryEntry.RefreshCache;
    ienum := deRootDirectoryEntry.Children.GetEnumerator;
    while ienum.MoveNext do
    begin
      child := ienum.Current as DirectoryEntry;
      sChildName := child.Name;
      if Child.Properties.Contains('Path') then
        sChildPath := Child.Properties.Item['Path'].Value.ToString
        else sChildPath := '';
      slVirDirs.Add(sChildName + ' ' + sChildPath);
    end;
  except on Ex : Exception do
    begin
      if Ex.Message <> 'The system cannot find the path specified' then
        ShowMessage(Ex.Message);
    end;
  end;
end;

BE WARNED there is a gotcha when doing the above, if you have "aspnet_client" as a virtual folder under IIS then even with the "If Child.Properties.Contains('Path') then" test an exception is always thrown and I haven't yet found a way around that, apart from putting something like "If Child.Name <> "aspnet_client then.....", but still working on it but at least I can now get the information I need and from that get a list of the files.

I have also found the way to rename a virtual directory, if you leave aside the minor detail that it doesn't work :-)     The following (again round and ready) code shows the way:

procedure TMainForm.ReNameVirtualDirectory(sCurrentName: string; sNewName: string);
var
  bResult              : Boolean;
  deRootDirectoryEntry : DirectoryEntry;
begin
  bResult := False;
  deRootDirectoryEntry := DirectoryEntry.Create('IIS://localhost' + IISRootPath + '/' + sCurrentName);
  try
    deRootDirectoryEntry.RefreshCache;
    ShowMessage(deRootDirectoryEntry.Parent.Name);
    if deRootDirectoryEntry.Exists('IIS://localhost' + IISRootPath + '/' + sCurrentName) then
    Begin
      deRootDirectoryEntry.Rename(sNewName);
      deRootDirectoryEntry.CommitChanges;
      deRootDirectoryEntry.Close;
    End;
  except on Ex : Exception do
    begin
      if Ex.Message <> 'The system cannot find the path specified' then
        ShowMessage(Ex.Message);
    end;
  end;
end;

It should work but for some reason the Rename command throws up an "Invalid Handle" error and I have not been able to work out why so if anyone can throw some light on the reason for that I would be very grateful.

Graham



0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 17695878
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question