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

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

Working with Files in C#(轉)

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 夢在天涯 閱讀(588) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1818572
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              午夜视频精品| 麻豆精品精品国产自在97香蕉| 亚洲二区免费| 国内精品视频一区| 国产一区二区在线观看免费| 国产在线播放一区二区三区| 欧美一级淫片aaaaaaa视频| 久久大逼视频| 欧美激情一区二区三区在线视频 | 欧美大片在线影院| 欧美日韩免费一区| 国产一区二区精品久久99| 欧美制服丝袜第一页| 欧美一区二区三区在线免费观看| 欧美电影在线免费观看网站| 亚洲国产欧美一区| 久久国产婷婷国产香蕉| 最近看过的日韩成人| 亚洲综合精品自拍| 毛片一区二区| 亚洲视频香蕉人妖| 久久亚洲精品伦理| 99精品欧美一区| 久久久久国产一区二区| 国产精品盗摄一区二区三区| 亚洲国产精品黑人久久久| 欧美在线免费观看| 久久五月天婷婷| 亚洲第一天堂av| 久久久久88色偷偷免费| 亚洲永久精品国产| 欧美午夜在线视频| 亚洲天堂偷拍| 欧美专区亚洲专区| 国产午夜精品美女毛片视频| 小黄鸭精品密入口导航| 久久伊伊香蕉| 91久久中文字幕| 免费成人av| 久久国产一区二区三区| 国产一区二区按摩在线观看| 欧美激情1区2区3区| 国产农村妇女精品| 久久激情视频久久| 欧美日韩国产精品成人| 一区二区日韩免费看| 亚洲国产一二三| 欧美国产日韩亚洲一区| 99riav1国产精品视频| 亚洲国产精彩中文乱码av在线播放| 国产精品jvid在线观看蜜臀| 亚洲国产精品999| 伊人精品成人久久综合软件| 免费在线观看一区二区| 欧美一级视频一区二区| 宅男精品导航| 欧美女同在线视频| 亚洲一区视频在线| 一区二区三区免费网站| 国产精品丝袜xxxxxxx| 久久一二三四| 国产精品亚洲综合色区韩国| 99亚洲伊人久久精品影院红桃| 亚洲精品日本| 亚洲天堂av在线免费| 激情综合色丁香一区二区| 91久久黄色| 亚洲精品乱码久久久久久| 牛牛影视久久网| 午夜精品久久久久久99热| 久久精品99国产精品| 久久精品一区二区三区四区| 国产精品伦一区| 久久久久一区二区三区| 欧美精品一区在线播放| 亚洲国产精品久久久久婷婷884| 亚洲电影天堂av| 卡通动漫国产精品| 亚洲缚视频在线观看| 亚洲精品你懂的| 欧美日韩免费区域视频在线观看| 日韩一区二区精品| 国产一区二区视频在线观看 | 久久综合网色—综合色88| 欧美国产免费| 亚洲国产精品久久久久秋霞蜜臀| 日韩亚洲欧美中文三级| 欧美三级网址| 亚洲国产小视频在线观看| 一区二区国产精品| 国产精品美女久久久久久免费| 亚洲欧洲av一区二区三区久久| 亚洲精品久久久久久久久久久久久 | 正在播放亚洲一区| 欧美午夜宅男影院在线观看| 亚洲先锋成人| 久久久久久久网站| 一本大道久久精品懂色aⅴ| 久久婷婷国产综合国色天香| 亚洲二区在线视频| 亚洲免费视频中文字幕| 欧美激情区在线播放| 久久一区二区视频| 亚洲日本中文字幕区| 国产精品成人一区二区三区夜夜夜 | 午夜影院日韩| 亚洲国产精品久久久久婷婷老年| 欧美理论在线播放| 欧美在线啊v| 亚洲精品社区| 亚洲免费激情| 欧美激情一二三区| 亚洲欧美在线一区| 亚洲国产日韩综合一区| 亚洲欧美网站| 亚洲精品一区二区三区婷婷月 | 国产欧美在线观看| 欧美精品久久一区| 久久久国产精品一区| 玖玖综合伊人| 亚洲欧美日韩在线观看a三区| 亚洲高清二区| 国产亚洲午夜| 国产精品入口| 欧美日韩免费观看一区| 老司机aⅴ在线精品导航| 午夜国产一区| 一区二区日韩免费看| 亚洲国产欧美在线| 免费观看日韩av| 久久久久久综合| 性18欧美另类| 亚洲自拍偷拍一区| 一区二区三区国产精华| 亚洲日本成人女熟在线观看| 国内久久婷婷综合| 国产日韩一区在线| 国产精品卡一卡二卡三| 欧美日韩午夜剧场| 欧美欧美午夜aⅴ在线观看| 欧美阿v一级看视频| 麻豆9191精品国产| 久久久青草婷婷精品综合日韩| 小黄鸭精品aⅴ导航网站入口| 中文国产成人精品| 免费在线看一区| 免费日本视频一区| 欧美成人乱码一区二区三区| 一区二区欧美日韩| 99在线热播精品免费99热| 9l视频自拍蝌蚪9l视频成人| 亚洲欧洲日产国码二区| 亚洲精品少妇| 一区二区三区色| 亚洲欧美一区在线| 久久成人一区| 美女视频黄免费的久久| 农村妇女精品| 亚洲精品日韩在线| 中国日韩欧美久久久久久久久| 亚洲系列中文字幕| 久久国产精品毛片| 老鸭窝毛片一区二区三区| 欧美高清视频一区二区| 欧美日本不卡视频| 国产精品人人做人人爽| 国产亚洲精品自拍| 亚洲国产精品一区二区三区| 亚洲精选大片| 亚洲欧美日韩中文播放| 久久久久综合网| 欧美激情无毛| 正在播放亚洲一区| 久久精品99国产精品日本| 欧美激情精品久久久久| 国产精品女主播| 在线观看日韩欧美| 亚洲调教视频在线观看| 久久视频在线免费观看| 亚洲精品欧美激情| 久久国产视频网站| 欧美日韩免费在线观看| 国产一级精品aaaaa看| 亚洲精品视频在线看| 久久av资源网站| 亚洲欧洲一级| 久久精品人人做人人爽电影蜜月| 你懂的一区二区| 国产日产欧美精品| 99精品久久久| 噜噜噜在线观看免费视频日韩| 99精品福利视频| 免费成人性网站| 国产丝袜一区二区三区| 在线一区免费观看| 欧美国产精品日韩| 欧美怡红院视频| 国产精品v欧美精品v日本精品动漫| 在线成人小视频| 久久国产精品第一页|