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

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>
              欧美 日韩 国产 一区| 欧美视频精品在线观看| 亚洲日本精品国产第一区| 久久久久青草大香线综合精品| 香蕉久久夜色| 欧美在线一级va免费观看| 亚洲欧美日韩国产| 欧美在线免费视频| 欧美成人dvd在线视频| 亚洲日本精品国产第一区| 99在线视频精品| 午夜精品国产更新| 老司机免费视频一区二区三区| 欧美黑人国产人伦爽爽爽| 国产精品成人观看视频国产奇米| 国产综合网站| 亚洲精品美女91| 午夜精品亚洲一区二区三区嫩草| 久久久久久亚洲精品中文字幕| 欧美jjzz| 亚洲欧美久久久久一区二区三区| 久久精品网址| 欧美色中文字幕| 精品成人免费| 亚洲无线视频| 免费亚洲一区二区| 中国女人久久久| 麻豆乱码国产一区二区三区| 欧美视频精品一区| 亚洲国产天堂网精品网站| 午夜精品av| 亚洲人在线视频| 久久久99国产精品免费| 国产精品家庭影院| 亚洲激情视频网| 久久久久久久激情视频| 99国产一区二区三精品乱码| 久久久av毛片精品| 国产欧美精品一区二区三区介绍| 亚洲欧洲日本一区二区三区| 欧美一级欧美一级在线播放| 最新成人在线| 美女诱惑黄网站一区| 国产午夜精品一区二区三区欧美 | 国产人成精品一区二区三| 在线欧美福利| 久久精品一区二区三区中文字幕| 亚洲人体偷拍| 欧美xxxx在线观看| 在线观看亚洲视频| 欧美一区二区三区四区在线观看| 亚洲精品视频在线看| 欧美国产日本| 日韩特黄影片| 亚洲精品欧美极品| 欧美伦理a级免费电影| 亚洲乱码国产乱码精品精可以看| 免费永久网站黄欧美| 久久乐国产精品| 亚洲第一视频| 免费人成网站在线观看欧美高清| 久久国产精品久久w女人spa| 国产农村妇女精品一区二区| 午夜精品福利在线| 亚洲午夜一区| 国产农村妇女精品| 久久久www免费人成黑人精品 | 亚洲手机成人高清视频| 亚洲精品乱码久久久久久蜜桃91| 葵司免费一区二区三区四区五区| 一区视频在线播放| 免费av成人在线| 久久亚洲精品伦理| 亚洲日本中文字幕免费在线不卡| 亚洲黄色成人| 欧美午夜不卡在线观看免费 | 国产精品久久久久久亚洲毛片 | 亚洲人成人99网站| 欧美日韩国产色视频| 亚洲图片欧美一区| 亚洲欧美国产精品桃花| 国产综合视频在线观看| 欧美大片免费观看| 欧美日韩国产综合网| 性做久久久久久久久| 久久久精品动漫| 一本色道久久精品| 午夜视频在线观看一区| 91久久精品美女高潮| 亚洲图片欧美日产| 在线国产亚洲欧美| 亚洲裸体在线观看| 国产亚洲观看| 亚洲欧洲一区二区三区久久| 国产精品免费观看在线| 欧美69wwwcom| 国产精品亚洲综合久久| 亚洲成人在线网| 午夜久久美女| 欧美电影免费观看网站| 欧美一区二区三区免费观看| 久久精品日产第一区二区| 在线亚洲免费视频| 久久精品中文| 午夜精品一区二区三区在线播放 | 久久精品一区二区三区不卡| 蜜臀久久久99精品久久久久久| 亚洲在线观看视频| 欧美va天堂| 久久综合久久美利坚合众国| 欧美午夜理伦三级在线观看| 欧美成人午夜免费视在线看片| 国产精品亚洲аv天堂网| 91久久久久久久久| 在线免费精品视频| 欧美一区二区三区四区视频 | 亚洲婷婷综合色高清在线| 久久亚洲午夜电影| 久久精品30| 欧美午夜电影完整版| 亚洲国产片色| 在线电影国产精品| 欧美中文日韩| 久久精品久久综合| 国产精品久久久一区二区三区| 亚洲福利视频专区| 一色屋精品亚洲香蕉网站| 午夜在线成人av| 亚洲欧美bt| 国产精品久久久久毛片软件| 日韩午夜激情av| 亚洲卡通欧美制服中文| 久久亚洲私人国产精品va| 久热re这里精品视频在线6| 国产一区二区三区日韩| 亚洲欧洲av一区二区三区久久| 午夜精品99久久免费| 国产精品亚洲综合久久| 午夜欧美电影在线观看| 久久久xxx| 亚洲国产精品va在线看黑人| 美女精品一区| 亚洲精品免费观看| 亚洲一区二区三区777| 欧美午夜不卡视频| 亚洲一区www| 久久国产一区二区| 国内伊人久久久久久网站视频| 欧美一区二区三区免费视频| 久久久亚洲高清| 亚洲国产日韩欧美在线99| 欧美国产日本| 亚洲视频在线观看免费| 午夜精品影院| 激情久久五月天| 欧美精品大片| 亚洲一二三区视频在线观看| 久久成人久久爱| 在线视频国产日韩| 欧美黄色网络| 亚洲综合电影| 欧美国产激情二区三区| 亚洲国产成人porn| 欧美三区在线视频| 午夜久久福利| 亚洲第一狼人社区| 亚洲淫片在线视频| 国语自产精品视频在线看抢先版结局| 久久激情视频| 亚洲精品一区二区三区四区高清| 亚洲欧美在线另类| 在线免费精品视频| 国产精品国产三级国产普通话蜜臀 | 狠狠色狠狠色综合系列| 欧美激情视频网站| 午夜综合激情| 亚洲日本精品国产第一区| 欧美专区福利在线| 一本色道久久综合| 禁久久精品乱码| 国产精品xxxxx| 女女同性精品视频| 欧美亚洲三级| 一本久道久久久| 欧美成人高清视频| 欧美在线观看视频一区二区三区| 亚洲毛片av| 狠狠色噜噜狠狠狠狠色吗综合| 欧美人成在线视频| 久久久久久久久久久久久9999| 中文av一区特黄| 91久久精品国产91久久| 久久性色av| 久久精品九九| 午夜日韩在线观看| 亚洲免费视频网站| 亚洲视频在线观看网站| 一本色道久久综合亚洲精品不卡 | 久久综合网络一区二区| 亚洲免费视频一区二区|