青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

Working with Files in C#(轉(zhuǎn))

In this article, you will learn how to manipulate directories and files in your system. Further, we will discuss how to read from and write to a file by using the powerful .NET classes.

The Namespace

System.IO provides all the necessary classes, methods, and properties for manipulating directories and files. Table 1 elaborates the main classes under this namespace.

Class Purpose/Use
Binary Reader and Writer Read and write primitive data types
Directory, File, DirectoryInfo, and FileInfo Create, delete, and move files and directories. Get specific information about the files by making use of the properties defined in these classes.
FileStream Access the files in a random fashion
MemoryStream Access data stored in memory
StreamWriter and StreamReader Read and write textual information
StringReader and StringWriter Read and write textual Information from a string buffer

Table 1—Classes under System.IO

Working with DirectoryInfo and FileInfo classes

The base class of DirectoryInfo and FileInfo is FileSystemInfo. This is an abstract class, meaning you can't instantiate this class. But you can use the properties defined by this class. Table 2 elaborates its properties and methods.

Properties Purpose/Use
Attributes Returns attributes associated with a file. Takes FileAttributes enumeration values
CreationTime Returns the time of creation of the file
Exists Checks whether a supplied file is a directory or not
Extension Returns the file extension
LastAccessTime Returns last accessed time of the file
FullName Returns the full path of the file
LastWriteTime Returns the time of last written activity to the file
Name Returns the name of a given file
Delete() Deletes a file. Be careful when using this method.

Table 2—Members of FileSystemInfo class

The DirectoryInfo class provides methods for creating, moving, and deleting directories. To make use of the above properties, create an object of the DirectoryInfo class as shown in Listing 1:

Listing 1

DirectoryInfo dir1 = new DirectoryInfo(@"F:\WINNT");

You then can access the properties by using the object dir1, as shown in the code fragment in Listing 2:

Listing 2

Console.WriteLine("Full Name is : {0}", dir1.FullName);
Console.WriteLine("Attributes are : {0}",
                   dir1.Attributes.ToString());

You can also apply the values of FileAttributes enumeration. Its values are shown in Table 3.

Properties Purpose/Use
Archive Returns the file's Archive status
Compressed Returns whether the file is compressed or not
Directory Returns whether the file is a directory or not
Encrypted Returns whether the file is encrypted or not
Hidden Returns whether the file is hidden or not
Offline Signifies that the data is not available
ReadOnly Indicates that the file is read only
System Indicates that the file is a System file (probably a file under the Windows folder)

Table 3—FileAttributes Enumeration Values

Working with Files under a Directory

Suppose that you want to list all BMP files under the f:\Pictures directory. You can write a code as shown in the code snippet given in Listing 3:

Listing 3

