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
ksd123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jayaraja JayaramanSharepoint Architect ConsultantCommented:
0
ksd123Author Commented:
I want to convert vbscript to C#, the above tools convert Visual Basic to c#
0
Jayaraja JayaramanSharepoint Architect ConsultantCommented:
you can flip it eitherways in the drop down
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ksd123Author Commented:
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#.
0
Jayaraja JayaramanSharepoint Architect ConsultantCommented:
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.
0
ksd123Author Commented:
Hi Experts,
I am looking for some help to convert the above code in vbscript to C#.
0
Robberbaron (robr)Commented:
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..
0
Gerwin Jansen, EE MVETopic Advisor Commented:
What is the goal of your vbscript application?
0
Manoj PatilSr. Software EngineerCommented:
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
0
Jaime OlivaresSoftware ArchitectCommented:
Here you have comparative code that performs similar operations to scan the installed IIS web sites:
http://www.iis.net/configreference/system.applicationhost/sites
0
ksd123Author Commented:
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#.
0
Jaime OlivaresSoftware ArchitectCommented:
You should have specific questions. You "question" looks like a job request. I think you have a good starting point now.
0
Robberbaron (robr)Commented:
your tasks is almost exactly as per the http://www.iis.net/ examples linked by myself and Jaime Olivares.

you can see the vbs and c# methods compared.

need to load in the WebAdmin package..

/// nuget Install-Package Microsoft.Web.Administration
using Microsoft.Web.Administration;
 
potential changes required,,,, not tested but compiles...
        private static void DoIt()
        {
            using (ServerManager serverManager = new ServerManager())
            {
                Configuration config = serverManager.GetApplicationHostConfiguration();
                ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
                ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
                ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Test Site");

                if (siteElement == null)
                {
                    throw new InvalidOperationException("Element not found!");
                }
                else
                {
                    //ok site exists
                }

                ///now add new
                ConfigurationElementCollection siteCollection = siteElement.GetCollection();
                ConfigurationElement appElement = FindElement(sitesCollection, "application", "name", @"TestApp");
                if (appElement == null)
                {
                    ///thats good
                }
                else
                {
                    siteCollection.Remove(appElement);
                }

                ConfigurationElement applicationElement = siteCollection.CreateElement("application");

                applicationElement["path"] = @"/WhatGoesHere";
                ConfigurationElementCollection applicationCollection = applicationElement.GetCollection();
                ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory");
                virtualDirectoryElement["path"] = @"/";
                virtualDirectoryElement["physicalPath"] = @"C:\Inetpub\wwwroot";
                applicationCollection.Add(virtualDirectoryElement);
                siteCollection.Add(applicationElement);

                serverManager.CommitChanges();
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ksd123Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.