【问题标题】:How to include the current user's path on a file?如何在文件中包含当前用户的路径?
【发布时间】:2015-06-09 13:19:32
【问题描述】:

我在用户本地驱动器上的文件夹中有一个文件。我想通过最后几个文件夹来引用它,我该怎么做?示例:

string filePath = \\folder\file.txt; 

【问题讨论】:

  • 你将如何使用什么?你读过我发布的链接吗?它回答了你的问题吗?不,为什么?
  • 字符串 filePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)/folder/file.txt; ?
  • 使用System.IO.Path类,尤其是Combine:string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "/folder/file.txt‌");
  • (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) = 文件路径中有什么?就像它等于 C:/Users/CurrentUser 吗?

标签: c# connection


【解决方案1】:

使用“GetParent”

string path = Directory.GetCurrentDirectory(); //current path
string path1 = Directory.GetParent(path).ToString(); //parent of current path
string path2 = Directory.GetParent(path1).ToString(); //parent..
string path3 = Directory.GetParent(path2).ToString(); //parent..

如果您真的想使用它,请对其进行优化。它只是展示了它是如何工作的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多