Vb.net 如何在我的项目根目录中创建文件夹和文件
我有一个vb.net应用程序,它存储在我的D:驱动器中。我需要在我的项目根目录中创建一个文件夹和一个文件。我使用以下代码创建文件夹和文件Vb.net 如何在我的项目根目录中创建文件夹和文件,vb.net,file,directory,relative-path,Vb.net,File,Directory,Relative Path,我有一个vb.net应用程序,它存储在我的D:驱动器中。我需要在我的项目根目录中创建一个文件夹和一个文件。我使用以下代码创建文件夹和文件 If (Not System.IO.Directory.Exists("\test\")) Then System.IO.Directory.CreateDirectory("\test\") If (Not System.IO.File.Exists("\test\output.txt"))
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
但是文件夹和文件是在C:drive中创建的。
我使用Dim fullpath作为String=Path.GetFullPath(“\test\output.txt”)
来标识文件和文件夹的创建位置
我想在我的项目根目录中创建它。这意味着我需要使用相对路径条件创建文件夹。如果您想要运行应用程序的可执行文件或dll的目录,请:
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
如果需要解决方案/项目目录,则:
你可以使用这条路,从那里开始工作
MsgBox(Application.StartupPath)
这些答案对你有帮助吗?
MsgBox(Application.StartupPath)