以各种硬盘驱动器名称打开.txt文件 [英] Open .txt file in various hard drive names

查看:98
本文介绍了以各种硬盘驱动器名称打开.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这样的开放文本文件:

What i'm trying its open text file like this:

Dim FILE_NAME As String = "C:\FileName.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
    Process.Start(FILE_NAME)
Else
    MsgBox("File Does Not Exist")
End If



但问题是,当我将带有这些文件的.exe给另一个用户时,他可能有另一个名字HardDrive,D:除了C:例如..因此:


But the problem is that, when i give my .exe with those files to another user, he might have another name of HardDrive, D: except C: for example.. therefore this:

"C:\FileName.txt"

将找不到文件。我不需要openfiledialog。怎么解决?如果我可以将.txt文件放在与.exe相同的文件夹中,这将是最好的。一些cmd将知道它应该只从这个文件夹打开文件..只是猜测...再次感谢



我尝试过:



will not find a file. I dont need openfiledialog. How to solve it? It would the best if i can put .txt file in same folder with .exe and some cmd will know that it should open file only from this folder.. just guessing... thanks in further

What I have tried:

Dim FILE_NAME As String = "C:\FileName.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
    Process.Start(FILE_NAME)
Else
    MsgBox("File Does Not Exist")
End If

推荐答案

使用配置文件:管理应用程序设置(.NET) [ ^ ]
Use a configuration file: Managing Application Settings (.NET)[^]


要使用C#获取系统驱动器,您可以使用:
To get the system drive in C# you can use:
var p = System.IO.Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System));

在Vb.NET中:

Dim p = System.IO.Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System))

获取exe所在的路径:

To get the path where the exe is:

Dim strPath As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)

您将不得不删除字符串file:\\虽然。

Your will have to remove the begin of the string "file:\\" though.


这篇关于以各种硬盘驱动器名称打开.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