【CITE】當類庫項目中無法使用Application.StartupPath的時侯 (注:主要是在進行反射讀取文件的時候!!)


http://jcserver.blog.163.com/blog/static/24044859200851582354135/

 

通常我們WinForm編程時,要獲取程序當 前運行的文件夾路徑會用Application.StartupPath ,但是Application.StartupPath在編寫類庫項目時卻無法 使用,因為我們根本無法用using System.Windows.Forms;來引入Application.StartupPath 的命名空間,這個時侯我們要用AppDomain.CurrentDomain.BaseDirectory。

private static string fullPathFileName = Application.StartupPath + "//Set.Ini "; //用於WINFORM
private static string fullPathFileName = AppDomain.CurrentDomain.BaseDirectory+"//Set.Ini"; //用於類項目

 
        public static void WriteLog(string txt)
        {

            try
            {

                string path = Application.StartupPath + @"\log\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";

                if (!Directory.Exists(path))
                {

                    Directory.CreateDirectory(path);

                }

                path += DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("HH") + ".txt";

                if (!File.Exists(path))
                {

                    File.Create(path);

                }

                FileStream fs;

                StreamWriter sw;

                fs = new FileStream(path, FileMode.Append);

                sw = new StreamWriter(fs, Encoding.Default);

                sw.Write(DateTime.Now.ToString("HH:mm:ss") + " " + txt + "\r\n");

                sw.Close();

                fs.Close();

            }

            catch (Exception ex)
            {

                WriteLog("程序發生異常(WriteLog)。詳情:" + ex.Message);

            }

        }

 


免責聲明!

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



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