How do I specify relative file path in VB.net - vb.net

I have a Webbrowser control in a form which displays a pdf file. I have to specify the URL as the file location on my computer.
eg.
E:\Folder\Manual.pdf
Both the pdf file and the program are in the same folder.
How do I specify the URL so that when I move the folder onto another drive, it opens the same pdf file?

The location of your application is
Dim path as String = My.Application.Info.DirectoryPath
The you could use:
Dim pdffile as String = IO.Path.Combine(path, "pdffile.pdf")
WebBrowser1.Navigate(pdffile)

If I understand you correctly, then:
Dim myPdf As String =
IO.Path.Combine(IO.Directory.GetParent(Application.ExecutablePath).FullName, "myPdfFile.pdf")

Another way you could do it is by using something like the code below;
Private Sub FamilyLocateFile_Click(sender As Object, e As EventArgs) Handles FamilyLocateFile.Click
If LocateFamilyDialog.ShowDialog = DialogResult.OK Then
FamilyWMP.URL = LocateFamilyDialog.FileName
ElseIf LocateFamilyDialog.ShowDialog = DialogResult.Cancel Then
MsgBox(MsgBoxStyle.Critical, "Error!")
End If
End Sub
What this will do is play a file in a Windows Media Player ActiveX object. The file can be selected with an OpenFile Dialog, which in this case is called LocateFamilyDialog. You don't need the ElseIf part of the statement, but you will need to insert an open file dialog and a control that can display PDFs. I think it'll work with WebBrowsers, but I'm not sure.

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

get full path using openfiledialog

Simple utility to get a file for use.
Using openfiledialog I am trying to get the full path EG: File = text1.txt and it is located in c:\temp. So the full path is C:\temp\text1.txt.
But all I can get is the file name. I've searched I've hunted, I'e tried for a couple of hours and nothing works.
Here is code with comments...
'open the openfile dialog so the user can search for a file
Dim openFileDialog1 As New OpenFileDialog()
'set the root to the z drive
openFileDialog1.InitialDirectory = "Z:\"
'make sure the root goes back to where the user started
openFileDialog1.RestoreDirectory = True
'show the dialog
openFileDialog1.ShowDialog()
'check there is something to work with... the user did not exit before selecting a file etc.
If openFileDialog1.FileName.Length = 0 Then
'if the user selected a file set the value of the replacefile text box
Else
TB_ReplacementFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
End If
All I get is the file name...
The MSDN documentation and numerous posts all over the place say all you need is openfiledialog.FileName. However this did not work for me, can't tell you why. What DID work is to use this:
TB_ReplacementFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
This works great, I get what I need. I cannot explain why I have to do this. Not sure how I can be the problem, but that must be the problem right?!
Hopefully this helps someone.
The FileName Property returns the full path.
The file name includes both the file path and the extension. If no files are selected, this method returns an empty string ("").
If (openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
TB_ReplacementFile.Text = openFileDialog1.FileName
End If
Not 100% sure if this would resolve the issue you're having, or if it's simply just another way to handle it, but I prefer to check the DialogResult. I.E:
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "Z:\"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.Ok Then
Console.WriteLine(openFileDialog1.fileName)
End If
this worked to get the directory or path
dim sDir as String = System.IO.Path.GetDirectoryName(openfiledialog1.FileName.ToString)

How to make textfile to save in program directory in Visual Studio

As i have abandoned the array approach to the problem, i need to know how to make listbox to save in textfile always in program's directory so it can be used/accessed to populate a different listbox, any ideas? Below is my code.
SaveFileDialog1.Filter = "Text files (.txt)|.txt"
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then
Using SW As New IO.StreamWriter(SaveFileDialog1.FileName, False)
For Each itm As String In Me.ListBox1.Items
SW.WriteLine(itm)
Next
End Using
End If
A little bit of research on your part would've helped you understand what you are trying to accomplish better.
How do I get Program Data directory? My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
How do I Write multiple lines to file? File.WriteAllLines()
How do I Read multiple lines from a file? File.ReadAllLines()
Once you understand the basics you can easily put them together
Create two List boxes, and one button on your WinForm:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.
'Get the Program Data Directory (This is hidden by default by the OS.)
Dim strPath As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
Dim fileName As String = "myFile.txt"
Dim fullPath = Path.Combine(strPath, fileName)
Dim data As String() = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"}
'Save the items to ListBox1 First
For Each item As String In data
ListBox1.Items.Add(item)
Next
'Now write the items to the textfile, line by line.
File.WriteAllLines(fullPath, data)
'Read all lines we just saved and load them onto an array of strings.
Dim tempAllLines() As String = File.ReadAllLines(fullPath)
'Display each on ListBox2 by iterating the array.
For Each line As String In tempAllLines
ListBox2.Items.Add(line)
Next
End Sub
Here, I created this form so you can get an idea of what i'm referring to.
You can get the path to the current executable's folder like this:
folderPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
However, that will only work if the executable is a .NET assembly. Otherwise, you could use the first argument in the command line (which is the full executable file path), like this:
folderPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()(0))
If, on the other hand, you want to get the path of the current assembly (which may be different than the executable that loaded it) you could do this:
folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Or, if you want to just get the current directory, you could use this:
folderPath = Directory.GetCurrentDirectory()
Once you have the folder path, you can add the file name to it with Path.Combine, like this:
filePath = Path.Combine(folderPath, fileName)
However, it's not recommended that you write data directly to the program's running path, since the user may not have permission to write to that folder. Using the program data folder would certainly be better, but even that can be risky:
folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyAppName")
The recommended place to store data from .NET apps is Isolated Storage.

