在 vb.net 中打开特定文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19275726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:25:28  来源:igfitidea点击:

Open a specific file in vb.net

vb.netfilepath

提问by Bernoulli Lizard

I want my program to open a specific .txt file. The text file will always stay in the same folder within the solution folder. However, the location of the solution folder itself may change if the solution is moved to a different computer, or a different directory on the current computer.

我希望我的程序打开一个特定的 .txt 文件。文本文件将始终保留在解决方案文件夹内的同一文件夹中。但是,如果将解决方案移动到另一台计算机或当前计算机上的不同目录,则解决方案文件夹本身的位置可能会发生变化。

I know how to hardcode the file path append the file name and then open it. But how can I define the file path so that the file can still be opened if the solution moves to a different computer?

我知道如何硬编码文件路径附加文件名然后打开它。但是如何定义文件路径,以便在解决方案移动到另一台计算机时仍然可以打开文件?

回答by Nunners

If the file is contained within the solution, you can use a virtual path which is then mapped to a physical path using Server.MapPath

如果该文件包含在解决方案中,则可以使用虚拟路径,然后使用该虚拟路径映射到物理路径 Server.MapPath

The following should work :

以下应该工作:

Dim filePath As String = Server.MapPath("~/FileName.txt")

Please note that the location of FileName.txt in my example is in the root of the solution and not in any specified folders, ~/is essentially the root of the current solution.

请注意,在我的示例中 FileName.txt 的位置在解决方案的根目录中,而不是在任何指定的文件夹中,~/本质上是当前解决方案的根目录。

For more information on the Server.MapPath method and Virtual Paths see below:

有关 Server.MapPath 方法和虚拟路径的更多信息,请参见下文:

Server.MapPath MSDN Documentation

Server.MapPath MSDN 文档

Virtual Path Utility Class

虚拟路径实用程序类

回答by peterG

If you're asking about a desktop application, application.ExecutablePath will do what you want. It's not really a good idea though, if your application will reside within Program Files - it's best to avoid writing to anywhere within there, and you will have to run as administrator on post-XP OS's .

如果您询问的是桌面应用程序,application.ExecutablePath 将满足您的需求。不过,这不是一个好主意,如果您的应用程序将驻留在 Program Files 中 - 最好避免写入其中的任何地方,并且您将不得不在 post-XP OS 上以管理员身份运行。

回答by rory.ap

If you meant to say your FileName.txt was always present in the same directory as the assembly, you could do something simple like this:

如果您想说 FileName.txt 始终与程序集位于同一目录中,则可以执行以下简单操作:

Function GetAssemblyDirectoryPath() As String
    Dim fullAssemblyPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location
    Return fullAssemblyPath.Substring(0, fullAssemblyPath.LastIndexOf("\"c))
End Function

This just returns the path to the folder where the assembly resides. The "Solution" might not always be present, but the assembly will, so this will work in both cases (if you make sure the file is always copied to the output directory).

这只是返回程序集所在文件夹的路径。“解决方案”可能并不总是存在,但程序集会存在,因此这将适用于两种情况(如果您确保文件始终复制到输出目录)。

回答by Tun Zarni Kyaw

If you are sure that your application (.exe) and (.txt) files are in the same folder, then just use the file name - do not put the path.

如果您确定您的应用程序 (.exe) 和 (.txt) 文件位于同一文件夹中,则只需使用文件名 - 不要输入路径。

IO.File.OpenText("thefile.txt")

Edited: Mostly "current working directory" is the same directory where the (.exe) file exists. Yes, sometimes, it is not the same directory. Thus, Application.ExecutablePath would be the right solution.

编辑:大多数“当前工作目录”与 (.exe) 文件所在的目录相同。是的,有时,它不是同一个目录。因此,Application.ExecutablePath 将是正确的解决方案。

Dim fn As String
fn = Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\")+1) & "thefile.txt"
IO.File.OpenText("thefile.txt")

...

...