[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Converting vbscript to C#

Posted on 2014-08-19
15
Medium Priority
?
2,751 Views
Last Modified: 2014-08-30
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
0
Comment
Question by:ksd123
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40270252
0
 

Author Comment

by:ksd123
ID: 40270590
I want to convert vbscript to C#, the above tools convert Visual Basic to c#
0
 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40270605
you can flip it eitherways in the drop down
0
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.

 

Author Comment

by:ksd123
ID: 40271652
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
 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40272525
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
 

Author Comment

by:ksd123
ID: 40275306
Hi Experts,
I am looking for some help to convert the above code in vbscript to C#.
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 40281388
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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40281863
What is the goal of your vbscript application?
0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 40282544
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 40282955
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
 

Author Comment

by:ksd123
ID: 40283791
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 40283828
You should have specific questions. You "question" looks like a job request. I think you have a good starting point now.
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 1500 total points
ID: 40284729
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
 

Author Closing Comment

by:ksd123
ID: 40294621
Thank you
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 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