Visual Basic 2010 Space in File for Process.Star



我试图了解如何在文件位置中使用多个空格,但我仍然遇到问题。

Process.Start(%userprofile%AppDataRoamingMicrosoftWindowsStart MenuProgramsXXXXX XXXXX XXXXXXXXXX XXXXX XXXXXXXXXXXXXXXX.appref-ms)

我尝试了多种解决方案,但没有一个帮助我。

有人可以帮助我吗?

谢谢

文件路径只是一个字符串。在 VB.NET 中,字符串文字需要括在双引号中""。换句话说,为了使字符串硬编码到代码中的任何位置,它必须括在双引号中,这不是Process.Start()独有的,它适用于任何字符串文字,字符串是否包含空格并不重要

因此,为了将文件路径传递给Process.Start(),您可以直接调用:

Process.Start("Thepathtoyourfile")
'Process.Start(Thepathtoyourfile)   ' Wrong! Won't compile.

..或者,您可以执行以下操作:

Dim filePath As String = "Thepathtoyourfile"
Process.Start(filePath)

也就是说,请注意Process.Start("%userprofile%...")不起作用,因为 VB.NET 不会将%userprofile%转换为当前用户目录的实际路径。为此,您需要将Environment.GetFolderPath()与适当的Environment.SpecialFolder枚举一起使用。

类似以下内容的内容应该有效:

Dim userDir As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim filePath As String = IO.Path.Combine(userDir, "AppDataRoaming...")
Process.Start(filePath)

或者您可以直接使用以下方法获取%AppData%路径(即%UserProfile%AppDataRoaming

(:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

希望有帮助。

相关内容

最新更新