码迷,mamicode.com
首页 > Windows程序 > 详细

C#的IO操作

时间:2015-05-06 08:01:29      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        private const string FILE_NAME = "Test.txt";
        static void Main(string[] args)
        {
            //Console.WriteLine( File.Exists(@"C:\Hello\IO.txt"));//文件是否存在
            //Console.WriteLine(Directory.Exists(@"C:\"));//路径是否存在

            //string path = ".";//当前路径
            //if (args.Length > 0)
            //{
            //    if (Directory.Exists(args[0]))
            //    {
            //        path = args[0];
            //    }
            //    else
            //    {
            //        Console.WriteLine("{0}not found;using current directory;",args[0]);
            //    }
            //}
            //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("{0,-12:N0}{1,-20:g}{2}",size,creationTime,name);
            //}

            /* 例1 
             * if (File.Exists(FILE_NAME))
              {
                  Console.WriteLine("{0}already exists!",FILE_NAME);
                  Console.ReadLine();
                  return;
              }
              FileStream fs = new FileStream(FILE_NAME, FileMode.Create);
              BinaryWriter w = new BinaryWriter(fs);
              for (int i = 0; i <= 11; i++)
              {
                  w.Write("a");
              }
              w.Close();
              fs.Close();*/
            //using (StreamWriter w = File.AppendText("test.txt"))
            //{
            //    Log("hehe", w);
            //    Log("Hello jikexueyuan",w);
            //    w.Close();

            //}
            if (!File.Exists(FILE_NAME))
            {
                Console.WriteLine("{0}does not exist!", FILE_NAME);
                Console.ReadLine();
                return;
            }
            //FileStream fs = new FileStream(FILE_NAME,FileMode.Open,FileAccess.Read );
            //BinaryReader r = new BinaryReader(fs);
            //for (int i = 0; i < 11; i++)
            //{
            //    Console.WriteLine(r.ReadString());
            //}
            //r.Close();
            //fs.Close();
            using (StreamReader sr = File.OpenText(FILE_NAME)) 
            {
                string input;
                while ((input = sr.ReadLine()) != null)
                {
                    Console.WriteLine(input);
                }
                Console.WriteLine("The end of the stream");
                sr.Close();
            }
            Console.ReadKey();
        }
        //public static void Log(string logMessage, TextWriter w)
        //{
        //    w.Write("\r\nLog Entry:");
        //    w.WriteLine(":{0}",logMessage);
        //    w.Flush();
        //}
    }
}

 

C#的IO操作

标签:

原文地址:http://www.cnblogs.com/heisaijuzhen/p/4480819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!