Reputation: 2118
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.
Upvotes: 1
Views: 8269
Reputation: 3923
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
Upvotes: 2
Reputation: 1547
You could use this path, and work from there...
MsgBox(Application.StartupPath)
Upvotes: 0