【问题标题】:Path.GetFullPath Function searches only in Current ProjectPath.GetFullPath 函数仅在当前项目中搜索
【发布时间】:2017-03-14 02:13:39
【问题描述】:
    private static void saveDirAndFiles(TreeNode currNode)
    {

        using (StreamWriter file =  new StreamWriter("test.html"))
        {
            if(currNode.ValueType=="DIR") //Are you a File or Directory
            {
                file.WriteLine(currNode.Value);//relative Name of Directory
            }
            else
            {
                string[] file1 = File.ReadAllLines(Path.GetFullPath(currNode.Value)); //EXEPTION

                string prgcode = "";
                foreach (string line in file1)
                {
                    prgcode += line;
                }

                file.WriteLine(currNode.Value);//relative Name of File +...
                file.WriteLine(String.Format("<code><pre>{0}</pre></code>", prgcode)); //... The Content of the File
            }

        }
        foreach (TreeNode item in currNode.ChildNodes)
        {
            saveDirAndFiles(item);
        }

    }

该函数仅在我当前正在处理的项目中搜索绝对路径。我的项目在桌面上,文件名在 C 目录上。异常说:找不到 Project\bin\debug\Filename。

【问题讨论】:

  • Path.GetFullPath 不搜索驱动器。 Read the documentation. 即使搜索到了,如果在两个不同的目录中有两个名为“Filename”的文件怎么办?您需要从用户或配置文件中获取目录的名称。

标签: c# file directory filepath


【解决方案1】:

Path.GetFullPath 为您提供给定路径序列的绝对文件路径,可以是绝对的或相对的。它甚至不检查文件是否存在,也不在文件系统中执行搜索。

如果你想从其他地方加载你的文件,你有两个选择:要么使用Path.Combine(首选选项)将相对路径显式地重新设置为不同的基本路径,要么更改环境当前工作目录(可能对应用程序的其他部分有副作用)。

【讨论】:

  • 在这种情况下如何使用 Path.Combine。如何获取目录的名称,以便将其与文件结合起来?
  • 您必须知道基本路径或询问用户,例如,使用文件夹选择对话框。
猜你喜欢
  • 2021-05-29
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 2016-08-20
  • 1970-01-01
相关资源
最近更新 更多