c#文件夹和文件操作 实现图书管理系统

163 阅读2分钟

一、文件夹和文件操作(补充)

1.创建一个文件夹

image.png

2.新建文件

image.png

3.删除文件夹(同时会删除文件夹下的所有文件)

image.png

4.删除指定的文件

image.png

5.获取文件夹下的所有文件和文件夹

image.png

6.获取文件夹名

image.png

练习题一:

使用学到的文件操作,创建三个文件文件名分别是数学.txt,语文.txt,英语.txt,里面分别存放着50个学生的数学,语文,英语成绩,每一行放着名字和对应的分数,名字是小明1,小明2这样的规律显示,分数随机,每一行显示的完整内容是 科目+名字+分数,接下来用我们前面讲到的正则表达式,找出所有txt文件中的小明1的成绩逐行显示到1.txt文件中

image.png

练习题二:

请利用面向对象分析实现图书管理系统,具有录入,查询 ,删除,显示所有图书信息功能。借助文件操作的方式存储数据,将书籍存放到图书文件夹下,每本书都是一个txt文件,文件名为该书的名字,书中的初始内容为书名,图书的数据有书名,作者,价格,删除的时候要输入书的名字来进行删除

第一种方式:

image.png 第二种方式:

    namespace booksystem
{
    public class Book
    {
        public string name;
        public string author;
        public int price;

        //录入
        public void insertBook(List<string> listBook)
        {
            Console.WriteLine("请输入书名");
            string name = Console.ReadLine();
            Console.WriteLine("请输入作者");
            string author = Console.ReadLine();
            Console.WriteLine("请输入图书价格");
            string price = Console.ReadLine();
            bool flag = false;
            string newBookName = "./图书/" + name + ".txt";
            string newContent = "图书的书名为:<<" + name + ">> 作者为:" + author + " 图书价格为:" + price;
            if (name.Length>0)
            {
                for (int i = 0; i < listBook.Count; i++)
                {

                    if (!listBook[i].Equals(name))
                    {
                        flag = true;
                        File.AppendAllText(newBookName, newContent);
                        break;
                    }
                }
            }else
            {
                Console.WriteLine("您输入的图书名称为空,无法插入");
            }
            
            if (flag)
            {
                Console.WriteLine("您输入的{0}图书新增成功!", name);
            }

        }
        //查询
        public void selectBook(List<string> bookName)
        {
            Console.WriteLine("查询的图书如下:");
            for (int i = 0; i < bookName.Count; i++)
            {
                Console.WriteLine(bookName[i]);
            }
        }
        //删除
        public void deleteBook(List<string> listBook)
        {
            Console.WriteLine("请输入要删除的图书名称");
            string deleteBookName = Console.ReadLine();
            string deleteBookNameP = "./图书/" + deleteBookName + ".txt";
            bool flag = false; //监听是否删除成功
            if (deleteBookName.Length > 0)
            {
                for (int i = 0; i < listBook.Count; i++)
                {

                    if (listBook[i].Equals(deleteBookName))
                    {
                        File.Delete(deleteBookNameP);
                        flag = true;
                        Console.WriteLine("{0}删除成功!", deleteBookName);
                        break;
                    }
                }
            }else
            {
                Console.WriteLine("您输入的图书信息为空删除不存在");
            }
            
            if (!flag)
            {
                Console.WriteLine("您输入的{0}图书信息不存在,删除失败!", deleteBookName);
            }
        }
        //获取文件夹下的所有图书信息
        public List<string> getBookInfo()
        {
            List<string> listBook = new List<string>();
            //获取当前文件夹下的所有txt文件
            string[] bookNames = Directory.GetFiles("./图书");
            string bookName = "";
            foreach (string s in bookNames)
            {
                int lastIndex = s.LastIndexOf("\\") + 1;
                bookName = s.Substring(lastIndex, s.Length - lastIndex - 4);
                listBook.Add(bookName);
            }
            return listBook;
        }
        //查询具体的某种图书信息
        public void printBookInfo(List<string> listBook)
        {
            Console.WriteLine("请输入你要查询的图书名称");
            string bookName = Console.ReadLine();
            bool flag = false;
            string bookNameP = "./图书/" + bookName + ".txt";
            if (bookName.Length > 0)
            {
                for (int i = 0; i < listBook.Count; i++)
                {

                    if (listBook[i].Equals(bookName))
                    {
                        StreamReader sd = new StreamReader(bookNameP);
                        string content = sd.ReadToEnd();
                        sd.Close();
                        Console.WriteLine("查询的{0}详细信息:{1}", bookName, content);
                        flag = true;
                    }
                }
                if (!flag)
                {
                    Console.WriteLine("您输入的图书名称不存在,查询失败");
                }
            }
            else
            {
                Console.WriteLine("您没有输入图书名称,查询失败");
            }
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Book b = new Book();
            Console.WriteLine("*************欢迎来到图书管理系统******************");
            while (true)
            {
                Console.WriteLine("请输入你操作的命令:【1:新增图书】 【2:查询具体图书信息】【3:查询所有图书】 【4:删除图书】 【5:退出图书管理系统】");
                int order = Convert.ToInt32(Console.ReadLine());
                switch (order)
                {
                    case 1:
                        b.insertBook(b.getBookInfo());
                        break;
                    case 2:
                        b.printBookInfo(b.getBookInfo());
                        break;
                    case 3:
                        b.selectBook(b.getBookInfo());
                        break;
                    case 4:
                        b.deleteBook(b.getBookInfo());
                        break;
                    case 5:
                        return;
                    default:
                        Console.WriteLine("您输入的指令有误,请重新输入");
                        break;
                }
            }
        }
    }
}

二、lambda表达式(匿名函数)

image.png