Click here to Skip to main content
15,071,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a GUI in Vb.net to list all my powershell scripts and to be able to load and run easily at the touch of a button. I am looking to have an instruction screen to pull from the PowerShell Script on how to use the script. I am having issues just pulling only the text between <# #> at the top of the script.

What I have tried:

This works in C# "string commentText = ExtractStringFromFile(fullFileName, "<#", "#>") " but I am looking to get this to working VB.Net
Posted
Updated 19-Jun-20 16:30pm

There is no standard method called "ExtractStringFromFile" - so you will need to file that and either include the assembly in you project, or translate the C# code to VB.

We have no access to your system, so we can't even begin to guess where it is or how it works - so this one is up to you. Start with your C# project, put the mouse cursor over a call to the method, right click, and select "Go to definition". That may find it for you.
   
Comments
Maciej Los 22-Jun-20 5:09am
   
5ed!
Here is a working copy of what I found to work for what I needed in my VB.Net Project.

VB
Private Sub buttonExecuteScript_Click(sender As Object, e As EventArgs) Handles buttonExecuteScript.Click
        
        Me.richTextBoxScriptHeader.Clear()
        fullFileName = strFilePath 

        If Not String.IsNullOrEmpty(fullFileName) Then
            Dim commentText As String = ExtractStringFromFile(fullFileName, "<#", "#>")
            Me.richTextBoxScriptHeader.Text = commentText
        End If
        End Sub
Private Function ExtractStringFromFile(ByVal fullFileName As String, ByVal startMarker As String, ByVal endMarker As String) As String
        Dim result As String = String.Empty

        If Not File.Exists(strFilePath) Then
            Msgbox("The passed filename does not exist.")
        End If

        Dim fileText As String = File.ReadAllText(strFilePath)
        Dim startMarkerPosition As Integer = fileText.IndexOf(startMarker) + startMarker.Length
        Dim endMarkerPosition As Integer = fileText.IndexOf(endMarker)

        If endMarkerPosition > startMarkerPosition Then
            result = fileText.Substring(startMarkerPosition, endMarkerPosition - startMarkerPosition)
        End If

        Return result
End Function
   
v2
Comments
Maciej Los 22-Jun-20 5:11am
   
5ed!
Dim lstring As String = "murugesh #read this text# abc"
       Dim lary() As String
       lary = Split(lstring, "#")
       MsgBox(lary(1))
   
Comments
SkullDocDan 12-Jun-20 21:44pm
   
This does work correctly but I come across an extra # in the synopsis causing it to stop prematurely. Do you know of a way to have it read between <# and #> in VB.Net?
kgmmurugesh 13-Jun-20 4:20am
   
Dim lstring As String = "murugesh <#read this text#> abc"
Dim lary() As String
Dim lpos1, lpos2 As Integer
lpos1 = InStr(lstring, "<#") + 2
lpos2 = InStr(lstring, "#>")


MsgBox(Mid(lstring, lpos1, lpos2 - lpos1))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900