C#中winform使用相對路徑讀取文件的方法


http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73b6cd0d3027fa3cf1fd5790801013db2e5703f170fd7c27e7001d8131ab5e4732f77552fe68cc8895ddccbd77373dc7a742f0b873105a31fb8bf3532b126c34de8df0e91bfba25e2a4c5a0dc4352ba44737e9780814d7010dd1cfa033093b1e83f022e16ad9d41728b2e&p=86759a46d7c159e50be296395b5697&newp=8774c71985cc43b708e2977f0f5785231610db2151d6d3156b82c825d7331b001c3bbfb42324140ed7ce766603ad425eebf23273300421a3dda5c91d9fb4c5747990&user=baidu&fm=sc&query=C%23winform%B4%FA%B4a%D7x%C8%A1%D6%B8%B6%A8%CE%C4%BC%FE&qid=93cca5ce00011011&p1=6

 

 

winform使用相對路徑讀取C#文件的方法,實例分析了文件的技巧與實際應用,需要的朋友可以參考下

 

本文實例講述了winform使用相對路徑讀取

方法一:由於生成的exe文件

復制代碼如下:
string haarXmlPath = @"../../haarcascade_frontalface_alt_tree.xml";

FileInfo file = new FileInfo(fileName);

string  fullName = file.FullName;

方法二:獲取exe文件名,形成全路徑

復制代碼如下:
string haarXmlPath = @"haarcascade_frontalface_alt_tree.xml";

string fullName = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\"));

fullName = fullName.Substring(0, fullName.LastIndexOf("\\")) + "\\" + haarXmlPath;

另一種方式:

復制代碼如下:

/// <summary>
/// 獲取應用程序根路徑
/// </summary>
private static string GetApplicationPath()
{
        string path = Application.StartupPath;
        //string path=AppDomain.CurrentDomain.BaseDirectory; //另一種獲取方式
        string folderName = String.Empty;
        while (folderName.ToLower() != "bin")
        {
            path = path.Substring(0, path.LastIndexOf("\\"));
            folderName = path.Substring(path.LastIndexOf("\\") + 1);
        }
        return path.Substring(0, path.LastIndexOf("\\") + 1);
}

希望本文所述對大家的C#程序設計有所幫助


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM