获取文本文件或 C# 中的任何文件的完整路径

发布于 2024-09-14 17:04:37 字数 309 浏览 1 评论 0原文

尊敬的先生,
我的问题是,当我打开 txt 文件(即 abc.txt)时,我想获得仅在计算机中运行或打开文件的完整路径,我已经这样做了,

Process prs=Process.GetProcessByName("notepad");
foreach(Process p in prs)
{
     p.Modules[0].FileName.ToString();
}

这给了我 Notepad.exe 路径。我想要我的文本文件路径如 D:\abc.txt 所以我可以得到这个。

谢谢

Respected Sir,
my Question is that when I am Opening my txt file i.e abc.txt so I want to get full path of only running or opening file in my computer I have done this

Process prs=Process.GetProcessByName("notepad");
foreach(Process p in prs)
{
     p.Modules[0].FileName.ToString();
}

Which is giving me Notepad.exe path. I want my text file path like D:\abc.txt
so i can get this.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若有似无的小暗淡 2024-09-21 17:04:37
string fullPath = Path.GetFullPath(strFileName);
string fullPath = Path.GetFullPath(strFileName);
与之呼应 2024-09-21 17:04:37

您可以附加FileSystemEventHandler并检查哪些文件被打开或更改。

也许这会有所帮助

http://www.codeguru.com/csharp/ csharp/cs_network/article.php/c6043

You could attach FileSystemEventHandler and check which files are opened, or changed.

Maybe this will help

http://www.codeguru.com/csharp/csharp/cs_network/article.php/c6043

洛阳烟雨空心柳 2024-09-21 17:04:37

对于记事本,您可以像这样获取文件名:

Process[] prs = Process.GetProcessesByName("notepad");
foreach (Process p in prs)
{
    string title = p.MainWindowTitle;
    Console.WriteLine(title.Substring(0, title.IndexOf('-') -1));
}

重要的部分是:

title.Substring(0, title.IndexOf('-') -1)

不幸的是,这不会为您提供完整路径。

For notepad, you can get the name of the file like this:

Process[] prs = Process.GetProcessesByName("notepad");
foreach (Process p in prs)
{
    string title = p.MainWindowTitle;
    Console.WriteLine(title.Substring(0, title.IndexOf('-') -1));
}

The important part being:

title.Substring(0, title.IndexOf('-') -1)

Unfortunately this won't get you the full path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文