C#学习

news2024/9/22 3:43:29

C#学习

  • 1.B站丑萌气质狗
    • C#的循环-判断
    • 泛型
    • 错误处理
    • 面向对象
    • static的使用
    • 定义showInfo类和Hero类
  • 在这里插入图片描述 然后在该解决方案add新建一个类库,点击rebuild,会在bin文件夹下生成.dll文件 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/df960c0eefc04f8db1b3ba6488b01c5d.png) 现在解决方案里有ConsoleAppFrameWork和ClassLibrary两个项目,需要把类库中的.dll文件引入进来,然后会在ConsoleAppFrameWork的References下面看到。 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/ec1945fb0af147b7984ba1f3b8387761.png) ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/4eaf405189824917b6a429b1588c86b4.png)
    • 文件读写

1.B站丑萌气质狗

视频链接

C#的循环-判断

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppFramework
{
    internal class Program
    {
        static void Main(string[] args)
        {
            for (int i=0; i<=2;i++) {
                GetUserInfo();//第一个用户

            }
           
            Console.ReadKey();
        }
        static string ChangeData(String name1) {
            if (name1 == "张三")
            {
                Console.WriteLine("你输入的是张三");
                name1 = "法外狂徒张三";
            }
            else if (name1 == "李四")
            {
                Console.WriteLine("你输入的是李四");
                name1 = "无情铁手李四";
            }
            else if (name1 == "王五")
            {
                Console.WriteLine("你输入的是王五");
                name1 = "柔情姐姐王五";
            }
            else {
                Console.WriteLine("你输入的不正确,无法修改");
            }
            return name1;
        }
        public static void GetUserInfo() {
            Console.WriteLine("这是我的第一个程序");
            Console.WriteLine("请输入你的故乡");
            string home = Console.ReadLine();
            Console.WriteLine("请输入你的名字");
            string name = Console.ReadLine();
            name=ChangeData(name);
            Console.WriteLine("请输入你的年龄");
            string age = Console.ReadLine();
            Console.WriteLine("请输入你的爱好");
            string hobby = Console.ReadLine();
            Console.WriteLine("");
            Console.WriteLine($"故乡:{home}  名字:{name}  年龄:{age}  爱好:{hobby}");
        }
    }
}

泛型

功能:查找泛型集合中有几个【张三】

      static void Main(string[] args)
      {

          //泛型集合
          List<string> names = new List<string>();
          names.Add("张三");
          names.Add("李四");
          names.Add("王五");
          int num = 0;
          for (int i = 0; i < names.Count; i++)
          {
              if (names[i] == "张三")
              {
                  num = num + 1;
              }
          }
          Console.WriteLine(num);

          Console.ReadKey();
      }

错误处理

(1)不循环的错误处理

      static void Main(string[] args)
      {

          int age = 0;
          Console.WriteLine("请输入年龄:");
          string input = Console.ReadLine();
          try {
              age = int.Parse(input); 
          }
          catch { 
              Console.WriteLine("请输入正确的年龄(数字字符串)!");
              return;//捕获到错误后直接return,要不然后面的代码还会执行,错误的打印出10
          }
          age += 10;
          Console.WriteLine("你的年龄是:"+age.ToString());
          Console.ReadKey();
          
      }

(2)循环的错误处理,直到控制台输入正确的年龄,跳出循环

        static void Main(string[] args)
        {

            int age = 0;

            bool flag = true;
            for (; flag; ) {//直到输入正确的年龄,跳出循环
                Console.WriteLine("请输入年龄:");
                string input = Console.ReadLine();
                try
                {
                    age = int.Parse(input);
                    flag= false;

                }
                catch
                {
                    Console.WriteLine("请输入正确的年龄(数字字符串)!");
                    flag = true;//出现年龄错误时一直进入循环,直到输入正确的年龄
                }
                age += 10;
               // Console.WriteLine("你的年龄是:" + age.ToString());及时出错也会进入这一步,年龄是0+10=10
            }
            Console.WriteLine("请输入姓名!");
            string name = Console.ReadLine();
            Console.WriteLine("请输入家乡!");
            string hometown = Console.ReadLine();
            Console.WriteLine($"故乡:{hometown}  名字:{name}  十年后的年龄:{age}");
            Console.ReadKey();


        }

