How do you call files without drive path - vb.net

I don't know how to explain this right but I would like to ask you guys how do you call all the files in a folder without the "C:\" directory so wherever I move my project in another computer I don't have to edit the path directory in my project is that even possible? I'm using visual basic 2008.

I would advice to use AppDomain.CurrentDomain.BaseDirectory , this code will return the directory where the current application is located, lets say you want to get all the directories in the folder where the application is located, then you will get something like this.
Dim currentpath As String = AppDomain.CurrentDomain.BaseDirectory 'Get the directory where the application is located.
Dim Directories() As String = Directory.GetDirectories(currentpath) 'get all the directories what are in the current location of the application
'
Console.WriteLine("This application is located at:: {0}", currentpath)
'
If Directories.Length = 0 Then
Console.WriteLine("There aren't any folders found in the location of the application.")
Else
'
Console.WriteLine("The follow folder(s) are found.")
'
For Each folder In Directory.GetDirectories(currentpath)
Console.WriteLine(folder)
Next
'
End If
'
Console.ReadLine()
Output:
This application is located at :: C:Users\Kona\Desktop\
The follow folder(s) are found.
C:\Users\Kona\Desktop\C#
C:\Users\Kona\Desktop\VB
C:\Users\Kona\Desktop\Haskell
C:\Users\Kona\Desktop\Java

You can easily just use the:
Environment.CurrentDirectory
variable.
(use ..\ to go up a directory...)

Related

How Do I Get The Full Path Of A Selected Directory And Then Use That Directory Within The Code

