• 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是文件名称。

  • 相关阅读:
    从txt读取数据到Cvmat
    PCA之后进行归一化
    vc中调用exe文件并传递参数
    C#安装包过程
    电脑技巧
    DLL文件的创建与调用
    SVM调用方法
    舌顶上腭位置与作用
    KNN算法的實現
    How to Read a Paper
  • 原文地址:https://www.cnblogs.com/jiangshuai52511/p/7683704.html
Copyright © 2020-2023  润新知