Releasing an image file after display - vb.net

My app repeatedly displays a TIF file from a directory and closes them.
But when I try to rename the file later, VB says that it is in use by another process.
Here is the code:
Imports System.IO
Dim imgRight As Image
Dim fileRight as String
fileRight = "C:\1_Audit\PD_SE-4423344\PD_SE-4423344_0008.tif"
imgRight = Image.FromFile(fileRight)
Me.PicBoxRight.Image = imgRight
.
.
.
Me.PicBoxRight.Image.Dispose()
Me.PicBoxRight.Image = Nothing
imgRight.Dispose()
.
.
.
oldFileName = fileRight
newFileName = "PD_SE-4423344_0006.tif"
My.Computer.FileSystem.RenameFile(oldFileName, newFileName)
What do I have to do to release fileRight???

Related

Saving image from php id URL using webbrowser in VB.net

I am trying to save a image from webbrowser but can't find an answer for it.
Example of the problem is the URL is hxxp://domain[.]com/image/?img=1234 on webbrowser it shows the image and the source is <img src='hxxp://domain[.]com/image/?img=1234'>..
I was unable to save it using the My.Computer.Network.DownloadFile and MemoryStream(tClient.DownloadData methods.
In order to download the file, I will need to session cookie also?
How can I do this?
Any image that you see in the WebBrowser has already been downloaded to your computer. It is normally cached in the folder:
C:\Users\<username>\AppData\Local\Microsoft\Windows\Temporary Internet Files
or whatever other folder is set in Internet Options. You can get your image from that folder.
The following program shows how to copy file TDAssurance_Col.png from the Temporary Internet Files folder to the C:\Temp folder.
Module Module1
Sub Main()
CopyFileFromTemporaryInternetFolder("TDAssurance_Col.png", "C:\Temp")
End Sub
Public Sub CopyFileFromTemporaryInternetFolder(filename As String, destinationFolder As String)
' Search actual path of filename.
Dim temporaryInternetFilesFolder As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "Content.IE5")
Dim pattern As String = Path.GetFileNameWithoutExtension(filename) & "*" & Path.GetExtension(filename)
Dim pathnames() As String = Directory.GetFiles(temporaryInternetFilesFolder, pattern, SearchOption.AllDirectories)
' If file found, copy it.
If pathnames.Count > 0 Then
File.Copy(pathnames(0), Path.Combine(destinationFolder, filename))
End If
End Sub
End Module

how do I use a document using debug folder (vb.net)

I am trying to show a document (called zzz.txt so it appears at the bottom) in a text box, and I have put it in the debug folder (TextViewerthingy > obj > Debug) and have used CurDir() & "\zzz.txt" for the location, which apparently is not correct. I have used this correctly with other projects
Dim filename As String = CurDir() & "\zzz.txt"
Dim ObjReader As New System.IO.StreamReader(filename)
gives me an error saying the file is not there. This is the location, what is it that I'm doing wrong in this case?
C:\Users\Notshowingmyname\source\repos\TextViewerthingy\TextViewerthingy\bin\Debug
Try this one :
Imports System.IO
Dim filename as string = Directory.GetCurrentDirectory() + "\zzz.txt"
Or second alternative :
filename = My.Computer.FileSystem.CurrentDirectory + "\zzz.txt"
Make sure file name and extensions are correct.

How can I automatically generate filenames before saving it in a folder VB .NET

I have a program that saves the image file path to the database. The image is then save to a specific folder where it is being retrieved by the file path saved in the database. However, I cannot save an image with the same filename because it is already in the folder.
Here is my code in copying the image into the folder:
Private Sub ButtonBrowseImage_Click(sender as System.Object, e As System.EventArgs) Handles ButtonBrowseImage.Click
Dim opf as New OpenFileDialog
opf.Filter="Choose Image(*.JPG, *.PNG, *.GIF)|*.jpg; *.png; *.gif"
If opf.ShowDialog=Windows.Forms.DialogResult.Ok Then
If opf.CheckFileExists Then
Dim paths As String = Application.StartupPath.Substring (0, (Application.StartupPath.Length - 10 ))
Dim CorrectFileName As String = System.IO.Path.GetFileName(opf.filename)
System.IO.File.Copy(opf.FileName, paths + "\\artwork\\" + CorrectFileName)
End If
End If
Is there any way to automatically generate filename or overwrite the saved image file with the same image but with a different filename?
To generate random file name, you can use:
Path.GetRandomFileName()
Here is an example:
Imports System.IO
Module Module1
Sub Main()
Dim result = Path.GetRandomFileName()
Console.WriteLine("Random file name is " + result)
End Sub
End Module
' This code produces output similar to the following:
' Random file name is w143kxnu.idj
' Press any key to continue . . .

