我遇到一些获取应用程序位置并将某些文件存储在文件夹中的代码的问题。问题是文件编写器无法正确读取路径。
path变量返回:C:\Users\user\Desktop\RoboControl\bin\debug\scripts\
但是,文件编写器有一个无法找到路径的异常,并尝试在此处写入:C:\scripts\test.txt
我尝试使用更多变量,脚本目录的变量和要保存的文件的变量。
Dim file As System.IO.StreamWriter
Dim dpath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath()), "\scripts") ''scripts directory path, returns ok
Dim fpath = Path.Combine(dpath, TextBox1.Text) ''final file path, returns ok
file = My.Computer.FileSystem.OpenTextFileWriter(fpath, False) ''here the path variable is not read correctly
''Text from the first form (this form is a dialog box)
file.Write(Form1.TextBox1.Text)
file.Close()
编辑:我意识到的一件事是fpath不包含完整的dpath变量,仅包含\scripts\test.txt
,但是编写者应在当前目录中创建一个文件。
答案 0 :(得分:1)
您不需要从runWriter
获取文件夹路径,因为Application.ExecutablePath
是该文件夹路径。您不需要在部分路径中添加斜杠,因为Application.StartupPath
会为您这样做。您可能不需要多次调用Path.Combine
,因为它已经接受了一段时间的多个部分路径。您可能可以使用:
Path.Combine
查看是否可以为您提供正确的路径。请注意,Dim filePath = Path.Combine(Application.StartupPath, "scripts", TextBox1.Text)
后没有括号,因为它是属性,而不是方法。不确定是否与您的问题有关。