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

C++ Programmer's Cookbook

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

Accessing Files and Directories

 

Welcome to the next installment of the .NET Nuts & Bolts column. In this column we'll explore interacting with files from within .NET. The topics covered will include how to get the properties on files in a directory. It will involve using classes in the System.IO namespace.

Working with Streams

No, I haven't turned this into an article about the great outdoors. Streams have their place with computers as well, although I wouldn't recommend getting your computer near one of the traditional kind. Streams are a concept that have been around for a while, but that are new to Microsoft developers via .NET. A stream is a base class used to abstract the specifics of input and output from the underlying device(s). In general, streams support the ability to read or write. Some streams provide additional capabilities such as seek, which allows navigation forward to a specific location. The device could be a physical file, memory, or the network. List of classes that inherit from the Stream base class are as follows:

  • FileStream—read, write, open, and close files
  • MemoryStream—read and write managed memory
  • NetworkStream—read and write between network connections (System.Net namespace)
  • CryptoStream—read and write data through cryptographic transformations
  • BufferedStream—adds buffering to another stream that does not inherently support buffering

While the streams are used to abstract the input and output from the device, the stream itself is not directly used to read and write data. Instead, a reader or writer object is used to interact with the stream and perform the physical read and write. Here is a list of classes used for reading and writing to streams:

  • BinaryReader and BinaryWriter—read and write binary data to streams
  • StreamReader and StreamWriter—read and write characters from streams
  • StringReader and StringWriter—read and write characters from Strings
  • TextReader and TextWriter—read and write Unicode text from streams

Reading and Writing Text

The following section will use StreamWriter, StreamReader, and FileStream to write text to a file and then read and display the entire contents of the file.

Sample Code to Write and Read a File

using System;
using System.IO;

namespace CodeGuru.FileOperations
{
  /// <remarks>
  /// Sample to demonstrate writing and reading a file.
  /// </remarks>
  class WriteReadFile
  {
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
     FileStream fileStream = null;
     StreamReader reader = null;
     StreamWriter writer = null;

     try
     {
      // Create or open the file
      fileStream = new FileStream("c:\\mylog.txt",
         FileMode.OpenOrCreate,
         FileAccess.Write);
      writer = new StreamWriter(fileStream);

      // Set the file pointer to the end of the file
      writer.BaseStream.Seek(0, SeekOrigin.End);

      // Force the write to the underlying file and close
      writer.WriteLine(
          System.DateTime.Now.ToString() + " - Hello World!");
      writer.Flush();
      writer.Close();

      // Read and display the contents of the file one
      // line at a time.
      String fileLine;
      reader = new StreamReader("c:\\mylog.txt");
      while( (fileLine = reader.ReadLine()) != null )
      {
        Console.WriteLine(fileLine);
      }
     }
     finally
     {
      // Make sure we cleanup after ourselves
      if( writer != null ) writer.Close();
      if( reader != null ) reader.Close();
     }
   }
  }
}

Working with Directories

There two classes for the manipulation of directories. The classes are named Directory and the DirectoryInfo. The Directory class provides static methods for directory manipulation. The DirectoryInfo class provides instance methods for directory manipulation. They provide the same features and functionality, so the choice comes down to whether you need an instance of an object or not. The members include, but are not limited to the following:

  • Create—create a directory
  • Delete—delete a directory
  • GetDirectories—return subdirectories of the current directory
  • MoveTo—move a directory to a new location

Sample Code to Produce a List of All Directories

The following sample code demonstrates the ability to produce a list of directories using recursion. A recursive procedure is one that calls itself. You must ensure that your procedure does not call itself indefinitely; otherwise, you'll eventually run out of memory. In this case, there are a finite number of subdirectories, so there is automatically a termination point.

using System;
using System.IO;

namespace CodeGuru.FileOperations
{
  /// <remarks>
  /// Sample to demonstrate reading the contents of directories.
  /// </remarks>
  class ReadDirectory
  {
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
     DirectoryInfo dirInfo = new DirectoryInfo("c:\\");
     Console.WriteLine("Root: {0}", dirInfo.Name);
     ReadDirectory.ProduceListing(dirInfo, "  ");
     Console.ReadLine();
   }

   /*
    * Recursively produce a list of files
    */
   private static void ProduceListing(DirectoryInfo dirInfo,
                                      string Spacer)
   {
     Console.WriteLine(Spacer + "{0}", dirInfo.Name);
     foreach(DirectoryInfo subDir in dirInfo.GetDirectories())
     {
      Console.WriteLine(Spacer + Spacer + "{0}", subDir.Name);
      if( subDir.GetDirectories().Length > 0 )
      {
        ProduceListing(subDir, Spacer + "  ");
      }
     }
   }
  }
}

