Link to home
Start Free TrialLog in
Avatar of ksd123
ksd123

asked on

Converting vbscript to C#

Hi Experts,

I have following vbscript and need help to convert  into C#.

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection

siteElementPos=FindElement(sitesCollection,"site",Array("name","TestSite"))

If(siteElementPos=-1) Then
WScript.Echo "Element not found"
WScript.Quit
End If

Set siteElement = sitesCollection.Item("siteElementPos)
Set siteCollection = siteElement.Collection

appElementPos=FindElement(siteCollection,"application",Array("path","/TestApp"))

If(appElementpos>-1) Then
siteCollection.DeleteElement(appElementPos)
End If

Set ApplicationElement = siteCollection.CreateNewElement("application")
ApplicationElement.Properties.Item("path").Value = "/TestApp"
ApplicationElement.Properties.Item("applicationPool").Value ="Win32AppPool"

Set ApplicationCollection = ApplicationElement.Collection

Set virtualDirectoryElement = applicationCollection.CreateNewElement("virtualDirectory")
virtualDirectoryElement.Properties.Item("path").Value = "/"
virtualDirectoryElement.Properties.Item("physicalPath").Value = "C:\Inetpub\wwwroot\TestApp"
ApplicationCollection.AddElement(virtualDirectoryElement)
siteCollection.AddElement(ApplicationElement)
adminManager.CommitChanges()

Function FindElement(collection,elementTagName,valuesToMatch)

For i=0 To CInt(collection.Count) -1

Set element=collection.Item(i)
if(element.Name=elementTagName Then
matches=True

For iVal=0 To UBound(valuesToMatch) Step 2

Set prop=element.GetPropertyByName(valuesToMatch(iVal))
value=prop.Value

If Not value=Null Then
value=CStr(Value)
End If

If Not value=valuesToMatch(iVal +1) Then
matches=False
Exit For
End If

Next

If matches Then
	Exit For
      End If
End If
Next

If matches Then

	FindElement=i
 Else

	FindElement=-1
End If

End Function

Open in new window


Thanks in Advance
Avatar of Jayaraja Jayaraman
Jayaraja Jayaraman
Flag of United States of America image

Avatar of ksd123
ksd123

ASKER

I want to convert vbscript to C#, the above tools convert Visual Basic to c#
you can flip it eitherways in the drop down
Avatar of ksd123

ASKER

Hi Experts,

I want to convert the above code written in vbscript to C#, there is some difference between vbscript and Visual Basic.We have tools available to convert Visual Basic to c# and Vice versa.I am looking for some help to convert vbscript to C#.
sorry my bad.
As far as i know, there are no automation tool.
You can use: http://converter.telerik.com/[^], which converts vb to C#, but vb is not vbs, it's similar to vb.
Avatar of ksd123

ASKER

Hi Experts,
I am looking for some help to convert the above code in vbscript to C#.
while i dont understand what you are trying to achieve but
http://www.iis.net/configreference/system.applicationhost/sites/site/application seems to have much the same items..
What is the goal of your vbscript application?
There are no automatic tools to do this, as far as I know.  There was a Migration Assistant for converting classic asp files over to ASP.NET 1.1, but it wasn't very good, and wasn't even updated further.

See this link. It shows you the functions of VBScript and alternatives in C#. It will help you.

http://www.netcoole.com/asp2aspx/vbhtml/csfuncs.htm
Here you have comparative code that performs similar operations to scan the installed IIS web sites:
http://www.iis.net/configreference/system.applicationhost/sites
Avatar of ksd123

ASKER

Thank you all.Actually I am writing a custom action using C# with WIX in visual 2012.  We have a legacy application that uses vbscript to do the following task and I want to convert it to c# for custom action.

1)Get the Sites collection in IIS and find the Web site  with name called "Test Site"
2)If not found ,send error message "element not found"
3)If found, check if there is any "application"  with name "TestApp" within "Test Site"
4)If found,  Delete it
5)Create a new "application"  with name "TestApp" and then "virtualDirectory" with physical path "c:\inetpub\wwwroot\
TestApp"  and commit changes.

I want to acheive above steps in c#.
You should have specific questions. You "question" looks like a job request. I think you have a good starting point now.
ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ksd123

ASKER

Thank you