I have currently created code which grabs a ceratin file extension and puts it into a certain folder.
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Excel Files")
Dim filePaths31 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.xlsx")
For Each filePath31 In filePaths31
Dim filename31 = IO.Path.GetFileName(filePath31)
Dim newPath31 = IO.Path.Combine("C:\Users\bj\Desktop\Excel Files", filename31)
If IO.File.Exists(newPath31) Then
MessageBox.Show("Error: Please check if the file elready exists")
Return
End If
IO.File.Move(filePath31, newPath31)
Next filePath31
MessageBox.Show("Excel Files Compiled And Cleaned")
This code works well although the directory implemented inside the code and should be different for every user.
I have experimented with this code here which allows a user to select a directory, although now I need help assigning that full directory into IO.Path.Combine(users chosen directory here + \Excel Files, filename31
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim grade As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
This code I believe grabs the user directory and stores it in the grade variable. I need to figure out how to store that 'grade' variable into my code, and also create a new folder in that directory that is specified in the code.

Get full path to .exe file if only a subfolder path to it is given

I have to find the rscript.exe that exists in a folder called bin but I do not have the full reference to this folder.
Full Reference would be:
C:\Program Files\R\R-3.5.3\bin
but I do not know the version part beforehand. So I must go to
C:\Program Files\R and search the subfolders for the rscript.exe and recieve the full path to it.
I have already searched but all I could find was how to find out the path of the running application via reflections and tried out to rewrite the code without any solution. Could anyone help me here?
You can use the Directory.GetDirectories method with the searchPattern argument and loop throught the subdirectories:
Dim rootPath As String = "C:\Program Files\R"
If Directory.Exists(rootPath) Then
Dim currPath As String
For Each subFolder As String In IO.Directory.GetDirectories(rootPath, "R-*", SearchOption.TopDirectoryOnly)
currPath = IO.Path.Combine(subFolder, "bin", "rscript.exe")
If IO.File.Exists(currPath) Then
MessageBox.Show("File found at " & currPath)
End If
Next
End If

Download .txt files from web using vb.net

I have managed to find a way to download .txt files from a website that i have access to which is great, but i am using a list of filenames i have added to a datagridview.
My next progression is to download any .txt files from the root and subdirectories on the website but if it exists in my download location c:\logs it will skip it.
Any direction is gratefully received. VBVirg
Dim _FilePath As String = "<file path and name here>"
If My.Computer.FileSystem.FileExist(_FilePath) = True Then
'File exists.
End If
In addition to Vincent's answer:
Dim FilePath As String = "YourFile"
If My.Computer.FileSystem.FileExists(FilePath) = True Then
'File exists, delete it
My.Computer.FileSystem.DeleteFile(FilePath)
Else
'The file doesn't exist.
End If

How to create a folder and a file in my project root directory

I have a vb .net application which is stored in my D: drive. I need to create a folder and a file in my project root directory. I used the following code to create the folder as well as the file.
If (Not System.IO.Directory.Exists("\test\")) Then
System.IO.Directory.CreateDirectory("\test\")
If (Not System.IO.File.Exists("\test\output.txt")) Then
System.IO.File.Create("\test\output.txt")
End If
End If
But the folder and the file is created in C: drive.
I used Dim fullpath As String = Path.GetFullPath("\test\output.txt") to identify where the file and folder is created.
I want to create it in my project root directory. That means I need to create the folder using a relative path criteria.
If you want the directory of the executable or dll of your running application then:
Dim spath As String
spath = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
If (Not System.IO.Directory.Exists(Path.Combine(spath, "test"))) Then
System.IO.Directory.CreateDirectory(Path.Combine(spath, "test"))
If (Not System.IO.File.Exists(Path.Combine(spath, "test\output.txt"))) Then
System.IO.File.Create(Path.Combine(spath, "test\output.txt"))
End If
End If
If you want the solution/project directory then:
Dim spath As String = Directory.GetCurrentDirectory
If (Not System.IO.Directory.Exists(Path.Combine(spath, "test"))) Then
System.IO.Directory.CreateDirectory(Path.Combine(spath, "test"))
If (Not System.IO.File.Exists(Path.Combine(spath, "test\output.txt"))) Then
System.IO.File.Create(Path.Combine(spath, "test\output.txt"))
End If
End If
You could use this path, and work from there...
MsgBox(Application.StartupPath)

How do I create a folder in VB if it doesn't exist?

I wrote myself a little downloading application so that I could easily grab a set of files from my server and put them all onto a new pc with a clean install of Windows, without actually going on the net. Unfortunately I'm having problems creating the folder I want to put them in and am unsure how to go about it.
I want my program to download the apps to program files\any name here\
So basically I need a function that checks if a folder exists, and if it doesn't it creates it.
If Not System.IO.Directory.Exists(YourPath) Then
System.IO.Directory.CreateDirectory(YourPath)
End If
Under System.IO, there is a class called Directory.
Do the following:
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
It will ensure that the directory is there.
Try the System.IO.DirectoryInfo class.
The sample from MSDN:
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' Specify the directories you want to manipulate.
Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
Try
' Determine whether the directory exists.
If di.Exists Then
' Indicate that it already exists.
Console.WriteLine("That path exists already.")
Return
End If
' Try to create the directory.
di.Create()
Console.WriteLine("The directory was created successfully.")
' Delete the directory.
di.Delete()
Console.WriteLine("The directory was deleted successfully.")
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
Since the question didn't specify .NET, this should work in VBScript or VB6.
Dim objFSO, strFolder
strFolder = "C:\Temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
objFSO.CreateFolder strFolder
End If
Try this: Directory.Exists(TheFolderName) and Directory.CreateDirectory(TheFolderName)
(You may need: Imports System.IO)
VB.NET? System.IO.Directory.Exists(string path)
Directory.CreateDirectory() should do it.
http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(VS.71).aspx
Also, in Vista, you probably cannot write into C: directly unless you run it as an admin, so you might just want to bypass that and create the dir you want in a sub-dir of C: (which i'd say is a good practice to be followed anyways. -- its unbelievable how many people just dump crap onto C:
Hope that helps.
(imports System.IO)
if Not Directory.Exists(Path) then
Directory.CreateDirectory(Path)
end if
If Not Directory.Exists(somePath) then
Directory.CreateDirectory(somePath)
End If
You should try using the File System Object or FSO. There are many methods belonging to this object that check if folders exist as well as creating new folders.
I see how this would work, what would be the process to create a dialog box that allows the user name the folder and place it where you want to.
Cheers
Just do this:
Dim sPath As String = "Folder path here"
If (My.Computer.FileSystem.DirectoryExists(sPath) = False) Then
My.Computer.FileSystem.CreateDirectory(sPath + "/<Folder name>")
Else
'Something else happens, because the folder exists
End If
I declared the folder path as a String (sPath) so that way if you do use it multiple times it can be changed easily but also it can be changed through the program itself.
Hope it helps!
-nfell2009