路径.GetFullPath函数只在当前项目中搜索

本文关键字:项目 搜索 GetFullPath 函数 路径 | 更新日期: 2023-09-27 18:15: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未找到。

路径.GetFullPath函数只在当前项目中搜索

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

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