zoukankan      html  css  js  c++  java
  • 【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 + "
    ");
    
                    sw.Close();
    
                    fs.Close();
    
                }
    
                catch (Exception ex)
                {
    
                    WriteLog("程序发生异常(WriteLog)。详情:" + ex.Message);
    
                }
    
            }
  • 相关阅读:
    将excel中的sheet1导入到sqlserver中
    .net中 Timer定时器
    Exception异常处理机制
    算法
    八、上网行为管理
    获取网站路径绝对路径的方法汇总
    Window逆向基础之逆向工程介绍
    Java Web代码审计流程与漏洞函数
    创建一个Java Web项目,获取POST数据并显示
    七、虚拟专用网
  • 原文地址:https://www.cnblogs.com/hardsoftware/p/5734708.html
Copyright © 2011-2022 走看看