Visual Basic Form Coding (how to set a directory)

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim Newline As String
Newline = System.Environment.NewLine
System.IO.File.WriteAllLines("C:\Users\Sang\Desktop\filename.txt", Result1.Lines)
System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", Result2.Lines)
System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", values.Lines)
End Sub
This is my coding for making a text file on my desktop. However, my friend can not run this code because this code is only for myself as you can see above. I would like to use a folderbroswerdialog to generalize this coding for everyone. To be specific, if a user pressed this button on the form, folder browser should ask him where he wants to save this text file and text file should be saved in the directed folder or desktop. I tried to do it on my own by looking at many youtube videos and resources but I failed. How should I proceed this?
You can use the Environment.SpecialFolder Enumeration which contains the ubications of the system's directories that you can retrieve using Environment.GetFolderPath method :
Dim DesktopDir As String =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Then you can use the Path.Combine method to properlly combine a directory/file path:
Dim OutputFile As String =
IO.Path.Combine(DesktopDir, "filename.txt")
Then:
IO.File.WriteAllLines(OutputFile, "Text Here")
Take a look here for using the FolderBrowserDialog:
http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx
PERFECTLY great examples there.

How can I make a "browse for file.." button in VB.net?

I'm trying to build a simple FTP uploader. How can I make it so the user can select a file to upload? See, what I want is to have a button (which I do) that a user can click, and it shows the OpenFileDialog (which I have), but then when they select a file, I want its path to be shown in a text box. How can I do that?
Try the following code
Dim dialog As New OpenFileDialog()
If DialogResult.OK = dialog.ShowDialog Then
TextBox1.Text = dialog.FileName
End If
One way is to convert the filename to a FileInfo which contains all sorts of information about the file including the path. This opens the dialog and displays the path of the selected file.
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim fi As New System.IO.FileInfo(OpenFileDialog1.FileName)
TextBox1.Text = fi.DirectoryName
End If
You want to get the OpenFileDialog's property Filename property. See OpenFileDialog's class members here on MSDN.
Hope this helps
Add a OpenFileDialog And Add This Code
If YourOpenFileDialogName.ShowDialog = YourOpenFileDialogName.OK Then
textBox1.Text = YourOpenFileDialogName.FileName
End If