Windows Universal App Error while copying files

I try to copy all files from one folder to a folder on my USB stick.
In the source directory are some XML files and some JPG files.
File1.xml
Picture1-1.jpg
Picture1-2.jpg
File2.xml
Picture2.jpg
This is my code so far:
Private Async Sub btnCopyToUSB_Click(sender As Object, e As RoutedEventArgs) Handles btnCopyToUSB.Click
Dim FolderPick As Windows.Storage.Pickers.FolderPicker = New Windows.Storage.Pickers.FolderPicker
Dim USBDirectory As String
FolderPick.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder
FolderPick.FileTypeFilter.Add(".xml")
Dim Folder As Windows.Storage.StorageFolder = Await FolderPick.PickSingleFolderAsync
Dim DestinationFolder As Windows.Storage.StorageFolder
Dim existingFile As StorageFile
Dim copiedFile As StorageFile
If Not (Folder Is Nothing) Then
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PlatinaFolderToken", Folder)
USBDirectory = ProjectID & "-" & DocumentType ' ProjectID="ABC" DocumentType="XYZ" 'Directory "ABC-XYZ" already exists
DestinationFolder = Await Folder.GetFolderAsync(USBDirectory)
Dim existingFiles As IReadOnlyList(Of StorageFile) = Await Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync
For Each existingFile In existingFiles
copiedFile = Await existingFile.CopyAsync(DestinationFolder, existingFile.Name, NameCollisionOption.ReplaceExisting)
Next
End If
End Sub
The first file is copied (File1.xml).
When copying the second file (Picture1-1.jpg) it raises an exception
Error HRESULT E_FAIL has been returned from a call to a COM component.
What is my problem here? When I rename the files or add another file (a.xml) so it is alphabetically in front of File1.xml then a.xml is copied and File1.raises the error. So only the first file is copied.
Have you checked your destination USB drive? I had this error while writing on a corrupt USB drive. Aber formatting it worked like a charm.
Check if you can access the files using Windows Explorer or copy from cmd shell
use this:
File.Copy(Path.Combine(existingFile.Name, DestinationFolder, True)
instead of :
copiedFile = Await existingFile.CopyAsync(DestinationFolder, existingFile.Name, NameCollisionOption.ReplaceExisting)

VB.net Return String with Path of folder

My program needs to read an XML file that a software vendor sent me in order to complete a process. The problem is I cannot tell the program where the file is locate!
When i publish the program and install the program it generates a random folder every time it installs
Location the same folder name is always different
C:\Users\Ray\AppData\Local\Apps\2.0\6ZNVVG8V.C6O\0MELQPL9.LCB\lol-..tion_531c8308fa0ff83d_0001.0000_5a2aee0cd0a667c1
I Have figured out how to get that folder to show by doing this
Dim resourcePath As String = _
System.IO.Path.GetFullPath(My.Resources.ResourceManager.BaseName)
Dim rIndex As Integer = resourcePath.LastIndexOf("\")
resourcePath = resourcePath.Substring(0, rIndex)
Dim filePath As String = System.IO.Path.Combine(resourcePath, "Client.xml")
But the program creates a second folder that it puts the XML and the ICOn file in thats randomly generated but in the same directory.
How do I get the program to look in that folder for the xml?
PLEASE HELP ME !
Ray
You could do this:
Dim query = _
From d In System.IO.Directory.GetDirectories(resourcePath)
Let f = New FileInfo(System.IO.Path.Combine(d, "Client.xml"))
Where f.Exists
select f.FullName
Dim filePath = query.FirstOrDefault()
get list of all files by filter
lblPaymentMode.Location = New Point(lblDate.Right - lblPaymentMode.Width, lblPaymentMode.Location.Y)
Dim mFiles() As String = Directory.GetFiles("Path of folder", "*.xml", SearchOption.AllDirectories)
For i As Integer = 0 To mFiles.Count - 1
Debug.Print(mFiles(i)) 'print name and path of of each file
Next