MVC07

1. 讲解ASP.net MVC的I/O操作

新建一个控制台程序,输入代码如下

using System;
using System.IO;

namespace IO
{
    class Program
    {
        static void Main(string[] args)
        {
            // 判断文件是否存在
            Console.WriteLine(File.Exists(@"C:UsersASUSDesktopmemoa.txt"));
            // 判断目录是否存在
            Console.WriteLine(Directory.Exists(@"C:"));
            // 下面例子将展示如何查找某一目录下所有exe文件的信息
            // . 表示当前目录
            string path = ".";
            if (args.Length > 0)
            {
                // 如果需要在其他目录执行则打开控制行,之后进入项目下的Debug目录,见下图
                path = args[0];
            }
            else
            {
                Console.WriteLine("Directory not found");
            }

            DirectoryInfo dir = new DirectoryInfo(path);
            foreach(FileInfo f in dir.GetFiles("*.exe"))
            {
                string name = f.Name;
                long size = f.Length;
                DateTime creationTime = f.CreationTime;
                Console.WriteLine(name);
                Console.WriteLine(size);
                Console.WriteLine(creationTime);
                Console.WriteLine("------------");
            }
        }
    }
}

File,Directory为一个静态的Class,无法实例化。

2.写入文件

using System;
using System.IO;

namespace IO
{
    class Program
    {
        private const string FILE_NAME = "a.txt";
        static void Main(string[] args)
        {
            if (File.Exists(FILE_NAME))
            {
                Console.WriteLine("already exists.");
                return;
            }

            FileStream fs = new FileStream(FILE_NAME, FileMode.Create);
            BinaryWriter w = new BinaryWriter(fs);

            for(int i = 0; i < 10; i++)
            {
                w.Write("a");
            }
            w.Close();
            fs.Close();
        }
    }
}

 

如果文件已存在,我们需要覆盖内容到里面怎么办?

using System;
using System.IO;

namespace IO
{
    class Program
    {
        private const string FILE_NAME = "a.txt";
        static void Main(string[] args)
        {
            using(StreamWriter w = File.AppendText("test.txt"))
            {
                Log("Hi,is me.", w);
                Log("how are u", w);
                w.Close();
            }
           
        }
        // 方法,用于写入数据
        public static void Log(string logMessage,TextWriter w)
        {
            w.Write("
Log Entry");
            w.WriteLine(":{0}", logMessage);
            w.Flush();

        }
    }
}

using 内代码执行完毕后会自动释放资源,常用于读写文件以及连接数据库

2.读取文件

using System;
using System.IO;

namespace IO
{
    class Program
    {
        private const string FILE_NAME = "a.txt";
        static void Main(string[] args)
        {
            if (!File.Exists(FILE_NAME))
            {
                Console.WriteLine("{0} does not exist!",FILE_NAME);
                return;
            }
            // 路径,操作类别,权限
            FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(fs);
       // 读取前5个字符
for(int i = 0; i < 5; i++) { Console.WriteLine(r.ReadString()); } r.Close(); fs.Close(); } } }

完整读取某一文件:

using System;
using System.IO;

namespace IO
{
    class Program
    {
        private const string FILE_NAME = "test.txt";
        static void Main(string[] args)
        {
            if (!File.Exists(FILE_NAME))
            {
                Console.WriteLine("{0} does not exist!",FILE_NAME);
                return;
            }
            using(StreamReader sr = File.OpenText(FILE_NAME))
            {
                string input;
                while((input = sr.ReadLine())!=null)
                {
                    Console.WriteLine(input);
                }
                Console.WriteLine("ended");
                sr.Close();
            }
        }

    }
}
原文地址:https://www.cnblogs.com/Tanqurey/p/12252858.html