面向对象

基本属性

    internal class Program
    {
        static void Main(string[] args)
        {   
            Console.WriteLine("锄禾日当午!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("汗滴禾想吐!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("谁知盘中餐!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("粒粒皆辛苦!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("这位农民工是谁?");
            string name=Console.ReadLine();
            Console.WriteLine("这位农民原来是--->" + name);
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            //这位农民的基本信息,技能及伤害
            string sex = "男";
            int age = 22;
            int high = 180;
            int shanghai = 100;
            int xueliang = 100;
            string jineng1 = "飞檐走壁";
            int jinneg1shanghai = 20;
            string jineng2 = "飞沙走石";
            int jinneg2shanghai = 30;
            Console.WriteLine("性别:"+sex);
            Console.WriteLine("年龄:"+age);
            Console.WriteLine("身高:"+high);
            Console.WriteLine("基础伤害:"+shanghai);
            Console.WriteLine("基础血量:"+xueliang);
            Console.WriteLine("技能1伤害:"+jinneg1shanghai);
            Console.WriteLine("技能2伤害:"+jinneg2shanghai);
            Console.ReadKey();
            // Console.WriteLine("基础伤害:");

            Console.WriteLine("此时远处传来一个粗犷的声音");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            Console.WriteLine("走进一看是一位警察");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            Console.WriteLine("这个警察的名称");
            string jingchaname = Console.ReadLine();
            Console.WriteLine("这位警察原来是--->" + jingchaname);
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();

            //这位警察的基本信息,技能及伤害
            string sex2 = "男";
            int age2 = 22;
            int high2 = 180;
            int shanghai2 = 100;
            int xueliang2 = 100;
            string jineng11 = "跆拳道";
            int jinneg11shanghai = 20;
            string jineng22 = "狙击手";
            int jinneg22shanghai = 30;
            Console.WriteLine("性别:" + sex2);
            Console.WriteLine("年龄:" + age2);
            Console.WriteLine("身高:" + high2);
            Console.WriteLine("基础伤害:" + shanghai2);
            Console.WriteLine("基础血量:" + xueliang2);
            Console.WriteLine("技能1伤害:" + jinneg11shanghai);
            Console.WriteLine("技能2伤害:" + jinneg22shanghai);
            Console.ReadKey();



        }

(2)封装类属性后创键的对象

class Hero {
    public string name = "";
    public string sex = "男";
    public int age = 22;
    public int high = 180;
    public int shanghai = 100;
    public int xueliang = 100;
    public string jineng1 = "飞檐走壁";
    public int jinneg1shanghai = 20;
    public string jineng2 = "飞沙走石";
    public int jinneg2shanghai = 30;
}

internal class Program
{
    static void Main(string[] args)
    {   
        Console.WriteLine("锄禾日当午!");
        Console.WriteLine("(请按任意键继续)!");
        Console.ReadKey();//代码暂停,输入任意键继续
        Console.WriteLine("汗滴禾想吐!");
        Console.WriteLine("(请按任意键继续)!");
        Console.ReadKey();//代码暂停,输入任意键继续
        Console.WriteLine("谁知盘中餐!");
        Console.WriteLine("(请按任意键继续)!");
        Console.ReadKey();//代码暂停,输入任意键继续
        Console.WriteLine("粒粒皆辛苦!");
        Console.WriteLine("(请按任意键继续)!");
        Console.ReadKey();//代码暂停,输入任意键继续
        Console.WriteLine("这位农民工是谁?");

        Hero hr1=new Hero();
        hr1.name =Console.ReadLine();
        Console.WriteLine("这位农民原来是--->" + hr1.name);
        Console.WriteLine("(请按任意键继续)!");
        Console.ReadKey();
        //这位农民的基本信息,技能及伤害
        

        hr1.sex = "男";
        hr1.age = 22;
        hr1.high = 180;
        hr1.shanghai = 100;
        hr1.xueliang = 100;
        hr1.jineng1 = "飞檐走壁";
        hr1.jinneg1shanghai = 20;
        hr1.jineng2 = "飞沙走石";
        hr1.jinneg2shanghai = 30;
        Console.WriteLine("性别:"+ hr1.sex);
        Console.WriteLine("年龄:"+ hr1.age);
        Console.WriteLine("身高:"+ hr1.high);
        Console.WriteLine("基础伤害:"+ hr1.shanghai);
        Console.WriteLine("基础血量:"+ hr1.xueliang);
        Console.WriteLine("技能1伤害:"+ hr1.jinneg1shanghai);
        Console.WriteLine("技能2伤害:"+ hr1.jinneg2shanghai);
        Console.ReadKey();
        // Console.WriteLine("基础伤害:");

        Console.WriteLine("此时远处传来一个粗犷的声音");
        Console.WriteLine("(请按任意键继续)!");
        Console.ReadKey();
        Console.WriteLine("走进一看是一位警察");
        Console.WriteLine("(请按任意键继续)!");
        Console.ReadKey();
        Console.WriteLine("这个警察的名称");

        Hero hr2 = new Hero();
        hr2.name = Console.ReadLine();
        Console.WriteLine("这位警察原来是--->" + hr2.name);
        Console.WriteLine("(请按任意键继续)!");
        Console.ReadKey();

        //这位警察的基本信息,技能及伤害
        hr2.sex= "男";
        hr2.age = 22;
        hr2.high = 180;
        hr2.shanghai = 100;
        hr2.xueliang= 100;
        hr2.jineng1 = "跆拳道";
        hr2.jinneg1shanghai = 20;
        hr2.jineng2 = "狙击手";
        hr2.jinneg2shanghai = 30;
        Console.WriteLine("性别:" + hr2.sex);
        Console.WriteLine("年龄:" + hr2.age);
        Console.WriteLine("身高:" + hr2.high);
        Console.WriteLine("基础伤害:" + hr2.shanghai);
        Console.WriteLine("基础血量:" + hr2.xueliang);
        Console.WriteLine("技能1伤害:" + hr2.jinneg1shanghai);
        Console.WriteLine("技能2伤害:" + hr2.jinneg2shanghai);
        Console.ReadKey();



    }
      

(3)封装到hero类中,并定义showInfo方法

    class Hero {
        public string name = "";
        public string sex = "男";
        public int age = 22;
        public int high = 180;
        public int shanghai = 100;
        public int xueliang = 100;
        public string jineng1 = "飞檐走壁";
        public int jinneg1shanghai = 20;
        public string jineng2 = "飞沙走石";
        public int jinneg2shanghai = 30;

        public void showInfo(){
        Console.WriteLine("性别:" + sex);
        Console.WriteLine("年龄:" + age);
        Console.WriteLine("身高:" + high);
        Console.WriteLine("基础伤害:" + shanghai);
        Console.WriteLine("基础血量:" + xueliang);
        Console.WriteLine("技能1伤害:" + jinneg1shanghai);
        Console.WriteLine("技能2伤害:" + jinneg2shanghai);
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {   
            Console.WriteLine("锄禾日当午!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("汗滴禾想吐!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("谁知盘中餐!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("粒粒皆辛苦!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("这位农民工是谁?");

            Hero hr1=new Hero();
            hr1.name =Console.ReadLine();
            Console.WriteLine("这位农民原来是--->" + hr1.name);
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            //这位农民的基本信息,技能及伤害
            

            hr1.sex = "男";
            hr1.age = 22;
            hr1.high = 180;
            hr1.shanghai = 100;
            hr1.xueliang = 100;
            hr1.jineng1 = "飞檐走壁";
            hr1.jinneg1shanghai = 20;
            hr1.jineng2 = "飞沙走石";
            hr1.jinneg2shanghai = 30;
            //Console.WriteLine("性别:" + hr1.sex);
            //Console.WriteLine("年龄:" + hr1.age);
            //Console.WriteLine("身高:" + hr1.high);
            //Console.WriteLine("基础伤害:" + hr1.shanghai);
            //Console.WriteLine("基础血量:" + hr1.xueliang);
            //Console.WriteLine("技能1伤害:" + hr1.jinneg1shanghai);
            //Console.WriteLine("技能2伤害:" + hr1.jinneg2shanghai);
            hr1.showInfo();
            Console.ReadKey();
            // Console.WriteLine("基础伤害:");

            Console.WriteLine("此时远处传来一个粗犷的声音");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            Console.WriteLine("走进一看是一位警察");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            Console.WriteLine("这个警察的名称");

            Hero hr2 = new Hero();
            hr2.name = Console.ReadLine();
            Console.WriteLine("这位警察原来是--->" + hr2.name);
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();

            //这位警察的基本信息,技能及伤害
            hr2.sex= "男";
            hr2.age = 22;
            hr2.high = 180;
            hr2.shanghai = 100;
            hr2.xueliang= 100;
            hr2.jineng1 = "跆拳道";
            hr2.jinneg1shanghai = 20;
            hr2.jineng2 = "狙击手";
            hr2.jinneg2shanghai = 30;
            //Console.WriteLine("性别:" + hr2.sex);
            //Console.WriteLine("年龄:" + hr2.age);
            //Console.WriteLine("身高:" + hr2.high);
            //Console.WriteLine("基础伤害:" + hr2.shanghai);
            //Console.WriteLine("基础血量:" + hr2.xueliang);
            //Console.WriteLine("技能1伤害:" + hr2.jinneg1shanghai);
            //Console.WriteLine("技能2伤害:" + hr2.jinneg2shanghai);
            hr2.showInfo();
            Console.ReadKey();



        }

static的使用

static可以直接使用,不用对类实例化。

定义showInfo类和Hero类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppFramework
{
    class Hero {
        public string name = "";
        public string sex = "男";
        public int age = 22;
        public int high = 180;
        public int shanghai = 100;
        public int xueliang = 100;
        public string jineng1 = "飞檐走壁";
        public int jinneg1shanghai = 20;
        public string jineng2 = "飞沙走石";
        public int jinneg2shanghai = 30;

       
    }
    class ShowInfo{
        public static void showInfo(Hero hero)
        {
            Console.WriteLine("性别:" + hero.sex);
            Console.WriteLine("年龄:" + hero.age);
            Console.WriteLine("身高:" + hero.high);
            Console.WriteLine("基础伤害:" + hero.shanghai);
            Console.WriteLine("基础血量:" + hero.xueliang);
            Console.WriteLine("技能1伤害:" + hero.jinneg1shanghai);
            Console.WriteLine("技能2伤害:" + hero.jinneg2shanghai);
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {   
            Console.WriteLine("锄禾日当午!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("汗滴禾想吐!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("谁知盘中餐!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("粒粒皆辛苦!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("这位农民工是谁?");

            Hero hr1=new Hero();
            hr1.name =Console.ReadLine();
            Console.WriteLine("这位农民原来是--->" + hr1.name);
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            //这位农民的基本信息,技能及伤害
            

            hr1.sex = "男";
            hr1.age = 22;
            hr1.high = 180;
            hr1.shanghai = 100;
            hr1.xueliang = 100;
            hr1.jineng1 = "飞檐走壁";
            hr1.jinneg1shanghai = 20;
            hr1.jineng2 = "飞沙走石";
            hr1.jinneg2shanghai = 30;

            ShowInfo.showInfo(hr1);//不需要实例化ShowInfo类
            Console.ReadKey();

            Console.WriteLine("此时远处传来一个粗犷的声音");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            Console.WriteLine("走进一看是一位警察");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            Console.WriteLine("这个警察的名称");

            Hero hr2 = new Hero();
            hr2.name = Console.ReadLine();
            Console.WriteLine("这位警察原来是--->" + hr2.name);
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();

            //这位警察的基本信息,技能及伤害
            hr2.sex= "男";
            hr2.age = 22;
            hr2.high = 180;
            hr2.shanghai = 100;
            hr2.xueliang= 100;
            hr2.jineng1 = "跆拳道";
            hr2.jinneg1shanghai = 20;
            hr2.jineng2 = "狙击手";
            hr2.jinneg2shanghai = 30;
            ShowInfo.showInfo(hr1);//不需要实例化ShowInfo类
            Console.ReadKey();
        }
    }
}

把类抽离出来,放到新的文件里【新建了Hero和ShowInfo文件夹】
在这里插入图片描述
program.cs

using MyShowInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppFramework
{
    internal class Program
    {
        static void Main(string[] args)
        {   
            Console.WriteLine("锄禾日当午!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("汗滴禾想吐!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("谁知盘中餐!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("粒粒皆辛苦!");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();//代码暂停,输入任意键继续
            Console.WriteLine("这位农民工是谁?");

            Hero hr1=new Hero();
            hr1.name =Console.ReadLine();
            Console.WriteLine("这位农民原来是--->" + hr1.name);
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            //这位农民的基本信息,技能及伤害
            

            hr1.sex = "男";
            hr1.age = 22;
            hr1.high = 180;
            hr1.shanghai = 100;
            hr1.xueliang = 100;
            hr1.jineng1 = "飞檐走壁";
            hr1.jinneg1shanghai = 20;
            hr1.jineng2 = "飞沙走石";
            hr1.jinneg2shanghai = 30;

            ShowInfo.showInfo(hr1);//不需要实例化ShowInfo类
            Console.ReadKey();

            Console.WriteLine("此时远处传来一个粗犷的声音");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            Console.WriteLine("走进一看是一位警察");
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();
            Console.WriteLine("这个警察的名称");

            Hero hr2 = new Hero();
            hr2.name = Console.ReadLine();
            Console.WriteLine("这位警察原来是--->" + hr2.name);
            Console.WriteLine("(请按任意键继续)!");
            Console.ReadKey();

            //这位警察的基本信息,技能及伤害
            hr2.sex= "男";
            hr2.age = 22;
            hr2.high = 180;
            hr2.shanghai = 100;
            hr2.xueliang= 100;
            hr2.jineng1 = "跆拳道";
            hr2.jinneg1shanghai = 20;
            hr2.jineng2 = "狙击手";
            hr2.jinneg2shanghai = 30;
            ShowInfo.showInfo(hr1);//不需要实例化ShowInfo类
            Console.ReadKey();



        }
      

        static string ChangeData(String name1) {
            if (name1 == "张三")
            {
                Console.WriteLine("你输入的是张三");
                name1 = "法外狂徒张三";
            }
            else if (name1 == "李四")
            {
                Console.WriteLine("你输入的是李四");
                name1 = "无情铁手李四";
            }
            else if (name1 == "王五")
            {
                Console.WriteLine("你输入的是王五");
                name1 = "柔情姐姐王五";
            }
            else {
                Console.WriteLine("你输入的不正确,无法修改");
            }
            return name1;
        }
        public static void GetUserInfo() {
            Console.WriteLine("这是我的第一个程序");
            Console.WriteLine("请输入你的故乡");
            string home = Console.ReadLine();
            Console.WriteLine("请输入你的名字");
            string name = Console.ReadLine();
            name=ChangeData(name);
            Console.WriteLine("请输入你的年龄");
            string age = Console.ReadLine();
            Console.WriteLine("请输入你的爱好");
            string hobby = Console.ReadLine();
            Console.WriteLine("");
            Console.WriteLine($"故乡:{home}  名字:{name}  年龄:{age}  爱好:{hobby}");
        }
    }
}

ShowInfo.cs

using ConsoleAppFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyShowInfo
{

        class ShowInfo
        {
            public static void showInfo(Hero hero)
            {
                Console.WriteLine("性别:" + hero.sex);
                Console.WriteLine("年龄:" + hero.age);
                Console.WriteLine("身高:" + hero.high);
                Console.WriteLine("基础伤害:" + hero.shanghai);
                Console.WriteLine("基础血量:" + hero.xueliang);
                Console.WriteLine("技能1伤害:" + hero.jinneg1shanghai);
                Console.WriteLine("技能2伤害:" + hero.jinneg2shanghai);
            }
        }
    }
        

Hero.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppFramework
{
    class Hero
    {
        public string name = "";
        public string sex = "男";
        public int age = 22;
        public int high = 180;
        public int shanghai = 100;
        public int xueliang = 100;
        public string jineng1 = "飞檐走壁";
        public int jinneg1shanghai = 20;
        public string jineng2 = "飞沙走石";
        public int jinneg2shanghai = 30;


    }

}

在这里插入图片描述
然后在该解决方案add新建一个类库,点击rebuild,会在bin文件夹下生成.dll文件
在这里插入图片描述
现在解决方案里有ConsoleAppFrameWork和ClassLibrary两个项目,需要把类库中的.dll文件引入进来,然后会在ConsoleAppFrameWork的References下面看到。
在这里插入图片描述
在这里插入图片描述

重新启动,将ConsoleAppFrameWork设为启动项,再次执行时多了两个文件classLibrary.cll和pdb文件,【这个是结果】
可能处出现的问题:哪些文件中使用到新建的类库,就在哪些文件里using 类库【类库记得rebuild,.dll文件】
在这里插入图片描述

文件读写

删除一个文件
ChangFile/programmer.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChangeFile
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path = "C:/Users/sesa779614/learning";
            //获取路径下的所有文件
            DirectoryInfo root=new DirectoryInfo(path);
            FileInfo[] files= root.GetFiles();
          
            List< FileInfo > filesInfo = files.ToList();

            for (int i=0; i<filesInfo.Count;i++) { 
                FileInfo file = filesInfo[i];
                if (file.Name== "Async in C# 5.0.pdf") { 
                    System.IO.File.Delete(file.FullName);
                    Console.WriteLine("Async in C# 5.0.pdf已经被删除了");
                }
               // Console.WriteLine(file.FullName);
               // Console.WriteLine(file.Name);
            }

            //string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

            //foreach (string file in files)
            //{
            //    Console.WriteLine(file);
            //}
            Console.ReadKey();
        }
    }
}

文件改名

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChangeFile
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path = "C:/Users/sesa779614/learning";
            //获取路径下的所有文件
            DirectoryInfo root=new DirectoryInfo(path);
            FileInfo[] files= root.GetFiles();
          
            List< FileInfo > filesInfo = files.ToList();

            for (int i=0; i<filesInfo.Count;i++) { 
                FileInfo file = filesInfo[i];
                string filename = file.Name;

                if (filename== "Async in C# 5.0.pdf") { 
                    System.IO.File.Delete(file.FullName);
                    Console.WriteLine("Async in C# 5.0.pdf已经被删除了");
                }
                if (filename.Contains("C#")) {
                    //如果文件名中包含C#,就给文件改名称
                    string srgFileName=filesInfo[i].FullName;
                    string destFileName=filesInfo[i].Directory.FullName+"/gaigai"+ filesInfo[i].Extension;//文件夹路径+新的文件名
                    File.Move(srgFileName,destFileName);

                }
                Console.WriteLine(filename);
                Console.WriteLine(filename.Contains("C#"));
            }

            //string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

            //foreach (string file in files)
            //{
            //    Console.WriteLine(file);
            //}
            Console.ReadKey();

        
        
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1926384.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SAC-IA粗配准算法记录

1. 算法思路 SAC-IA(Sample Consensus Initial Aligment,SAC-IA)粗配准算法是一种基于局部特征描述子的点云粗配准算法,其需要计算点云的快速点特征直方图(FPFH)来保持对应点对之间的相似关系,根据相似关系来搜索点云中的对应点。其基本原理是采用采样一致性的思想,通过查…

Zabbix6.0使用自带模板(Redis by Zabbix agent 2)监控Redis数据库

注意&#xff1a;Zabbix6.0使用Redis by Zabbix agent 2 模板可直接监控Redis数据。 1、添加Redis账号密码信息(如果Redis没有设置密码可省略此步骤) vim zabbix_agent2.confPlugins.Redis.Sessions.redis.Uritcp://redis.huayunworld.com:6379 Plugins.Redis.Sessions.redis…

工具推荐|语音轻松记笔记,AI帮你识别和润色

# 你日常有没有遇到这样的场景&#xff1f; 偶尔有一些奇思妙想想要记录下来&#xff0c;但没有一个轻量的工具&#xff0c;往往会想着想着就把这个想法抛之脑后。特别是搞短视频的&#xff0c;你也许希望把当时的想法录下来&#xff0c;稍微剪辑下就能出一条不错的口播视频。…

外泌体相关基因肝癌临床模型预测——2-3分纯生信文章复现——5.拷贝数变异及突变图谱(1)

内容如下: 1.外泌体和肝癌TCGA数据下载 2.数据格式整理 3.差异表达基因筛选 4.预后相关外泌体基因确定 5.拷贝数变异及突变图谱 6.外泌体基因功能注释 7.LASSO回归筛选外泌体预后模型 8.预后模型验证 9.预后模型鲁棒性分析 10.独立预后因素分析及与临床的相关性分析…

CMU 15-213 CSAPP. Ch9. Virtual Memory

CMU 15-213 CSAPP (Ch1~Ch3) CMU 15-213 CSAPP (Ch5~Ch7) CMU 15-213 CSAPP (Ch8) CMU 15-213 CSAPP (Ch9) CMU 15-213 CSAPP (Ch10) 视频链接 课件链接 课程补充 该课程使用 64位 编译器&#xff01; Ch9. Virtual Memory 9.1 Address spaces 将内存看成数组&#xff0c;物…

OpenGL笔记十二之实现三角形在屏幕横向上往复运动的动画

OpenGL笔记十二之实现三角形在屏幕横向上往复运动的动画 —— 2024-07-14 晚上 bilibili赵新政老师的教程看后笔记 code review! 文章目录 OpenGL笔记十二之实现三角形在屏幕横向上往复运动的动画1.运行2.vs3.fs4.main.cpp的关键部分 1.运行 2.vs #version 330 core layout …

成都工业学院2022级数据库原理及应用专周课程学生选课系统(进阶篇)

运行环境 操作系统&#xff1a;Windows 11 家庭版 运行软件&#xff1a;Visual Studio Code Navicat Premium 16 进阶内容 过程函数改为触发器 例如将学生选课的过程函数改为对选课表添加触发器 使用ruoyi-vue实现可视化 配置并运行ruoyi-vue 进行代码生成 将生成的代码添…

【Linux】03.权限

一、权限的概念 Linux下有两种用户&#xff1a;超级用户&#xff08;root&#xff09;、普通用户。 超级用户&#xff1a;可以在 linux 系统下做任何事情&#xff0c;不受限制普通用户&#xff1a;在linux下做有限的事情超级用户的命令提示符是“#”&#xff0c;普通用户的命…

ctfshow-web入门-php特性(web104-web108)

目录 1、web104 2、web105 3、web106 4、web107 5、web108 1、web104 需要传入的 v1 和 v2 进行 sha1 加密后相等。 解法1&#xff1a; 这里都没有判断 v1 和 v2 是否相等&#xff0c;我们直接传入同样的内容加密后肯定也一样。 ?v21 post&#xff1a; v11 拿到 flag…

C++从入门到起飞之——输入输出!

目录 1.命名空间 1.1namespace的价值 1.2namespace的定义 1.3命名空间使⽤ 2.C输⼊&输出 3.完结散花 个人主页&#xff1a;秋风起&#xff0c;再归来~ C从入门到起飞 个人格言&#xff1a;悟已往之不谏&#xff0c;知来者犹可追 克心守己…

Redis中的持久化详解

本篇文章会对Redis的持久化进行详解。主要涉及到的方面有&#xff1a;redis为什么需要持久化、redis怎么进行的持久化、持久化的方式都有哪些、每种持久化方式的优缺点是什么、持久化的流程进行展开详解。希望本篇文章会对你有所帮助。 文章目录 一、持久化简介 二、Redis的持久…

java日常开发中常用的集合工具类方法归总(java8 stream)

1、创建map集合的方式 方式1&#xff1a; Map<String, Object> map new HashMap<>(); map.put("a", "test"); map.put("b", "since"); 方式2&#xff1a; Map<String, Object> map2 new HashMap<>() {{…

事务ACID四大特性(图文详解~)

ACID ACID 是数据库管理系统中保证事务正确执行的四大特性的缩写。 1. Atomicity&#xff08;原子性&#xff09;&#xff1a; 原子性指事务是不可分割的单位&#xff0c;要么全部执行成功&#xff0c;要么全部失败回滚。—All or nothing. 通常使用日志记录机制来启动回滚功…

昇思25天学习打卡营第21天|CycleGAN 图像风格迁移互换

今天是参加昇思25天学习打卡营的第21天&#xff0c;今天打卡的课程是“CycleGAN 图像风格迁移互换”&#xff0c;这里做一个简单的分享。 1.简介 从今天开始到第25天的学习内容都是生成式网络的内容。今天要学习的第一个生成式网络是CycleGAN&#xff0c;目标是实现图像风格迁…

苹果将在2025年春季通过iOS更新大幅提升Siri的智能|TodayAI

据彭博社&#xff08;Bloomberg&#xff09;最新报道&#xff0c;苹果公司&#xff08;Apple&#xff09;计划在2025年春季推出其最新的Apple Intelligence AI系统&#xff0c;通过iOS 18.4版本更新为广大iPhone用户带来更智能的Siri。这一消息由科技行业知名记者马克古尔曼在其…

【从0到1进阶Redis】主从复制

1、概念 主从复制&#xff0c;是指将一个台 Redis 服务器的数据&#xff0c;复制到其他的 Redis 服务器。前者称为主节点&#xff08;master/leader&#xff09;&#xff0c;后者称为从节点&#xff08;slave/follower&#xff09;&#xff1b;数据的复制是单向的&#xff0c;…

计算机网络生成树协议介绍与实践

生成树协议 1.环路 二层环路&#xff1a;数据链路层&#xff0c;交换机&#xff08;二层设备&#xff09;通过线路连接环状。即物理成环并且没有开启防环协议。 危害&#xff1a;广播风暴&#xff1a;交换机将未知帧广播&#xff0c;收到后的交换机继续广播&#xff0c;不断…

C语言--递归

曾经有一个段子&#xff1a;上大学时&#xff0c;我们的c语言老师说&#xff1a;学c时&#xff0c;如果有50%的同学死在了循环上面&#xff0c;那么就有90%的同学死在了递归上面。接下来&#xff0c;就来看看递归是怎么个事&#xff1f; 一.递归的介绍 递归是指一个函数直接或…

CV09_深度学习模块之间的缝合教学(4)--调参

深度学习就像炼丹。炉子就是模型&#xff0c;火候就是那些参数&#xff0c;材料就是数据集。 1.1 参数有哪些 调参调参&#xff0c;参数到底是哪些参数&#xff1f; 1.网络相关的参数&#xff1a;&#xff08;1&#xff09;神经网络网络层 &#xff08;2&#xff09;隐藏层…

偶数位的数c++

题目描述 给你两个整数 l,r&#xff0c;求 l∼r 范围内有多少个位数为偶数的数。 输入 一行两个整数 l,r。 输出 输出位数为偶数的数的数量。 样例输入 5 15样例输出 6 提示 样例解释 10,11,12,13,14,15 位数为偶数&#xff0c;都是两位数。 数据规模与约定 对于 1…