Getting File Properties for Office Documents

Microsoft has an ActiveX component that can be used to programmatically retrieve the summary properties (title, subject, etc.) for files such as Excel, Word, and PowerPoint. It has advantages because it does not use Office Automation so Microsoft Office does not have to be installed. This component can be used to produce a listing of files and their properties.

Sample Code to Produce File Listing with Properties

The following code will populate a DataTable with a list of files and their properties. The DataTable could be bound to a DataGrid or another display control as desired. Be sure you've added the appropriate reference to the dsofile.dll that exposes the file properties. Because this is a COM-based DLL, COM Interop will be used to interact with the DLL.

// Setup the data table
DataTable fileTable = new DataTable("Files");
DataRow fileRow;
fileTable.Columns.Add("Name");
fileTable.Columns.Add("Title");
fileTable.Columns.Add("Subject");
fileTable.Columns.Add("Description");

// Open the directory
DirectoryInfo docDir = new DirectoryInfo("C:\\My Documents\\");
if( !docDir.Exists )
{
  docDir.Create();
}

// Add the document info into the table
DSOleFile.PropertyReader propReader =
       new DSOleFile.PropertyReaderClass();
DSOleFile.DocumentProperties docProps;

foreach(FileInfo file in docDir.GetFiles())
{
  try
  {
   fileRow = fileTable.NewRow();
   docProps = propReader.GetDocumentProperties(file.FullName);
   fileRow["Name"] = file.Name;
   fileRow["Title"] = docProps.Title;
   fileRow["Subject"] = docProps.Subject;
   fileRow["Description"] = docProps.Comments;
   fileTable.Rows.Add(fileRow);
  }
  catch( Exception exception )
  {
   Console.WriteLine("Error occurred: " + exception.Message);
  }
}
propReader = null;
this.DocumentGrid.DataSource = fileTable;
this.DocumentGrid.DataBind();

// Force cleanup so dsofile doesn't keep files locked open
GC.Collect();

Summary

You now have seen several cursory ways in which the System.IO namespace can be used to interact with files and directories. We took an additional look to see how to use the dsofile additional DLL from Microsoft to show the properties for Microsoft Office documents.

