How to get the full path of a file through the file's name - vb.net

I am trying to get the path of a file from the name of it. I made an executable file on my desktop, Commands.exe, I am trying to get the full path of it through a console application. I want the program to search the whole of my computer for the file, (it is on my desktop for simplicity) and return the path, bearing in mind the file could be anywhere. The path I want the program to return is:
"C:\Users\Jimbob\Desktop\Commands.exe"
My Code:
Imports System.IO
Module Module1
Dim fileLocation As String
Dim filename As String = "Commands.exe"
Sub Main()
fileLocation = Path.GetFullPath(filename)
Console.WriteLine(fileLocation)
Console.ReadLine()
End Sub
End Module
but instead it prints out
"C:\Users\Jimbob\Documents\Visual Studio 2012\Projects\get path
test\get path test\bin\Debug\Commands.exe"
It prints out the path of the project all of my code is in. In this path, there isn't even a Commands.exe in the Debug folder.
Help?

or couldn't you just
dim fileName as string = My.Application.Info.DirectoryPath() & "\Commands.exe"

You can use the Directory.GetFiles or the FileSystem.GetFiles methods to do this.
Something along the lines of:
fileLocation = My.Computer.FileSystem.GetFiles("C:\",
FileIO.SearchOption.SearchAllSubDirectories,
filename)(0)

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.

Delete A File That Contains The App Name (VB.NET)

This is the code I'm Using:
Dim file As String
Dim prefetchPath As String
Dim FileName As String = My.Application.Info.AssemblyName
prefetchPath = Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine) & "\Prefetch"
For Each file In IO.Directory.GetFiles(prefetchPath)
If file.Contains(FileName) Then
IO.File.Delete(file)
End If
Next
i don't know why it does not work if i use FileName. But it work if i use this code
If file.Contains("Example.exe") Then
IO.File.Delete(file)
End If
I want to make sure that if someone changes the name of the application the code works the same way(I already running the file as Administrator)
Help me Thanks.
My guess is that AssemblyName only returns the name without the extension, try including the .exe. Also, it is worth noting that you can use the IO.DirectoryInfo class and pass the file name in the GetFiles method to cut out your For/Each loop.
Here is a quick example:
Dim prefetchPath As String = IO.Path.Combine(Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine), "Prefetch")
Dim FileName As String = My.Application.Info.AssemblyName & ".exe"
If New IO.DirectoryInfo(prefetchPath).GetFiles(FileName).Count > 0 Then
IO.File.Delete(IO.Path.Combine(prefetchPath, FileName))
End If

How to get the file name of a file in VB?

I make a search program for searching a list of files in a computer and then copy the file into a store folder. The file name could be "*11*2.txt" As long as the program find this pattern, it should copy to the store folder. The problem is that I don't know the exactly name of the file before the search and I don't want to rename the file, I don't know how to save the file. Please help
I use the following to find the file, which does its work
Public Sub DirSearch(ByVal sDir As String, ByVal FileName As String)
Dim To_Path As String
To_Path = Form1.TextBox5.Text
For Each foundFile As String In My.Computer.FileSystem.GetFiles(sDir, FileIO.SearchOption.SearchAllSubDirectories, FileName)
Copy2Local(foundFile, To_Path)
Next
End Sub
Here is the current version of the Copy2Local (Note: it is not working right)
Public Sub Copy2Local(ByVal Copy_From_Path As String, ByVal Copy_To_Path As String)
' Specify the directories you want to manipulate.
Try
Dim fs As FileStream = File.Create(Copy_From_Path)
fs.Close()
' Copy the file.
File.Copy(Copy_From_Path, Copy_To_Path)
Catch
End Try
End Sub
First, you should check if ToPath is a valid directory since it's coming from a TextBox:
Dim isValidDir = Directory.Exists(ToPath)
Second, you can use Path.Combine to create a path from separate (sub)directories or file-names:
Dim copyToDir = Path.GetDirectoryName(Copy_To_Path)
Dim file = Path.GetFileName(Copy_From_Path)
Dim newPath = Path.Combine(copyToDir, file)
http://msdn.microsoft.com/en-us/library/system.io.path.aspx
(disclaimer: typed from a mobile)
To answer your question: You can get the file name with Path.GetFileName. Example:
Dim fileName As String = Path.GetFileName(foundFile)
However, there's a bunch of other things wrong with your code:
Here,
Dim fs As FileStream = File.Create(Copy_From_Path)
fs.Close()
you are overwriting your source file. This does not seem like a good idea. ;-)
And here,
Try
...
Catch
' Do Nothing
End Try
You are throwing away exceptions that would help you find and diagnose problems. Don't do that. It makes debugging a nightmare.
In vb.net, I'm using the following code to find the filename
Textbox1.Text = New FileInfo(OpenFileDialog.FileName).Name
this code work fine with open file dialog box

how to get file directory when open with or send to event?

How do I get a file's directory when open with or send to event happens?
I want it like media player, the application can get the file name & path when the user runs it with Open With.
The filename will be pased on the command line to your application, so you can access it by either
Module MainModule
Sub Main(cmdArgs As String())
Dim fileName as string = cmdArgs(0)
End Sub
End Module
For a console application, or by
Dim args As String() = Environment.GetCommandLineArgs()
Dim fileName As String = args(1)
For a GUI application.
Note that when using Environment.GetCommandLineArgs(), the first element in the array will be the full path to your executable, so you need to access the second element to get the file name. In the first example, cmdArgs will only contain the file name parameter.
To get the directory name containing the file, you can use
Dim path as String = System.IO.Path.GetDirectoryName(fileName)