【C#】代码整理备忘——1

1.wpf中弹出窗口信息提示:

MessageBox.Show("请先加载模型");

2.打开文件夹用:

System.Windows.Forms.FolderBrowserDialog dir = new System.Windows.Forms.FolderBrowserDialog();
       if (dir.ShowDialog() == System.Windows.Forms.DialogResult.OK)
       {
        //正式获得所有图片的路径                                   
       }

3.打开单个文件用:

                OpenFileDialog open_model_dialog = new OpenFileDialog();
                open_model_dialog.Filter = "模型文件|*.model";//此处限定可以选择的相关后缀文件
                open_model_dialog.Multiselect = false;
                open_model_dialog.Title = "加载模型";
                if (open_model_dialog.ShowDialog() ?? false)
                {
                    string model_file_path = open_model_dialog.FileName;
                    //如果模型文件不存在则抛出错误
                    CNTKLibraryManagedExamples.CNTKModelCalledByCSharp.ThrowIfFileNotExist(model_file_path, string.Format("Error: The model does not exist.", model_file_path));
                    //加载模型
                    model_func = Function.Load(model_file_path, DeviceDescriptor.CPUDevice);
                }

4.打try-catch的配合:

            try
            {
               

            }
            catch (Exception load_model_error)
            {
                MessageBox.Show("加载模型出错    错误信息:" + load_model_error.Message);
            }

5.Path的各种操作,需要引用System.IO:

class Program
    {
        static void Main(string[] args)
        {

            //获取当前运行程序的目录
            string fileDir = Environment.CurrentDirectory;
            Console.WriteLine("当前程序目录:"+fileDir);
            
            //一个文件目录
            string filePath = "C:\\JiYF\\BenXH\\BenXHCMS.xml";
            Console.WriteLine("该文件的目录:"+filePath);

            string str = "获取文件的全路径:" + Path.GetFullPath(filePath);   //-->C:\JiYF\BenXH\BenXHCMS.xml
            Console.WriteLine(str);
            str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath); //-->C:\JiYF\BenXH
            Console.WriteLine(str);
            str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath);  //-->BenXHCMS.xml
            Console.WriteLine(str);
            str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS
            Console.WriteLine(str);
            str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath); //-->.xml
            Console.WriteLine(str);
            str = "获取路径的根目录:" + Path.GetPathRoot(filePath); //-->C:\
            Console.WriteLine(str);
            Console.ReadKey();

        }
    }

参考于https://www.cnblogs.com/Joezhang433/p/10064535.html

5.Bitmap格式的读取:

Bitmap bmp = new Bitmap(Bitmap.FromFile(image_path));

6.将float或者double转换成两位小数的str:

String.Format("{0:F}", i)
或者
d.ToString("0.00###");

7.关于ref和out:
都是针对int这种类型的参数,传实参进去
1 关于重载

原则:有out|ref关键字的方法可以与无out和ref关键字的方法构成重载;但如想在out和ref间重载,编译器将提示:不能定义仅在ref和out的上的方法重载

2 关于调用前初始值

原则:ref作为参数的函数在调用前,实参必须赋初始值。否则编译器将提示:使用了未赋值的局部变量;

out作为参数的函数在调用前,实参可以不赋初始值。

3 关于在函数内,引入的参数初始值问题

原则:在被调用函数内,out引入的参数在返回前至少赋值一次,否则编译器将提示:使用了未赋值的out参数;

在被调用函数内,ref引入的参数在返回前不必为其赋初值。

但比不是说,引用类型就一定不会用到ref关键字,当我们试图将引用类型的重新赋值时

public void Changeref(RefClass rc)

{rc = new RefClass() { IntValue = 1000 };}

如果不加ref ,prc实际上是在函数内部将引用的地址,指向了另一new RefClass(),返回值会发现它的值未发生改变

8.通过递归获取总文件夹下的所有文件和所有子文件路径:

        private void GetAllImages(string path,List<string> image_patch_list)
        {
            //总的目录信息对象
            DirectoryInfo dir = new DirectoryInfo(path);
            //返回当前目录的文件列表
            FileInfo[] dir_files = dir.GetFiles();

            foreach (FileInfo file in dir_files)
            {
                if (file.FullName.ToUpper().EndsWith("JPG") | file.FullName.ToUpper().EndsWith("PNG")| 
                    file.FullName.ToUpper().EndsWith("BMP") | file.FullName.ToUpper().EndsWith("JPEG"))
                {
                    image_patch_list.Add(file.FullName);
                }                   
            }
            DirectoryInfo[] dir_subfolder = dir.GetDirectories();
            //获取子文件夹内的文件列表,递归遍历  
            foreach (DirectoryInfo d in dir_subfolder)
            {
                GetAllImages(d.FullName, image_patch_list);
            }
        }
                /// <summary>
        /// 通过递归过得总文件夹下的所有子文件夹路径
        /// </summary>
        /// <param name="path">总文件路径</param>
        /// <param name="image_patch_list">包含的所有子文件夹路径</param>
        private void GetAllFolder(string path, List<string> image_patch_list)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            DirectoryInfo[] dir_subfolder = dir.GetDirectories();
            FileInfo[] dir_files = dir.GetFiles();
            //如果该文件夹下无子文件夹
            if(dir_subfolder.Length == 0)
            {
                //把该文件夹路径加入
                image_patch_list.Add(dir.FullName);
            }
            //该文件夹下有子文件夹
            else
            {   
                //对每个子文件夹进行递归
                foreach(DirectoryInfo d in dir_subfolder)
                {
                    GetAllFolder(d.FullName, image_patch_list);
                }
            } 
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值