• C#取exe文件绝对路径


     方法一(加入dll):

    using System.Runtime.InteropServices;
    #region
    /// <summary> /// 取exe文件绝对路径 /// </summary> /// <param name="hModule"></param> /// <param name="lpFileName"></param> /// <param name="nSize"></param> /// <returns></returns> [DllImport("kernel32")] public static extern int GetModuleFileName(IntPtr hModule, [Out] StringBuilder lpFileName,int nSize); #endregion
    #region
    IntPtr processHandle = IntPtr.Zero;
    StringBuilder exeFileNamestrB = new StringBuilder(260);
    GetModuleFileName(processHandle, exeFileNamestrB, exeFileNamestrB.Capacity);
    #endregion

    方法二:(推荐-exe等)

    System.Environment.CurrentDirectory  // 获取或设置当前工作目录的完全限定路径,使用System.Environment.CurrentDirectory只能得到运行的虚拟路径,得不到物理路径

    方法三:其他定时任务运行时请用:

    System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

    方法四:(推荐-WCF,WebServer等)

    System.Web.Hosting.HostingEnvironment  // 

    例:WebServer使用外部文件时:

    static readonly string strCon = System.Web.Hosting.HostingEnvironment.MapPath($"/bin") + @"/data.db";
    //C:\Users\PC1\source\repos\SqliteWebApp\SqliteWebApp\bin/data.db

    方案五:(推荐)

    string fileName = Application.StartupPath+@"Popup.bmp";

    365个夜晚,我希望做到两天更一篇博客。加油,小白!
  • 相关阅读:
    JAVAWE第一天
    01--2048实例开篇
    10--动作系统(四)动作类中的reverse方法
    09--动作系统(三)使用持续动作
    08--动作系统(二)使用即时动作
    【转】cocos2d-x windows开发环境配置
    07--动作系统(一)
    06--触摸事件响应
    05--简单场景切换与精灵创建
    04--简单菜单使用
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/14011410.html
Copyright © 2020-2023  润新知