zoukankan      html  css  js  c++  java
  • C#获取路径

                //获取到bin目录的下层路径:binDebug

                string aa = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

                string cc = System.AppDomain.CurrentDomain.BaseDirectory;

                

                //获取到bin目录:binDebug

                string dd = System.Environment.CurrentDirectory;

                string ee = System.IO.Directory.GetCurrentDirectory();

                string ff = System.Windows.Forms.Application.StartupPath;

     

                //获取程序.exe

                string bb = System.Windows.Forms.Application.ExecutablePath;

    winform使用相对路径读取文件

    目录结构如下图所示:

     

    方法一:由于生成的exe文件在bindebug目录下,可以使用向上查找目录的方式获取要读取的xml文件

    代码如下:
    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);
    }
  • 相关阅读:
    javascript 中的暗物质 闭包
    关于使用 jBox 对话框的提交问题
    Orchard 的项目结构解决方案文件夹的原理与使用
    翻译:创建 Windows8 应用 Part I: Hello, world!
    翻译:FireBug 1.10 新特性
    SQL数据库设计经验
    我的女孩
    在WINDOWS下安装MRTG全攻略网络流量监控
    ASP实用函数库
    DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
  • 原文地址:https://www.cnblogs.com/candyzhmm/p/6398036.html
Copyright © 2011-2022 走看看