DirectoryInfo dir = new DirectoryInfo(@"F:\WINNT");
FileInfo[] bmpfiles = dir.GetFiles("*.bmp);
Console.WriteLine("Total number of bmp files", bmpfiles.Length);
Foreach( FileInfo f in bmpfiles)
{
  Console.WriteLine("Name is : {0}", f.Name);
  Console.WriteLine("Length of the file is : {0}", f.Length);
  Console.WriteLine("Creation time is : {0}", f.CreationTime);
  Console.WriteLine("Attributes of the file are : {0}",
                     f.Attributes.ToString());
}

Creating Subdirectories

You can easily create a subdirectory. Listing fragment 4 describes how to create a subdirectory called MySub under the Sub directory.

Listing 4

DirectoryInfo dir = new DirectoryInfo(@"F:\WINNT");
try
{
  dir.CreateSubdirectory("Sub");
  dir.CreateSubdirectory(@"Sub\MySub");
}
catch(IOException e)
{
  Console.WriteLine(e.Message);
}

Creating Files by Using the FileInfo Class

With the FileInfo class, you can create new files, access information about the files, delete, and move files. This class also provides methods for opening, reading from, and writing to a file. Listing 5 shows how to create a text file and access its information like its creation time, full name, and so forth.

Listing 5

FileInfo fi = new FileInfo(@"F:\Myprogram.txt");
FileStream fstr = fi.Create();
Console.WriteLine("Creation Time: {0}",f.CreationTime);
Console.WriteLine("Full Name: {0}",f.FullName);
Console.WriteLine("FileAttributes: {0}",f.Attributes.ToString());

//Way to delete Myprogram.txt file.

Console.WriteLine("Press any key to delete the file");
Console.Read();
fstr.Close();
fi.Delete();

Understanding the Open() Method

The FileInfo class defines a method named Open() with which you can create files by applying the values of the FileMode and FileAccess enumerations. The code snippet in Listing 6 describes its usage:

Listing 6

FileInfo f = new FileInfo("c:\myfile.txt");
FileStream s = f.Open(FileMode.OpenorWrite, FileAccess.Read);

You then can read from and write to a file by using the object 's'. In the overloaded Open() method, permission is given only for reading from a file. If you want to write to a file, you have to apply the ReadWrite value of FileAccess enumeration. Tables 4 and 5 describe the values of the FileMode and FileAccess enumerations.

Values Purpose/Use
Append Opens the file and adds data. This should be used with the FileAccess Write Enumeration value.
Create Creates a new file. Overwrites any existing file.
CreateNew Creates a new file. If the file already exists, IOException is thrown.
Open Opens an existing file
OpenOrCreate Opens a new file. If there is no file, it creates a new file.
Truncate Truncates an existing file

Table 4—FileMode Enumeration values

Values Purpose/Use
Read Data can be read (retrieved) from the file
ReadWrite Data can be added to and retrieved from the file
Write Data can be added to the file

Table 5—FileAccess Enumeration values

Writing to a Text File by Using the StreamWriter Class

You can easily write texts or other information to a file by using the CreateText() method of the FileInfo class. However, you have to obtain a valid StreamWriter. It's this StreamWriter reference that provides the required functionalities for writing to a file. To illustrate, Listing 7 writes a series of texts to the Mytext.txt file.

Listing 7

FileInfo f = new FileInfo("Mytext.txt")
StreamWriter w = f.CreateText();
w.WriteLine("This is from");
w.WriteLine("Chapter 6");
w.WriteLine("Of C# Module");
w.Write(w.NewLine);
w.WriteLine("Thanks for your time");
w.Close();

Reading from a Text File

You can read from a Text file by using the StreamReader class. For this, you have to specify the file name using the static OpenText() method of the File class. Listing 8 reads the contents that we have written in Listing 7:

Listing 8

Console.WriteLine("Reading the contents from the file");
StreamReader s = File.OpenText("Mytext.txt");
string read = null;
while ((read = s.ReadLine()) != null)
{
  Console.WriteLine(read);
}
s.Close();

------------------------------------------------------------------------------------------------------------------------

About the Author

Anand Narayanaswamy works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, Software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials.

posted on 2005-11-15 12:47 夢(mèng)在天涯 閱讀(582) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

  • 隨筆 - 461
  • 文章 - 4
  • 評(píng)論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816420
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              欧美激情一区二区在线| 女主播福利一区| 国产精品成人一区二区网站软件| 亚洲精品视频在线播放| 亚洲国产91色在线| 免费不卡中文字幕视频| 亚洲精品字幕| 日韩午夜激情av| 国产精品久久久久久久久搜平片| 亚洲欧美日韩一区在线| 亚洲影院免费观看| 国产亚洲欧美色| 欧美大片免费久久精品三p| 六月婷婷一区| 亚洲一本大道在线| 性久久久久久久久久久久| 一区二区视频免费在线观看 | 国产精品中文字幕欧美| 久久国产直播| 欧美成人综合一区| 亚洲专区国产精品| 久久另类ts人妖一区二区| 日韩午夜av电影| 午夜久久电影网| 亚洲人妖在线| 亚洲欧美激情视频| 亚洲精品在线看| 亚洲一区免费观看| 亚洲国产日本| 亚洲尤物精选| 亚洲国产精彩中文乱码av在线播放| 最新高清无码专区| 国产一区日韩二区欧美三区| 亚洲国产激情| 好吊色欧美一区二区三区四区| 亚洲国产欧美一区二区三区久久 | 国产精品日本欧美一区二区三区| 久久婷婷国产综合国色天香| 欧美日韩另类国产亚洲欧美一级| 久久久www成人免费精品| 欧美精品国产一区二区| 久久婷婷综合激情| 国产精品自拍一区| 日韩性生活视频| 亚洲电影免费观看高清完整版在线观看| 中日韩视频在线观看| 亚洲精品国产欧美| 欧美在线一级视频| 午夜精品理论片| 欧美日韩成人激情| 亚洲第一精品电影| 狠狠入ady亚洲精品经典电影| 日韩亚洲欧美成人一区| 亚洲破处大片| 久久深夜福利| 久久艳片www.17c.com| 国产精品日韩欧美| 国产精品99久久久久久宅男| 日韩西西人体444www| 久久久天天操| 久久综合色播五月| 国产一区二区久久久| 亚洲一区二区黄| 亚洲一线二线三线久久久| 欧美日韩国产在线播放| 亚洲国产天堂网精品网站| 亚洲高清视频一区| 久久尤物电影视频在线观看| 久久夜色精品国产噜噜av| 狠狠色伊人亚洲综合成人| 性色av一区二区三区在线观看| 欧美亚洲视频一区二区| 国产伦精品一区二区三区高清版| 一区二区三区日韩精品| 亚洲欧美制服中文字幕| 国产精品永久免费视频| 欧美一区二区在线| 久久尤物视频| 亚洲高清免费在线| 欧美电影打屁股sp| 亚洲黄色成人久久久| 一区二区欧美在线| 国产精品激情| 午夜一区二区三视频在线观看| 欧美一区二区在线| 在线观看亚洲a| 欧美第一黄色网| 一区二区三区导航| 久久久久久夜精品精品免费| 伊人久久亚洲影院| 欧美韩日视频| 亚洲字幕在线观看| 麻豆9191精品国产| 艳女tv在线观看国产一区| 欧美日韩一区二区免费在线观看 | 亚洲精品美女在线观看播放| 99精品免费视频| 国产精一区二区三区| 久久久精品视频成人| 亚洲国产欧美久久| 香蕉av777xxx色综合一区| 亚洲成人在线| 欧美色综合网| 久久精品国产亚洲一区二区三区| 欧美国产三区| 久久av一区二区三区| 亚洲国产精品视频| 国产精品一卡二卡| 蜜臀av性久久久久蜜臀aⅴ四虎| 日韩午夜激情av| 猫咪成人在线观看| 亚洲欧美一区在线| 亚洲高清免费在线| 国产欧美日本一区二区三区| 快she精品国产999| 亚洲欧美日韩精品久久久| 亚洲国产精品久久久久婷婷老年| 亚洲一区欧美| 亚洲欧洲日本一区二区三区| 国产欧美精品一区二区色综合| 欧美mv日韩mv国产网站app| 午夜精品久久久久久99热| 日韩视频―中文字幕| 你懂的网址国产 欧美| 午夜精品在线视频| 9久re热视频在线精品| 在线成人性视频| 国产精品综合色区在线观看| 欧美日本不卡视频| 美女视频网站黄色亚洲| 久久精品国产综合精品| 亚洲欧美国产高清| 一本久久a久久精品亚洲| 亚洲国产精品专区久久| 蜜桃av一区二区三区| 久久久国产亚洲精品| 欧美一区二区黄| 亚洲一区二区欧美| 亚洲私人影院| 日韩亚洲欧美一区二区三区| 亚洲国产日韩美| 伊人蜜桃色噜噜激情综合| 韩日欧美一区二区| 韩国女主播一区| 国产亚洲精品久久久久久| 国产欧美日本一区二区三区| 国产精品欧美一区二区三区奶水 | 久久久一区二区| 欧美有码视频| 久久爱另类一区二区小说| 欧美在线观看视频在线| 午夜欧美精品| 久久精品免费播放| 久久久午夜视频| 久色婷婷小香蕉久久| 免费成人性网站| 欧美大片免费观看| 欧美日韩mv| 国产精品扒开腿爽爽爽视频| 国产精品毛片高清在线完整版| 国产精品v欧美精品v日韩 | 久久久av水蜜桃| 久久综合色婷婷| 欧美激情四色 | 久久国产日本精品| 久久久精品五月天| 欧美高清视频一区二区| 欧美日韩免费一区| 国产麻豆午夜三级精品| 国产原创一区二区| 亚洲激情亚洲| 亚洲午夜精品视频| 久久激情五月丁香伊人| 免费日本视频一区| 亚洲精品激情| 亚洲在线免费视频| 久久这里只精品最新地址| 欧美日韩国产页| 国产欧美精品| 亚洲裸体俱乐部裸体舞表演av| 亚洲一区黄色| 美女主播一区| 99re6这里只有精品| 欧美一区二区三区男人的天堂| 久久一区国产| 国产精品久久久久影院亚瑟| 精品白丝av| 午夜精品久久久久99热蜜桃导演| 噜噜噜久久亚洲精品国产品小说| 亚洲欧洲日本专区| 欧美在线国产精品| 欧美日韩三级在线| 激情久久影院| 亚洲欧美在线观看| 亚洲国产婷婷| 久久久一区二区| 国产精品一区二区在线观看不卡| 亚洲欧洲精品一区二区| 久久精品一区二区三区中文字幕| 亚洲麻豆国产自偷在线|