posted on 2005-11-23 12:24 夢(mèng)在天涯 閱讀(572) 評(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

搜索

  •  

積分與排名

  • 積分 - 1812969
  • 排名 - 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>
              欧美~级网站不卡| 欧美视频日韩视频| 久久成人综合网| 亚洲第一免费播放区| 欧美成人免费va影院高清| 亚洲欧洲在线观看| 欧美日本一区| 亚洲欧美一区二区三区在线| 亚洲无人区一区| 一区二区视频免费在线观看 | 99日韩精品| 国产精品夜色7777狼人 | 久久国产手机看片| 亚洲国产欧美不卡在线观看 | 国产精品久久久久久久久搜平片 | 一区二区三区四区蜜桃| 国产精品福利在线观看网址| 久久精品亚洲热| 欧美日韩伊人| 亚洲激情另类| 亚洲成人在线| 亚洲一区二区三区精品视频| 在线成人av| 欧美一区二区三区四区在线观看地址 | 久久精品国产精品 | 午夜精品久久久| 欧美激情二区三区| 欧美国产日韩在线| 欧美理论大片| 午夜精品视频在线观看| 欧美精品一区二区三区一线天视频 | 快播亚洲色图| 在线电影一区| 榴莲视频成人在线观看| 快射av在线播放一区| 韩国成人福利片在线播放| 亚洲欧美色婷婷| 午夜影院日韩| 精品成人国产| 欧美成人免费在线| 亚洲全部视频| 亚洲自拍偷拍色片视频| 国产精品国产馆在线真实露脸| 99精品国产在热久久下载| 日韩午夜激情| 国产一区二三区| 久久综合中文色婷婷| 亚洲黄色免费网站| 亚洲欧美国产毛片在线| 国产精品日韩精品欧美精品| aa国产精品| 免费试看一区| 亚洲在线视频观看| 激情婷婷久久| 国产精品二区二区三区| 老司机午夜精品视频在线观看| 亚洲国产视频直播| 久久精品国产精品| 一本色道久久| 一色屋精品亚洲香蕉网站| 国产精品porn| 欧美成人高清视频| 久久久精品欧美丰满| 欧美xart系列高清| 亚洲电影av在线| 国产麻豆精品theporn| 欧美成人精精品一区二区频| 亚洲精品孕妇| 伊人久久亚洲美女图片| 国产精品99免费看| 欧美日韩国产色视频| 亚洲韩日在线| 日韩视频欧美视频| 夜夜夜精品看看| 欧美日韩二区三区| 免费成人黄色片| 91久久综合| 欧美国产免费| 久久亚洲综合| 国产精品专区h在线观看| 亚洲国产精品一区二区第一页| 久久riav二区三区| 欧美欧美全黄| 久久在线免费观看视频| 日韩视频免费看| 国产一区二区欧美| 欧美中文字幕在线播放| 在线日韩一区二区| 欧美精品国产| 亚洲曰本av电影| 亚洲在线网站| 国产麻豆精品久久一二三| 亚洲欧美日韩在线观看a三区| 久久久精彩视频| 亚洲人成毛片在线播放| 欧美日韩免费区域视频在线观看| 一区二区免费在线播放| 久久久久久欧美| 9色精品在线| 国产一区二区三区最好精华液| 麻豆91精品91久久久的内涵| 日韩视频免费观看| 久久亚洲不卡| 亚洲视频中文字幕| 国产亚洲精品久久飘花| 欧美激情按摩| 性色av香蕉一区二区| 亚洲日本成人| 久久综合给合久久狠狠色| 一区二区三区四区五区精品视频| 韩国福利一区| 国产精品日韩高清| 欧美日韩国产高清| 久久久久综合网| 亚洲综合成人婷婷小说| 91久久精品国产91性色| 久久夜色精品一区| 欧美一级精品大片| 一区二区三区高清视频在线观看 | 日韩亚洲一区二区| 欧美大片免费| 久久久久久久久久看片| 亚洲欧美日韩另类| 99综合在线| 91久久久久久| 亚洲国产精品一区制服丝袜| 国产欧美亚洲一区| 欧美午夜精品久久久久久超碰| 欧美成人午夜77777| 久久午夜国产精品| 久久精品日产第一区二区| 亚洲欧美一区二区精品久久久| 亚洲精选大片| 日韩一区二区久久| 亚洲日本电影在线| 亚洲日本中文字幕| 亚洲欧洲一区二区三区在线观看| 欧美成人精品h版在线观看| 老司机精品导航| 麻豆精品视频| 久久综合色天天久久综合图片| 久久久精品性| 狂野欧美激情性xxxx| 美女日韩在线中文字幕| 欧美成人免费网| 亚洲国产成人高清精品| 亚洲国产91色在线| 最新成人av网站| 99国产精品国产精品久久 | 亚洲国产精品va在看黑人| 欧美大片一区| 亚洲黄色成人| 一二三区精品| 亚洲欧美制服另类日韩| 欧美在线亚洲在线| 老巨人导航500精品| 欧美第一黄色网| 欧美特黄一级大片| 国产欧美一区二区精品仙草咪| 国内精品亚洲| 亚洲精品久久久久久一区二区| 毛片一区二区三区| 一本大道久久a久久综合婷婷| 9久草视频在线视频精品| 亚洲神马久久| 久久精品视频在线| 欧美黄色成人网| 国产精品国产亚洲精品看不卡15| 国产精品夜夜夜| 亚洲国产一区二区三区在线播| 99国产精品99久久久久久粉嫩| 亚洲欧美中文另类| 欧美69wwwcom| 亚洲视频图片小说| 久久久久网站| 国产精品久久一级| 韩国三级电影一区二区| 一区二区三区黄色| 久久久青草婷婷精品综合日韩 | 亚洲欧洲精品一区二区| 亚洲欧美国产一区二区三区| 久久精品中文字幕一区二区三区| 欧美激情a∨在线视频播放| 国产日韩欧美麻豆| 日韩午夜一区| 榴莲视频成人在线观看| 亚洲视频电影图片偷拍一区| 理论片一区二区在线| 国产精品亚洲成人| 99re6这里只有精品| 久久一区二区三区国产精品| 一本一本a久久| 男女精品网站| 激情亚洲一区二区三区四区| 亚洲欧美日韩天堂一区二区| 亚洲国产日韩欧美一区二区三区| 亚洲欧洲av一区二区| 欧美视频在线观看一区二区| 91久久综合亚洲鲁鲁五月天| 久久综合成人精品亚洲另类欧美|