为 txt 保存设置正确的文件路径 - vb.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15886981/
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 13:10:46  来源:igfitidea点击:

Setting Correct Filepath for a txt Save - vb.NET

vb.netinstallsavesettingfilepath

提问by Bobby Nicholls

Okay. I feel like this should be programming 101, but I cannot seem to find a decent answer on how to set a file path name to be dynamic enough to be set explicitly to where the exe is installed.

好的。我觉得这应该是编程 101,但我似乎找不到关于如何将文件路径名设置为足够动态以明确设置为安装 exe 的位置的合适答案。

Basically, this application is actually going to be installed in the Users personal folders, probably something like Local Data, and I need to get a txt file that is created by the program to be created into the same directory as the executable.

基本上,这个应用程序实际上将安装在用户个人文件夹中,可能类似于本地数据,我需要获取一个由程序创建的 txt 文件,以便将其创建到与可执行文件相同的目录中。

Current Path:

当前路径:

Dim strFilePath As String = "D:\Development\Bobby\Prototyping\Replication Desktop Client\Replication_Desktop_Client\ClientAccessList.txt"

I want to set it to something like

我想将它设置为类似

Dim strCurrentLocationOfEXE As String = HardDriveLetter & Users & CurrentUserPath & InstalledDirectory
Dim strFilePath As String = strCurrentLocationOfEXE & "\ClientAccessList.txt"`

but I can not for the life of me figure out how to get it to determine that, since it is not going to always be installed to the same folder (i.e. Username and perhaps hard drive letter will be different).

但我终生无法弄清楚如何确定它,因为它不会总是安装到同一个文件夹中(即用户名和硬盘驱动器号可能会有所不同)。

Ideas?

想法?

采纳答案by Steve

You can get the path where the assembly is running with

您可以获得程序集运行的路径

Dim fullPath = System.Reflection.Assembly.GetExecutingAssembly().Location
Dim folderName = Path.GetDirectoryName( fullPath )
Dim strFilePath = Path.Combine(folderName, "ClientAccessList.txt")

if you want to refer to the current user personal folder for this application then the way to go is through the Environment.SpecialFolderenumaration.
This enum is independent from the underlying OS (XP, Win7, x64, x32 etc) In this case you could use:

如果您想引用此应用程序的当前用户个人文件夹,则可以通过Environment.SpecialFolder枚举。
此枚举独立于底层操作系统(XP、Win7、x64、x32 等)在这种情况下,您可以使用:

Dim fullPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim strFilePath = Path.Combine(fullPath, "your_app_reserved_folder", "ClientAccessList.txt")

In this example "your_app_reserved_folder"should be a folder created during installation of your application where you put per user data files. (Usually this is the recommended way to go to store data files that should be kept separated by user)

在此示例中,"your_app_reserved_folder"应该是在安装应用程序期间创建的文件夹,您将每个用户的数据文件放在其中。(通常这是去存储应该由用户分开的数据文件的推荐方式)

If you want to check the existence of the folder before trying to use it just encapsulate the logic to get the file name in a method

如果您想在尝试使用它之前检查文件夹是否存在,只需在方法中封装获取文件名的逻辑

Public Function GetUserAppClientAccessList() As String

    Dim fullPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    Dim appFolder = Path.Combine(fullPath, "your_app_reserved_folder")
    if Not Directory.Exists(appFolder) then
        Directory.Create(appFolder)
    End If
    return = Path.Combine(appFolder, "ClientAccessList.txt")
End Function

回答by Steven Doggart

This will give you the file path of the executable:

这将为您提供可执行文件的文件路径:

Assembly.GetEntryAssembly().Location

And then to get the folder path, you can call Path.GetDirectoryName. So, to get the text file path, you can do something like this:

然后要获取文件夹路径,您可以调用Path.GetDirectoryName. 因此,要获取文本文件路径,您可以执行以下操作:

Dim exeFilePath As String = Assembly.GetEntryAssembly().Location
Dim exeFolderPath As String = Path.GetDirectoryName(exeFilePath)
Dim filePath As String = Path.Combine(exeFolderPath, "ClientAccessList.txt")

One bit of caution though: Assembly.GetEntryAssemblycan return Nothingif there is no .NET assembly executable, such as if the code is being called as a library via COM. In that case, you may want use the executable file path from the command line by calling Environment.GetCommandLineArgs()(0). And if that fails, for some reason, you could always resort to Directory.GetCurrentDirectory().

不过要注意一点:如果没有 .NET 程序集可执行文件,则Assembly.GetEntryAssembly可以返回Nothing,例如,如果代码作为库通过 COM 调用。在这种情况下,您可能希望通过调用从命令行使用可执行文件路径Environment.GetCommandLineArgs()(0)。如果由于某种原因失败了,您可以随时求助于Directory.GetCurrentDirectory().