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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級} {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 夢在天涯 閱讀(575) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811981
  • 排名 - 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>
              亚洲欧美在线aaa| 欧美a级理论片| 亚洲综合色婷婷| 久久激情综合| 免费成人黄色片| 国产日韩在线一区| 亚洲视频一区| 男女av一区三区二区色多| 一区二区三区免费看| 男男成人高潮片免费网站| 亚洲精品一区二区三区福利| 久久精品99国产精品| 一本大道久久a久久精二百| 亚洲欧美日韩直播| 欧美激情亚洲| 久久精品理论片| 国产日产亚洲精品系列| 99精品热视频| 亚洲国产成人在线播放| 91久久精品一区二区三区| 久久精品网址| 欧美黄免费看| 日韩视频一区二区三区| 免费影视亚洲| 久久久久久网| 国语自产精品视频在线看| 亚洲影院一区| 99国产精品久久久| 国产一区三区三区| 久久精品首页| 欧美日韩国产另类不卡| 一本久久综合亚洲鲁鲁| 亚洲欧洲精品天堂一级| 欧美a级片网站| 欧美在线3区| 欧美中文日韩| 国产一区再线| 日韩系列欧美系列| 国产精品视频yy9299一区| 欧美在线关看| 欧美日产国产成人免费图片| 久久精品日产第一区二区| 欧美激情精品久久久久久蜜臀| 欧美专区一区二区三区| 久久本道综合色狠狠五月| 亚洲视频免费在线| 欧美激情国产日韩| 欧美成人精品激情在线观看| 国产乱码精品一区二区三区不卡| 欧美一区二区福利在线| 欧美日韩成人一区二区| 欧美v日韩v国产v| 国产欧美日本| 午夜精品影院| 亚洲欧洲在线看| 欧美一区二区三区免费观看 | 亚洲三级影片| 国产精品国产三级国产专播精品人| 亚洲自拍偷拍福利| 欧美四级在线观看| 久久躁狠狠躁夜夜爽| 欧美激情一区二区三区在线视频观看 | 国产亚洲二区| 久久午夜激情| 黑人中文字幕一区二区三区| 午夜激情亚洲| 久久精品99| 好吊一区二区三区| 欧美专区亚洲专区| 久久久久久91香蕉国产| 欧美精品久久99| 亚洲欧洲免费视频| 欧美日韩大片| 99精品欧美一区二区蜜桃免费| 在线视频日本亚洲性| 欧美中文在线观看国产| 久久久免费精品| 国产精品video| 欧美成人免费va影院高清| 国产精品大片| 亚洲主播在线观看| 久久久综合精品| 亚洲国产天堂网精品网站| 亚洲欧美网站| 免费一区视频| 亚洲一级片在线看| 欧美成人精品激情在线观看| 久久成人免费网| 在线观看中文字幕不卡| 欧美一区=区| 欧美成人午夜77777| 一区二区高清在线| 国产精品麻豆成人av电影艾秋| 亚洲国产成人在线视频| 亚洲一区二区精品| 国产婷婷色一区二区三区| 久久av一区二区三区亚洲| 亚洲第一天堂无码专区| 亚洲一区二区成人| 亚洲高清不卡一区| 久久一日本道色综合久久| 久久国产直播| 亚洲欧洲一区二区三区| 国产精品人成在线观看免费| 久久久久国产精品厨房| 91久久久久久久久| 久久久欧美精品| 亚洲图片自拍偷拍| 伊人精品在线| 欧美电影在线播放| 欧美一区二区三区四区在线观看| 欧美国产日本| 久久精品一区二区三区中文字幕| 亚洲精品中文字幕在线| 国内精品免费午夜毛片| 欧美日韩一区二区三区在线视频 | 亚洲一区免费视频| 亚洲国产成人av在线| 国产热re99久久6国产精品| 欧美大香线蕉线伊人久久国产精品| 亚洲欧美精品一区| 夜夜嗨av一区二区三区网页| 欧美电影资源| 美女久久一区| 久久国产精品网站| 新狼窝色av性久久久久久| 国产一区二区在线观看免费播放| 欧美另类变人与禽xxxxx| 亚洲一区二区三区激情| 久久久精品一区| 午夜精品一区二区三区电影天堂| 国产免费亚洲高清| 欧美视频官网| 欧美日韩另类综合| 欧美日韩国产bt| 欧美激情综合在线| 欧美啪啪一区| 欧美国产丝袜视频| 模特精品裸拍一区| 免费人成网站在线观看欧美高清| 久久久另类综合| 久久久精品国产一区二区三区 | 欧美激情欧美激情在线五月| 久久久久久久久伊人| 久久精品亚洲精品| 久久精品一本久久99精品| 久久精品国产精品亚洲综合 | 亚洲另类在线视频| 亚洲精选成人| 中文有码久久| 午夜精品视频在线观看| 欧美专区在线| 欧美xxx成人| 亚洲欧洲三级电影| 99re热这里只有精品视频| 一本到12不卡视频在线dvd| 9l国产精品久久久久麻豆| 亚洲网站视频| 日韩亚洲不卡在线| 亚洲视频你懂的| 日韩西西人体444www| 亚洲视频网站在线观看| 亚洲欧美国产77777| 久久福利电影| 欧美国产亚洲另类动漫| 国产精品久久久久久久久久ktv | 国产日韩欧美视频在线| 国产综合精品一区| 亚洲第一网站| 中文在线资源观看网站视频免费不卡 | 欧美日韩黄色大片| 国产精品日韩在线播放| 精品成人一区二区| 国产亚洲欧美一级| 亚洲高清久久久| 亚洲一区二区三区在线观看视频| 欧美在线高清| 最新热久久免费视频| 欧美一区二区播放| 美国十次了思思久久精品导航| 欧美日韩激情网| 在线观看日韩专区| 亚洲永久免费av| 男人的天堂成人在线| 一区二区三区成人精品| 久久一综合视频| 国产欧美日韩麻豆91| 亚洲看片免费| 老司机久久99久久精品播放免费 | 亚洲免费观看高清在线观看 | 免费在线成人av| 国产色综合天天综合网| 99v久久综合狠狠综合久久| 久久精品网址| 亚洲一卡久久| 欧美区日韩区| 91久久国产综合久久| 久久久久成人精品| 亚洲宅男天堂在线观看无病毒| 欧美bbbxxxxx|