I would like to read each line of text from the file outputted from this block.
Each line is then used with the service controller to output a list of services running on that computer.
Dim de As New DirectoryEntry()
'Name place to write file to
Dim strFile As String = "C:\DomainUsers.txt"
Dim fileExists As Boolean = File.Exists(strFile)
'get list of all users on domain and write to file
de.Path = "WinNT://domain.com"
'Write each user to a text file as List
Using sw As New StreamWriter(File.Open(strFile, FileMode.OpenOrCreate))
MsgBox("Writing all Users to Memory, Click OK and wait for Window to Close")
For Each d As DirectoryEntry In de.Children()
sw.WriteLine(d.Name)
Next
Console.Clear()
End Using
'Reopen File and read each line
'Return List of Processes for each User in Overwritefashion to New Version of File
Using sr As New StreamReader(File.Open(strFile, FileMode.Open))
Dim InstalledServices As List(Of String) = GetInstalledServices(sr.ReadLine.Trim)
End Using
End Sub
'Lost as to how to go about using this function...
Function GetInstalledServices(ByVal Computer As String) As List(Of String)
End Function
You'll have to check it, but it'll be something like ...
Function GetInstalledServices(ByVal Computer As String) As List(Of String)
Dim RetVal as New List(Of String)
Dim scServices() As ServiceController()
Try
scServices = ServiceController.GetServices(Computer)
For Each Svc as ServiceController In scServices
RetVal.Add(Svc.ServiceName)
Next
Catch Ex as Exception
End Try
Return RetVal
End Function
Related
please how can I get count of duplicate lines?
Source data: line e.g. user_id;name;surname;3400;44711;30.05.2022 7:00:00;30.05.2022 15:30:00;0;480;0;1;682;10000120;9
Private Sub remove_duplicite(sender As Object, e As EventArgs)
Dim sFiles() As String
sFiles = Directory.GetFiles(filesPath1, remove_dupl)
Dim path As String = String.Join("", sFiles)
'MessageBox.Show(path)
Dim lines As New HashSet(Of String)()
'Read to file
Using sr As StreamReader = New StreamReader(path)
Do While sr.Peek() >= 0
lines.Add(sr.ReadLine())
Loop
End Using
'Write to file
Using sw As StreamWriter = New StreamWriter(path)
For Each line As String In lines
sw.WriteLine(line)
Next
End Using
Close()
End Sub
I try some answers but no success.But I think that will be easy.
Thank you
Dim sList As New List(of String)
sList.Add("1")
sList.Add("2")
sList.Add("2")
sList.Add("3")
Dim sListDistinct As List(Of String) = sList.Distinct().ToList()
Dim iCount as Integer = sList.Count - sListDistinct.Count
But depending on the size of your file, this isn't the best performance way.
Maybe check in your HashSet with .Contains and count if entry already exists
I know, the title of this question is horrible, however, let me explain what I'm trying to achieve.
Firstly, what does this program do?
The program is a remote access tool which uses a stub to build the client which is currently on Pastebin: https://pastebin.com/raw/jFwk2aAL
Pretty much, when the user who operates the server, (where all clients are added if they open the file) to build the program they press a button which says "Build". When the "Build" button has been clicked, this code is put into action:
Try
Dim dialog2 As New SaveFileDialog
dialog2.Filter = "Executables | *.exe"
dialog2.InitialDirectory = Application.StartupPath
dialog2.Title = "Select a path where the stub will be saved..."
If (dialog2.ShowDialog = DialogResult.OK) Then
Me.TR_Compile = New Thread(New ParameterizedThreadStart(AddressOf Me.lam__10))
Me.TR_Compile.Start(dialog2.FileName)
End If
dialog2 = Nothing
End If
Catch ex As Exception
Me.StatusBarMain.Text = ex.Message
End Try
Pretty much, this is where the server operator chooses where to save the file, being the stub (the file the client will open to appear on the server.)
Once the stub's location has been saved, this code is put into action:
Private Sub Compile(ByVal path As String)
Try
Me.Invoke(New DelegateAddOutput(AddressOf Me.AddOutput), New Object() {"Initializing Build..."})
Dim source As String = Conversions.ToString(FormMain.RijndaelDecrypt(New WebClient().DownloadString("https://pastebin.com/raw/jFwk2aAL"), "308c036ab4ae32ff202d417d1e15cb667326201f4754644509b9079a81")).Replace("127.0.0.1", Me.tb_ip.Text).Replace("4432", Conversions.ToString(Me.nud_port.Value)).Replace("CLIENTID", Me.tb_clientid.Text).Replace("Single_Mutex_App_Instance", Me.TB_Mutex.Text).Replace("ASSEMBLYTITLE", Me.tb_assemblytitle.Text).Replace("ASSEMBLYDESCRIPTION", Me.tb_assemblydescription.Text).Replace("ASSEMBLYCOMPANY", Me.tb_assemblycompany.Text).Replace("ASSEMBLYPRODUCT", Me.tb_assemblyproduct.Text).Replace("ASSEMBLYCOPYRIGHT", Me.tb_assemblycopyright.Text).Replace("ASSEMBLYTRADEMARK", Me.tb_assemblytrademark.Text).Replace("3.5.2.4", String.Format("{0}.{1}.{2}.{3}", New Object() {Me.NumericUpDown9.Value, Me.NumericUpDown2.Value, Me.NumericUpDown3.Value, Me.NumericUpDown4.Value})).Replace("0.0.0.0", String.Format("{0}.{1}.{2}.{3}", New Object() {Me.NumericUpDown8.Value, Me.NumericUpDown7.Value, Me.NumericUpDown6.Value, Me.NumericUpDown5.Value})).Replace("L$01", Conversions.ToString(Me.CheckBox1.Checked)).Replace("L$02", Conversions.ToString(Me.RadioButton1.Checked)).Replace("L$03", Conversions.ToString(Me.RadioButton2.Checked)).Replace("L$04", Conversions.ToString(Me.RadioButton3.Checked)).Replace("L$05", Me.TextBox1.Text).Replace("L$06", Conversions.ToString(Interaction.IIf((Strings.Right(Me.TextBox2.Text, 4) = ".exe"), Me.TextBox2.Text, (Me.TextBox2.Text & ".exe")))).Replace("L$07", Conversions.ToString(Me.CheckBox2.Checked)).Replace("L$08", Conversions.ToString(Interaction.IIf((Strings.Right(Me.TextBox3.Text, 1) = "\"), Me.TextBox3.Text, (Me.TextBox3.Text & "\")))).Replace("L$09", Conversions.ToString(Me.CheckBox6.Checked)).Replace("L$10", Conversions.ToString(Me.CheckBox3.Checked)).Replace("L$11", Conversions.ToString(Me.CheckBox5.Checked)).Replace("L$12", Conversions.ToString(Me.CheckBox7.Checked)).Replace("L$13", Conversions.ToString(Convert.ToInt32(Me.NumericUpDown1.Value)))
Me.Invoke(New DelegateAddOutput(AddressOf Me.AddOutput), New Object() {"Writing parameter to stub..."})
Dim compiler As ICodeCompiler = New VBCodeProvider().CreateCompiler
Dim options As New CompilerParameters
options.ReferencedAssemblies.Add("System.dll")
options.ReferencedAssemblies.Add("System.Windows.Forms.dll")
options.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
options.ReferencedAssemblies.Add("System.Management.dll")
options.ReferencedAssemblies.Add("System.Drawing.dll")
options.ReferencedAssemblies.Add("System.ServiceProcess.dll")
options.ReferencedAssemblies.Add("AForge.dll")
options.ReferencedAssemblies.Add("AForge.Controls.dll")
options.ReferencedAssemblies.Add("AForge.Video.DirectShow.dll")
options.ReferencedAssemblies.Add("AForge.Video.dll")
options.GenerateExecutable = True
options.OutputAssembly = path
options.CompilerOptions = "/target:winexe /platform:x86 /optimize+ /filealign:512"
Dim parameters2 As CompilerParameters = options
parameters2.CompilerOptions = Conversions.ToString(Operators.AddObject(parameters2.CompilerOptions, Interaction.IIf(Operators.ConditionalCompareObjectEqual(Me.PictureBox1.Tag, "", False), "", Operators.ConcatenateObject(" /win32icon:", Me.PictureBox1.Tag))))
Dim results As CompilerResults = compiler.CompileAssemblyFromSource(options, source)
Me.Invoke(New DelegateAddOutput(AddressOf Me.AddOutput), New Object() {"Stub builded..."})
Me.Invoke(New DelegateAddOutput(AddressOf Me.AddOutput), New Object() {"Merging Assemblies..."})
Dim startInfo As New ProcessStartInfo("ILMerge.exe")
startInfo.Arguments = String.Concat(New String() {"/target:winexe /out:""", path, """ """, path, """ AForge.Controls.dll AForge.dll AForge.Video.DirectShow.dll AForge.Video.dll"})
startInfo.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(startInfo)
Thread.Sleep(&HBB8)
Me.Invoke(New DelegateAddOutput(AddressOf Me.AddOutput), New Object() {"Finished build !"})
File.Delete(path.Replace(".exe", ".pdb"))
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
Me.Invoke(New DelegateAddOutput(AddressOf Me.AddOutput), New Object() {exception.Message})
ProjectData.ClearProjectError()
End Try
End Sub
In the "Compile" sub, the program downloads a paste made on pastebin which contains some encrypted characters. Then, the program uses this function:
Public Shared Function RijndaelDecrypt(ByVal UDecryptU As String, ByVal UKeyU As String) As Object
Dim managed As New RijndaelManaged
Dim salt As Byte() = New Byte() {1, 2, 3, 4, 5, 6, 7, 8}
Dim bytes As New Rfc2898DeriveBytes(UKeyU, salt)
managed.Key = bytes.GetBytes(managed.Key.Length)
managed.IV = bytes.GetBytes(managed.IV.Length)
Dim stream2 As New MemoryStream
Dim stream As New CryptoStream(stream2, managed.CreateDecryptor, CryptoStreamMode.Write)
Try
Dim buffer As Byte() = Convert.FromBase64String(UDecryptU)
stream.Write(buffer, 0, buffer.Length)
stream.Close()
UDecryptU = Encoding.UTF8.GetString(stream2.ToArray)
Catch exception1 As Exception
End Try
Return UDecryptU
End Function
to decrypt the text which is held on pastebin. It works great, however, I want to add my own functions to the stub, but, I can't, because the text is encrypted. So...
How can I decrypt this text, edit it myself, then, encrypt it again, upload it to a different paste on pastebin, but all being that my program can decrypt the text and write it too the stub successfully?
PLEASE COMMENT BELOW IF SOMETHING ISN'T CLEAR TO YOU.
Edit: Changed some text to make this question make sense more.
During parsing CSV file as a stream from Azure Blob, TextFieldParser always reaches EndOfData immediately, without any data read. The same code, but with the path to same physical file instead of stream works.
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(AzureStorageConnection)
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
Dim BlobList As IEnumerable(Of CloudBlockBlob) = blobClient.GetContainerReference("containername").ListBlobs().OfType(Of CloudBlockBlob)
For Each blb In BlobList
Dim myList As New List(Of MyBusinessObject)
Using memoryStream = New MemoryStream()
blb.DownloadToStream(memoryStream)
Using Reader As New FileIO.TextFieldParser(memoryStream)
Reader.TextFieldType = FileIO.FieldType.FixedWidth
Reader.SetFieldWidths(2, 9, 10)
Dim currentRow As String()
While Not Reader.EndOfData
Try
currentRow = Reader.ReadFields()
myList.Add(New GsmXFileRow() With {
' code to read currentRow and add elements to myList
})
Catch ex As FileIO.MalformedLineException
End Try
End While
End Using
End Using
Next
I have also tried to convert MemoryStream to TextReader
Dim myTextReader As TextReader = New StreamReader(memoryStream)
and then passing myTextReader into TextFieldParser, but this does not work either.
Using Reader As New FileIO.TextFieldParser(myTextReader)
I see this:
Value of Length property equals file size
and this:
'Position` property has same value
That means at the start of the loop, the MemoryStream has already advanced to the end of the stream. Just set Position back to 0, and you should be in a better place.
However, there may be another issue here, too. That stream data is binary with some unknown encoding. The TextFieldParser wants to work with Text. You need a way to give the TextFieldParser information about what encoding is used.
In this case, I recommend a StreamReader. This type inherits from TextReader, so you can use it with the TextFieldParser :
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(AzureStorageConnection)
Dim blobClient As CloudBlobClient = storageAccount.CreateCloudBlobClient()
Dim BlobList As IEnumerable(Of CloudBlockBlob) = blobClient.GetContainerReference("containername").ListBlobs().OfType(Of CloudBlockBlob)
Dim myList As New List(Of MyBusinessObject)
For Each blb In BlobList
'Several constructor overloads allow you to specify the encoding here
Using blobData As New StreamReader(New MemoryStream())
blb.DownloadToStream(blobData.Stream)
'Fix the position problem
blobData.Stream.Position = 0
Using Reader As New FileIO.TextFieldParser(blogData)
Reader.TextFieldType = FileIO.FieldType.FixedWidth
Reader.SetFieldWidths(2, 9, 10)
Dim currentRow As String() = Reader.ReadFields()
While Not Reader.EndOfData
Try
myList.Add(New GsmXFileRow() With {
' code to read currentRow and add elements to myList
})
currentRow = Reader.ReadFields()
Catch ex As FileIO.MalformedLineException
End Try
End While
End Using
End Using
Next
Public Sub openDB()
Dim Lines As New List(Of String)
Try
' Open the file using a stream reader.
Using sr As New StreamReader("Config.txt")
Dim line As String
' Read the stream to a string and write the string to the console.
line = sr.ReadLine()
Do Until String.IsNullOrEmpty(line)
Lines.Add(line)
line = sr.ReadLine
Loop
End Using
Catch e As Exception
Console.WriteLine("The file could not be read:")
Console.WriteLine(e.Message)
End Try
Dim dbname As String = g_DatabaseName
Dim server As String = Lines.Where(Function(str) str.Contains("server =")).ToString
Dim user As String = ""
Dim password As String = ""
conn = New MySqlConnection
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false; Convert Zero Datetime=True", server, user, password, dbname)
conn.Open()
End Sub
Im try to return some string from a text file, so I use StreamReader to read the file and store them into a list. Now I try to declare a variable to get "localhost" from list of string, but the code below is not work for me.
Dim server As String = Lines.Where(Function(str) str.Contains("server
=")).ToString
Enumerable.Where does not return a single string but possibly multiple, using ToString gives you not the first matching line but just the name of the type which is System.Linq.Enumerable+WhereArrayIterator1[System.String].
Either declare it as IEnumerable(Of String) or use First/ FirstOrDefault to get the first line that matches the condition:
Dim serverLine As String = Lines
.Where(Function(str) str.Contains("server ="))
.FirstOrDefault()
You can also use the overload of FirstOrDefault(Nothing if there was no such line):
Dim serverLine As String = Lines.FirstOrDefault(Function(str) str.Contains("server ="))
To extract Localhost:
Dim server As String = serverLine.Substring(serverLine.IndexOf("server =") + "server =".Length).Trim(""""c, " "c)
Learning and testing using Sql FILESTREAM for a web app. A client uploads a large file form the web page which takes 'X' time and when fully uploaded shows 100% complete. However, very large files also take time for SqlFileStream to write to the file system so I want to spin off a thread to complete that part. The code I've got seems to work fine but no data ends up in the filestream file.
I'm wrapping the initial record creation in it's own transaction scope and using a separate transaction scope in the thread. In the threaded routine I have the appropriate PathName() and TransactionContext but I assume I'm missing something while using a thread.
I've commented out the normal SqlFileStream call which works fine. Can you see anything wrong with what I'm doing here?
Public Function StoreFileStream()
Dim json As New Dictionary(Of String, Object)
Dim parms As New FileStreamThreadParameters
If HttpContext.Current.Request.Files.Count > 0 Then
Dim file As HttpPostedFile = HttpContext.Current.Request.Files(0)
If "contentType" <> String.Empty Then
Dim fs As Stream = file.InputStream
Dim br As New BinaryReader(fs)
Dim noBytes As New Byte()
Try
Dim filePath As String = ""
Dim trxContext As Byte() = {}
Dim baseFileId As Integer
Using trxScope As New TransactionScope
Using dbConn As New SqlConnection(DigsConnStr)
Using dbCmd As New SqlCommand("ADD_FileStreamFile", dbConn)
dbConn.Open()
Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
dbRdr.Read()
If dbRdr.HasRows Then
filePath = dbRdr("Path")
trxContext = dbRdr("TrxContext")
baseFileId = dbRdr("BaseFileID")
End If
dbRdr.Close()
End Using
' Code below writes to file, but trying to offload this to a separate thread so user is not waiting
'Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
' fs.CopyTo(dest, 4096)
' dest.Close()
'End Using
End Using
dbConn.Close()
End Using
trxScope.Complete()
End Using ' transaction commits here, not in line above
parms.baseFileId = baseFileId
parms.fs = New MemoryStream
fs.CopyTo(parms.fs)
Dim fileUpdateThread As New Threading.Thread(Sub()
UpdateFileStreamThreaded(parms)
End Sub)
fileUpdateThread.Start()
json.Add("status", "success")
Catch ex As Exception
Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
json.Add("status", "failure")
json.Add("msg", ex.Message)
json.Add("procedure", System.Reflection.MethodBase.GetCurrentMethod.Name)
End Try
Else
json.Add("status", "failure")
json.Add("msg", "Invalid file type")
json.Add("procedure", System.Reflection.MethodBase.GetCurrentMethod.Name)
End If
End If
Return json
End Function
Public Class FileStreamThreadParameters
Public Property baseFileId As Integer
Public fs As Stream
End Class
Private Sub UpdateFileStreamThreaded(parms As FileStreamThreadParameters)
Dim filePath As String = ""
Dim trxContext As Byte() = {}
Try
Using trxScope As New TransactionScope
Using dbConn As New SqlConnection(DigsConnStr)
Using dbCmd As New SqlCommand("SELECT FileBytes.PathName() 'Path', GET_FILESTREAM_TRANSACTION_CONTEXT() 'TrxContext' FROM FileStreamFile WHERE Id = " & parms.baseFileId, dbConn)
dbConn.Open()
Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
dbRdr.Read()
If dbRdr.HasRows Then
filePath = dbRdr("Path")
trxContext = dbRdr("TrxContext")
End If
dbRdr.Close()
Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
parms.fs.CopyTo(dest, 4096)
dest.Close()
End Using
End Using
End Using
dbConn.Close()
End Using
trxScope.Complete()
End Using
Catch ex As Exception
'Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
End Try
End Sub
Obviously this is a complex issue. I actually got it to work with the code below. However I eventually abandoned using SQL FILESTREAM altogether due to too much complexity in getting it all set up.
This is an existing web application with the sql server on a different box. After I got the filestreaming to work the next hurdle was authentication setup. Filestream requires Integrated Security on your connection string. Even with windows authentication on our Intranet app, I could not get the web app to use the windows credentials when connecting to the database. It always seemed to use the computer name or the app pool service. I tried many many examples I found on the net and here to no avail. Even if I got that to work then I would want to use and Active Directory group over individual logins which looked to be another hurdle.
This app stores documents in a varbinary column so that full text search can be enabled at some point. The issue was with large files which are generally pictures or videos. Since you can't search text on those anyway the strategy now is to store those on the file system and all other searchable docs (.docx, .pptx, etc) will still be stored in the varbinary. I'm actually sad that I could not get filestream to work as it seems like the ideal solution. I'll come back to it some day but it really should not be so frickin complicated. :-(
The code I got working is:
Dim filePath As String = ""
Dim trxContext As Byte() = {}
Dim baseFileId As Integer
Using trxScope As New TransactionScope
Using dbConn As New SqlConnection(DigsFSConnStr)
Using dbCmd As New SqlCommand("NEW_FileStreamBaseFile", dbConn)
dbCmd.CommandType = CommandType.StoredProcedure
dbCmd.Parameters.AddWithValue("#Title", fileDesc)
dbCmd.Parameters.AddWithValue("#Summary", summary)
dbCmd.Parameters.AddWithValue("#Comments", comments)
dbCmd.Parameters.AddWithValue("#FileName", uploadedFileName)
dbCmd.Parameters.AddWithValue("#ContentType", contentType)
dbCmd.Parameters.AddWithValue("#FileExt", ext)
'dbCmd.Parameters.AddWithValue("#FileBytes", noBytes) ' now that were offloading the file byte storage to a thread
dbCmd.Parameters.AddWithValue("#UploadedByResourceID", uploadedByResourceID)
dbCmd.Parameters.AddWithValue("#UploadedByShortName", uploadedByShortName)
dbCmd.Parameters.AddWithValue("#FileAuthor", fileAuthor)
dbCmd.Parameters.AddWithValue("#TagRecID", tagRecID)
dbCmd.Parameters.AddWithValue("#UserID", samAccountName)
dbCmd.Parameters.AddWithValue("#FileDate", fileDate)
dbCmd.Parameters.AddWithValue("#FileType", fileType)
dbCmd.Parameters.AddWithValue("#FileTypeRecID", fileTypeRecId)
' Save to file system too for xod conversion
file.SaveAs(HttpContext.Current.Server.MapPath("~/files/uploaded/") & uploadedFileName)
dbConn.Open()
Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
dbRdr.Read()
If dbRdr.HasRows Then
filePath = dbRdr("Path")
trxContext = dbRdr("TrxContext")
json.Add("baseFileId", dbRdr("BaseFileID"))
virtualFileRecId = dbRdr("VirtualFileRecID")
dbStatus = dbRdr("status")
If dbStatus = "failure" Then
json.Add("msg", dbRdr("msg"))
End If
End If
dbRdr.Close()
End Using
' Prepare and start Task thread to write the file
If dbStatus = "success" Then
bytes = br.ReadBytes(fs.Length)
Dim task As New System.Threading.Tasks.Task(
Sub()
UpdateNewFileStreamBytes(virtualFileRecId, bytes)
End Sub)
task.Start()
json.Add("status", "success")
Else
json.Add("status", "failure")
End If
End Using
dbConn.Close()
End Using
trxScope.Complete()
End Using ' transaction commits here, not in line above
With the task procedure of:
Private Sub UpdateNewFileStreamBytes(virtualFileRecId As Integer, fileBytes As Byte())
Dim filePath As String = ""
Dim trxContext As Byte() = {}
Try
Using trxScope As New TransactionScope
Using dbConn As New SqlConnection(DigsFSConnStr)
Using dbCmd As New SqlCommand("UPD_FileStreamBaseFile", dbConn)
dbCmd.CommandType = CommandType.StoredProcedure
dbCmd.Parameters.AddWithValue("#VirtualFileRecID", virtualFileRecId)
dbConn.Open()
Using dbRdr As SqlDataReader = dbCmd.ExecuteReader(CommandBehavior.SingleRow)
dbRdr.Read()
If dbRdr.HasRows Then
filePath = dbRdr("Path")
trxContext = dbRdr("TrxContext")
End If
dbRdr.Close()
Using dest As New SqlFileStream(filePath, trxContext, FileAccess.Write)
dest.Write(fileBytes, 0, fileBytes.Length)
dest.Close()
End Using
End Using
End Using
dbConn.Close()
End Using
trxScope.Complete()
End Using
Catch ex As Exception
Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
End Try