1. 程式人生 > >C 之FileInfo的簡單操作

C 之FileInfo的簡單操作

                     

和DirectoryInfo一樣,FileInfo類同樣是很方便的就能實現複雜的功能。 如下,我們一起來看一下一個簡單的小例子吧。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace ConsoleApplication1{    class Program    {        static void Main()        {            String temppath = Path.GetTempFileName();            try
{                FileInfo fileInfo = new FileInfo(temppath);                if (!fileInfo.Exists)                {                    using (StreamWriter writer = fileInfo.CreateText())                    {                        writer.WriteLine("Line 1!");                        writer.WriteLine("Linw 2!!"
);                    }                    //需要注意的是,打開了一個流就要及時的關閉。同樣可以在using程式碼塊中完成這一動作,而且效果更好!                    fileInfo.CopyTo("F:\\MyCSharpTest");                   // fileInfo.Delete();                }                }catch(Exception e){                    Console.WriteLine(e.Message);                }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36