Retrieve full path of FTP file on drag & drop? - vb.net

I can read the filename using next code when dragging a file from an Ftp folder browsed on Windows explorer.
But is there a way to retrieve the full Ftp path?
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Dim filename As String = ""
If e.Data.GetDataPresent("UniformResourceLocator") Then
Dim ioStream As System.IO.Stream = DirectCast(e.Data.GetData("FileGroupDescriptor"), System.IO.Stream)
Dim contents As Byte() = New [Byte](511) {}
ioStream.Read(contents, 0, 512)
ioStream.Close()
Dim sb As New System.Text.StringBuilder()
Dim i As Integer = 76
While contents(i) <> 0
sb.Append(CChar(ChrW(contents(i))))
i += 1
End While
filename = sb.ToString()
End If
End Sub

If the data dropped contains a UniformResourceLocator format, you can get the entire URL from that, for example:
Private Sub Form1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
If e.Data.GetDataPresent("UniformResourceLocator") Then
Dim URL As String = New IO.StreamReader(CType(e.Data.GetData("UniformResourceLocator"), IO.MemoryStream)).ReadToEnd
End If
End Sub
It first checks to see if a UniformResourceLocator format exists, and if so, gets the data from e (the drag/drop argument), converts it to a MemoryStream, and passes that to a new StreamReader (for easy reading), then does a .ReadToEnd() to get the entire string.

Related

Copy a File Using DragDrop VB.Net

What is wrong with my code? Getting a 'Process cannot access the file because it is being used by another process' error msg Is there a way around this. My google-fu was not giving me much luck. I was not able to Move or Copy, and I will take either.
Private Sub frmFiberTransMain_DragEnter(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub frmFiberTransMain_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim filePaths As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
Call CopyFileDrop(filePaths)
End If
End Sub
Private Sub CopyFileDrop(filePaths As String())
For Each fileLoc As String In filePaths
Dim fileName As String = fileLoc
Dim fi As New IO.FileInfo(fileName)
File.Create(fileName)
Dim justFileName As String = fi.Name
Dim newPathName As String = gProgDir & "\" & justFileName
Directory.Move(fileLoc, newPathName)
Next fileLoc
End Sub
File.Create(fileName) returns an open handle and you're not closing it. It doesn't look like you need that line. –
#LarsTech 50 mins ago

How do I output from a PowerShell script (PSObjects?) to a WinForms TextBox in real-time?

I am executing a PowerShell script from a Visual Basic WinForms UI, and I managed to code it so it executes on a BackgroundWorker thread so that the UI doesn't lock up while the script is running. What it does is imports a .printerExport file to add printers and drivers to a target host, and it runs great. The only issue is that I set the output of the PSObjects to a TextBox and they all get output at the end once the script is completed, rather than output in real time like it would in a PowerShell console window.
I have tried multiple things to get it to output in real-time, but I am out of ideas, and even ReportProgress doesn't manage to get the real-time output as well.
How can I get this done? Below are the three involved Sub functions I am currently using:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim HostName As String
Dim PrintName As String
Dim SourceFilePath As String
Dim DestinationPath As String
Dim FileName As String
HostName = txtHostName.Text
PrintName = "\\" & txtHostName.Text
SourceFilePath = txtFilePath.Text
DestinationPath = PrintName & "\c$\Temp\"
If String.IsNullOrEmpty(HostName) Or String.IsNullOrEmpty(SourceFilePath) Then
MessageBox.Show("Please enter the target host name and choose a file path.")
Return
End If
FileName = Path.GetFileName(SourceFilePath)
File.Copy(SourceFilePath, Path.Combine(DestinationPath, Path.GetFileName(SourceFilePath)), True)
Dim PsEnv As New RunspaceInvoke
Dim App As String = $"Invoke-Command -ComputerName {HostName} {{C:\Windows\System32\spool\tools\Printbrm.exe -r -s {PrintName} -f ""C:\Temp\{FileName}""}}"
Dim AppObjects As Collection(Of PSObject) = PsEnv.Invoke(App)
Dim Output As New StringBuilder()
Dim id As Integer = 0
For Each psobj2 As PSObject In AppObjects
id += 1
BackgroundWorker1.ReportProgress(id, psobj2.ToString() & vbCrLf & vbCrLf)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim userState As String = CType(e.UserState, String)
TextBox3.AppendText(userState)
End Sub
Private Sub buttonInstall_Click(sender As Object, e As EventArgs) Handles buttonInstall.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub

VB.net invalid length for a base 64 char array or string

I have an issue with conversion from a base-64 string to normal, readable text. I did some research and found that base 64 strings have to be of a length that is a multiple of 4. So I used padRight to give it a valid length, but I keep getting the same error. For example, I enter "hi" and it encodes as "⚫aGk====", which seems like 8 characters to me (which is obviously a multiple of 4). When I try to read it, it reads in with a length of 1.
I'm also using a custom file extension that I just called ".bgs". I'm not sure if that does anything. Writing to this file as a base64 string and reading/decoding it is the only thing I'm trying to do.
Here is my code:
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using bs As New BinaryWriter(File.Open("saveFile.bgs", FileMode.Create))
Dim originText As String = TextBox1.Text
Dim cipherText As String
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(originText)
cipherText = Convert.ToBase64String(byteArray)
Dim realLength As Integer = cipherText.Length() + 1
Dim len As Integer = (realLength Mod 4)
If (len > 0) Then bs.Write(cipherText.PadRight(realLength + (3 - len), "="))
bs.Close()
End Using
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Using bs As New BinaryReader(File.Open("saveFile.bgs", FileMode.Open))
Dim cipherText2 As String = bs.Read()
Dim originText2 As String = "Failed"
TextBox2.Text = cipherText2.Length() 'reports length of 1
Try
Dim byteArray2 As Byte() = Convert.FromBase64String(cipherText2)
originText2 = System.Text.Encoding.UTF8.GetString(byteArray2)
Catch ex As Exception
End Try
'TextBox2.Text = originText2
End Using
End Sub
Any help is much appreciated!
Update: it looks like the first character (the dot in the case above) seen in the .bgs file when I open it with notepad controls the contents of cipherText2, which is just a number, explaining why the length is so low.
Base64 encodes using only printable ASCII characters.
You are seeing the dot because you are using binary writer which prefixes strings with their length when writing to file.
Then you are using Read instead of ReadString so you read the string length as a number (which is then implicitly converted to string because you are not using Option Strict On like you should).
You can fix it by using ReadString instead of Read, but it would be easier if you used a text writer.
You also should not try to pad results of ToBase64String. It already gives you the correct string.
I would rewrite your code as:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim originText As String = TextBox1.Text
Dim cipherText As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(originText))
System.IO.File.WriteAllText("saveFile.bgs", cipherText, System.Text.Encoding.ASCII)
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim cipherText2 As String = System.IO.File.ReadAllText("saveFile.bgs", System.Text.Encoding.ASCII)
Dim originText2 As String = "Failed"
originText2 = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(cipherText2))
'TextBox2.Text = originText2
End Sub

