簡體   English   中英

Path.GetFullPath函數僅在當前項目中搜索

[英]Path.GetFullPath Function searches only in Current Project

    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為您提供給定路徑序列的絕對文件路徑,該路徑可以是絕對路徑也可以是相對路徑。 它甚至不檢查文件是否存在,也不在文件系統中執行搜索。

如果您要從其他位置加載文件,則有兩個選擇:要么使用Path.Combine (首選選項)將相對路徑顯式地重新建立為另一個基本路徑, Path.Combine更改當前工作目錄的環境(可能具有以下內容:在應用程序的其他部分產生影響)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM