winform使用相对路径读取文件的方法

获取exe文件的路径进行截取,分两次进行,然后拼接文件名,形成全路径

代码如下:
string haarXmlPath = @"haarcascade_frontalface_alt_tree.xml";

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

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

System.Diagnostics命名空间 :

string str1 =Process.GetCurrentProcess().MainModule.FileName;  //可获得当前执行的exe的文件名。 

System 命名空间:

string str2=Environment.CurrentDirectory;  //获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。

System.IO命名空间:

string str3=Directory.GetCurrentDirectory();  //获取应用程序的当前工作目录。

string str4=AppDomain.CurrentDomain.BaseDirectory;  //获取基目录,它由程序集冲突解决程序用来探测程序集。

string str5=Application.StartupPath;  //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

string str6=Application.ExecutablePath;  //获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;  //获取或设置包含该应用程序的目录的名称。

其他方法介绍:

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName   获取模块的完整路径。

2. System.Environment.CurrentDirectory   获取和设置当前目录(该进程从中启动的目录)的完全限定目录。

3. System.IO.Directory.GetCurrentDirectory()    获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:"www里,这个函数有可能返回C:"Documents and Settings"ZYB",或者C:"Program Files"Adobe",有时不一定返回什么东东,我也搞不懂了。

4. System.AppDomain.CurrentDomain.BaseDirectory   获取程序的基目录。

5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase   获取和设置包括该应用程序的目录的名称。

6. System.Windows.Forms.Application.StartupPath    获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"""而已

7. System.Windows.Forms.Application.ExecutablePath   获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。

获取嵌入的资源文件的路径和方法

eg1.获取项目资源文件中的图片:

pictureBox1.BackgroundImage = global::工程名.Properties.Resources.图片名(没有后缀);

另附一种方法:

//获取项目中“资源文件”的数据
 
Assembly asm = Assembly.GetExecutingAssembly(); 
ResourceManager rm = new ResourceManager("TestCustomForm.Properties.Resources", asm); 
String str = rm.GetString("ApplicationName");   
//添加到资源文件的字符串
Image img = rm.GetObject("bamboo") as Image;  //添加到资源文件的图片

eg2.获取项目添加项文件夹下的htm文件(注意:文件属性中生成操作得改为(嵌入的资源))不包括具体调用htm中具体字节方法:

private string getStringResource(String name)
        {
            //根据assembly名称加载程序集到当前的Appdomain
            //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("MyAssembly");
            System.Reflection.Assembly asm = this.GetType().Assembly;
            //在这个文件中列出的所有资源
            System.Diagnostics.Debug.WriteLine("found resouces:");
            foreach (String rs in asm.GetManifestResourceNames())
                {
                    System.Diagnostics.Debug.WriteLine(rs);
                 name = this.GetType().Namespace + "." + name;
                }
                
            System.IO.Stream strm = asm.GetManifestResourceStream(name);
            //默认的系统编码转换为字符串
            return new System.IO.StreamReader(strm, System.Text.Encoding.Default).ReadToEnd();
        }
                    

图片调用:Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"TestCustomForm.Res.button.btndown.bmp"));

其中:TestCustomForm为项目名称,Res为项目下的文件夹,button为Res的子文件夹,btndown.bmp是文件名称。

转载于:https://www.cnblogs.com/jiangshuai52511/p/7683704.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值