I'm using vb.net 3.5 with asp.net and I need to list all AppPools names from IIS and show them in dropdownlist.
any help please ?
thanks
finally I found the solution and here is the methods it could help ..
Public Function GetAppPoolNames() As List(Of String)
Dim Root As System.DirectoryServices.DirectoryEntry = GetDirectoryEntry("IIS://localhost/W3SVC/AppPools")
'DirectoryEntry Root = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
Dim AppList As New List(Of String)
If Root Is Nothing Then
Else
For Each dir As DirectoryEntry In Root.Children
Dim pr As System.DirectoryServices.PropertyCollection = dir.Properties
'ApplicationPool pool = new ApplicationPool();
'pool.Name = dir.Name;
'DropDownList1.Items.Add(pool.Name);
AppList.Add(dir.Name)
Next
End If
Return AppList
End Function
Private Function GetDirectoryEntry(ByVal path As String) As DirectoryEntry
Dim root As DirectoryEntry = Nothing
Try
root = New DirectoryEntry(path)
Catch
'Console.WriteLine("Could not access Node")
Return Nothing
End Try
If root Is Nothing Then
'Console.WriteLine("Could not access Node")
Return Nothing
End If
Return root
End Function
Related
I use the following code to open an excel-file:
Private EPXlApp As OfficeOpenXml.ExcelPackage
Private EPXlFile As FileInfo
Private EPXlSheet As ExcelWorksheet
Private EPXlWorkbook As ExcelWorkbook
Public Sub New(newFilePath As String, Optional password As String = "")
PathFile = newFilePath
OfficeOpenXml.ExcelPackage.LicenseContext = LicenseContext.NonCommercial
EPXlFile = New FileInfo(PathFile)
EPXlApp = New OfficeOpenXml.ExcelPackage(EPXlFile, password) ' ---> here the error occurs
EPXlWorkbook = EPXlApp.Workbook
EPXlSheet = EPXlApp.Workbook.Worksheets(1)
End sub
This works. But if one person block the file, i can't open it again. I don't understand the error message
System.IO.InvalidDataException: "File S:\Team\file.xlsm is not an encrypted package"
because thats the rigt password, it has already worked if no one blocked the excel file.
Thank you very much in advance!
I have a XML structure and am trying to add child nodes to child node of the root element. I am using insert before method but am getting object doesnt support this property or method error. Below is the code I have:
Sub GenerateXMLBodyForNewTestCreation()
Dim requestTemplate, cookie, createPerfTestURL As String
Dim noOfScripts, i, sample As Integer
requestTemplate = Sheets("XMLSampleForCreatePerfTest").Range("B1").Value
Set CreateTestXMLTemplate = CreateObject("Msxml2.DOMDocument")
CreateTestXMLTemplate.LoadXML (requestTemplate)
noOfScripts = Sheets("ScriptDetails").UsedRange.Rows.Count
Set root = CreateTestXMLTemplate.DocumentElement
Set root1 = CreateTestXMLTemplate.SelectNodes("//Test/Content/Groups/Group")(0)
For i = 2 To noOfScripts
Set y = root1.CloneNode(True)
sample = root.InsertBefore(y, root1(0))
Next i
CreateTestXMLTemplate.Save ("C:\Users\rrayudu\Desktop\XMLBody.xml")
End Sub
It may be a silly mistake but am struggling to get it working.
I would like to automatize the process of SSL certificate installation for IIS 7.5. The preferred way is to use VBScript. I work on the problem to create a new HTTPS binding and to bind correct certificate to this binding.
I actually solved this problem activating Add IIS Management Scripts and Tools role for my web-server and using script like this:
Set serverWebAdmin = GetObject("winmgmts:root\WebAdministration")
' EC8BCFF70983EA26BFEA087683329CB8C07366A5 is an certificate hash of the fake certificate
' that i obtain from the staging environment of Let's Encrypt
' "MY" is the name of certificate storage
serverWebAdmin.Get("SSLBinding").Create "*", 443,"EC8BCFF70983EA26BFEA087683329CB8C07366A5", "MY"
Set newBinding = serverWebAdmin.Get("BindingElement").SpawnInstance_
newBinding.BindingInformation = "*:443:"
newBinding.Protocol = "https"
Set issuedWebSite = serverWebAdmin.Get("Site.Name='sitename.com'")
webSiteBindings = issuedWebSite.Bindings
ReDim Preserve webSiteBindings(UBound(webSiteBindings) + 1)
Set webSiteBindings(UBound(webSiteBindings)) = newBinding
issuedWebSite.Bindings = webSiteBindings
Set pathResult = issuedWebSite.Put_
It works well but before to use WMI to manage the server i tried to use (and expand a little) an example from MSDN how to create a binding. I took the example on VBScript and added the declaration of certificate hash and certificate storage name (i checked also these properties, they are existing so seems to be possible to set them. I also checked the code of some open-source projects like WinAcme - written in C# - and they use the same properties).
So my code was looking like this (the part that sets properties of binding):
Set bindingElement1 = bindingsCollection.CreateNewElement("binding")
bindingElement1.Properties.Item("protocol").Value = "https"
bindingElement1.Properties.Item("bindingInformation").Value = "*:443:"
bindingElement1.Properties.Item("certificateHash").Value = "EC8BCFF70983EA26BFEA087683329CB8C07366A5"
bindingElement1.Properties.Item("certificateStoreName").Value = "MY"
bindingsCollection.AddElement(bindingElement1)
adminManager.CommitChanges()
It works BUT it only creates the binding and DOES NOT append good certificate to this binding. My problem is solved by the previous code snippet but I would like to understand: is it the second code snippent wrong? Is it possible to bind good certificate this way?
Thank you by advance.
The reason it did not work with your bindingElement1 variant is simply because you can't add it to the bindingCollection, instead you have to add it to a method:
First part which you already had:
Dim bindingElement1 As ConfigurationElement = bindingsCollection.CreateElement("binding")
bindingElement1("protocol") = "https"
bindingElement1("bindingInformation") = "192.168.1.1:443:contoso.com"
bindingsCollection.Add(bindingElement1)
After that simply add:
Dim method = bindingElement1.Methods.Item("AddSslCertificate").CreateInstance()
method.Input.Attributes.Item("certificateHash").Value = "EC8BCFF70983EA26BFEA087683329CB8C07366A5"
method.Input.Attributes.Item("certificateStoreName").Value = "MY"
method.Execute()
Commit changes:
serverManager.CommitChanges()
So in total with some error-catching it could look like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim serverManager As ServerManager = New ServerManager
Dim config As Configuration = serverManager.GetApplicationHostConfiguration
Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "contoso")
If (siteElement Is Nothing) Then
MsgBox("Element not found!")
End If
Dim bindingsCollection As ConfigurationElementCollection = siteElement.GetCollection("bindings")
Dim bindingElement1 As ConfigurationElement = bindingsCollection.CreateElement("binding")
bindingElement1("protocol") = "https"
bindingElement1("bindingInformation") = "192.168.1.1:443:contoso.com"
Try
bindingsCollection.Add(bindingElement1)
Catch ex As Exception : MsgBox(ex.Message) : End Try
Dim method = bindingElement1.Methods.Item("AddSslCertificate").CreateInstance()
method.Input.Attributes.Item("certificateHash").Value = "EC8BCFF70983EA26BFEA087683329CB8C07366A5"
method.Input.Attributes.Item("certificateStoreName").Value = "MY"
Try
method.Execute()
Catch ex As Exception : MsgBox(ex.Message) : End Try
serverManager.CommitChanges()
End Sub
Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
For Each element As ConfigurationElement In collection
If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
Dim matches As Boolean = True
Dim i As Integer
For i = 0 To keyValues.Length - 1 Step 2
Dim o As Object = element.GetAttributeValue(keyValues(i))
Dim value As String = Nothing
If (Not (o) Is Nothing) Then
value = o.ToString
End If
If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
matches = False
Exit For
End If
Next
If matches Then
Return element
End If
End If
Next
Return Nothing
End Function
Is there a way to detect from an application written in c# if it's being launched as a remote app using RDP?
Get the parent of your application's process and check if it's raised by rdpinit.exe. If so, it's a RemoteApp.
Quick example for getting the parent-process-id (sorry, vb.net):
<Extension()>
Public Function GetParentProcessId(process As Process) As Integer
If process Is Nothing Then Throw New NullReferenceException()
Dim parentProcessId As Integer
Dim snapShot As IntPtr = IntPtr.Zero
Try
snapShot = CreateToolhelp32Snapshot(SnapshotFlags.Process, 0)
If snapShot <> IntPtr.Zero Then
Dim procEntry As New PROCESSENTRY32
procEntry.dwSize = CUInt(Marshal.SizeOf(GetType(PROCESSENTRY32)))
If Process32First(snapShot, procEntry) Then
Do
If process.Id = procEntry.th32ProcessID Then
parentProcessId = CInt(procEntry.th32ParentProcessID)
Exit Do
End If
Loop While Process32Next(snapShot, procEntry)
End If
End If
Catch ex As Exception
Throw
Finally
If snapShot <> IntPtr.Zero Then
CloseHandle(snapShot)
End If
End Try
Return parentProcessId
End Function
Now you can get the parent-process easily.
Regards,
Jan
I am getting very strange error, worked out for last couple of hours to fix this
"Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb. "
Public Shared Sub DeleteListItem(ByVal listname As SPList, ByVal intItemID As Integer)
Using MySite As New SPSite(SPContext.GetContext(System.Web.HttpContext.Current).Web.Url)
Using MyWeb As SPWeb = MySite.OpenWeb()
MyWeb.AllowUnsafeUpdates = True
Dim itemColforGivenList As SPListItemCollection
Dim query As New SPQuery()
query.Query = "<Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>" &
intItemID & "</Value></Eq></Where>"
MyWeb.AllowUnsafeUpdates = True
itemColforGivenList = listname.GetItems(query)
If itemColforGivenList.Count > 0 Then
For i As Integer = listname.Items.Count - 1 To 0 Step -1
If listname.Items(i).ID = intItemID Then
MyWeb.AllowUnsafeUpdates = True
listname.Items.Delete(i)
listname.Update()
MyWeb.AllowUnsafeUpdates = False
End If
Next
End If
End Using
End Using
Please help me out
Have you tried calling MyWeb.Update(); directly after MyWeb.AllowUnsafeUpdates = true; ? It's possible that the listname.Update() call is checking this property by pulling directly from the web and not the object representing that web you have in the current context. This does create a couple concerns as you will need to call update on that web/list multiple times to enable and disable the property and also delete the item so keep that in mind.