Deleting Specific Files and then Extract them into another folder

With the following code I am trying to delete specific files inside of a folder on a flash drive, and then copy the remaining files into a separate folder. When the program runs and I initiate the button to do so, the program deletes files that have not been modified within the past year, but then it does not continue to extract the remaining files and place them into a separate folder.
Does anyone know why?
Imports System.IO
Public Class frmExtractionator
Dim txtFiles1 As Control
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim sourceDirectory As String = "E:\CopierFolderforTestDriveCapstone"
Dim archiveDirectory As String = "E:\FilesExtracted"
Try
DeleteUnmodifiedFiles(sourceDirectory, 365)
Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)
If (Not System.IO.Directory.Exists(archiveDirectory)) Then
System.IO.Directory.CreateDirectory(archiveDirectory)
End If
For Each currentFileLoc As String In txtFiles
Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
End Sub
Private Sub DeleteUnmodifiedFiles(ByVal directoryName As String, ByVal modificationThresholdDays As Integer)
Dim folder As New DirectoryInfo(directoryName)
Dim thresholdDate As Date
Dim wasModifiedSinceThreshold As Boolean
For Each file As FileInfo In folder.GetFiles
thresholdDate = DateTime.Now().AddDays(-1 * modificationThresholdDays)
wasModifiedSinceThreshold = (file.LastWriteTime > thresholdDate)
If (Not wasModifiedSinceThreshold) Then file.Delete()
Next
MessageBox.Show("Deleting Files")
End Sub
End Class
This will delete any file in the source directory that hasn't been modified for a year, and then will move any remaining files to the destination directory...
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim fileListA() As String
fileListA = (IO.Directory.GetFiles("C:\Scource_Directory"))
For Each i As String In fileListA
If (IO.File.GetLastWriteTime(i).ToShortDateString.Substring(6)) < (CType(DateTime.Now.Year.ToString, Integer) - 1) Then
IO.File.Delete(i)
End If
Next
Dim fileListB() As String
fileListB = (IO.Directory.GetFiles("C:\Scource_Directory"))
For Each i As String In fileListB
IO.File.Move(i, "Destination_Directory")
Next
End Sub

Trying to open a docx file from a bytestream - file corruption error

we consistently get a file corrupted error message when opening a docx file from a saved bytestream, evry other file type works just ok
Below is code from a sample form that replciate the issue
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Objective is to be able to copy a file to a bytestream then create a new document from that stream and then opne it.
'This replicates the behaviour of our primary application where it stores and retrieves the stream from a database
'With docx files we consistently experience some sort of corruption in the write of the original file
'When selecting .doc or other format files we do not encounter the same problem
'use selected file
Dim _o1 As String = TextBox1.Text
'get its bytestream
Dim fs As New FileStream(_o1, FileMode.Open, FileAccess.Read)
Dim byteStream(Convert.ToInt32(fs.Length)) As Byte
fs.Read(byteStream, 0, Convert.ToInt32(fs.Length))
'create a new file and use the bytestream to create it and save to disk
Dim _o As String = "C:\" & Now.Ticks & getNewFileName()
Dim fs1 As New FileStream(_o, FileMode.OpenOrCreate, FileAccess.Write)
Using bw As New BinaryWriter(fs1)
bw.Write(byteStream)
bw.Flush()
End Using
'open the new document
System.Diagnostics.Process.Start(_o)
Application.DoEvents()
End Sub
Private Function getNewFileName() As String
Dim fi As New FileInfo(TextBox1.Text)
Return Now.Ticks.ToString & fi.Name
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.Filter = "docx files |*.docx"
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
End If
End Sub
Forgive me, but that is some messed up code.
Dim _o As String = "C:\" & Now.Ticks & getNewFileName()
will become...
Dim _o As String = "C:\" & Now.Ticks & Now.Ticks.ToString & fi.Name
Example result "C:\" "634015010433498951" "634015010433498951" "FileName.txt" is probably not what you are expecting unless you intend to subtract the two tick counts to determine how long it took to populate FileInfo.
Your FileStream corruption could be a encoding issue, off by one file length issue, or even a long filename in a deep path could a problem. Instead of using FileStream, this code should work fine:
Dim sourceFile As String = TextBox1.text
Dim fi As New System.IO.FileInfo(sourceFile)
Dim destFile = "C:\" & Now.Ticks & fi.Name
fi.CopyTo(destFile)
'open the new document
System.Diagnostics.Process.Start(destFile)