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

C++ Programmer's Cookbook

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

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 夢在天涯 閱讀(573) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1815003
  • 排名 - 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>
              免费久久精品视频| 久久一区中文字幕| 国产欧美日韩精品丝袜高跟鞋 | 久久久不卡网国产精品一区| 亚洲女同精品视频| 欧美在线亚洲一区| 久久久久免费观看| 欧美成人久久| 欧美三级视频在线播放| 国产欧美日韩另类视频免费观看| 狠狠色狠狠色综合系列| 亚洲靠逼com| 午夜精品影院| 欧美激情1区2区| 中文欧美字幕免费| 久久久久久尹人网香蕉| 欧美黄在线观看| 国产欧美一区二区三区国产幕精品| 黄色亚洲大片免费在线观看| 亚洲精品乱码久久久久久按摩观| 亚洲视频播放| 欧美成人免费在线观看| 一本色道久久综合狠狠躁篇的优点 | 久久九九全国免费精品观看| 蜜臀99久久精品久久久久久软件| 亚洲国产视频直播| 午夜精品影院| 欧美区日韩区| 伊人蜜桃色噜噜激情综合| 亚洲午夜电影网| 欧美高清在线视频观看不卡| 亚洲性xxxx| 欧美日韩精品二区第二页| 国产日韩成人精品| 一区二区三区国产在线| 免费国产自线拍一欧美视频| 中文欧美日韩| 欧美日韩中文字幕| 亚洲成色www久久网站| 久久国产精品亚洲va麻豆| 亚洲精品久久久久久久久久久久| 久久成人免费电影| 国产麻豆综合| 亚洲视频一区二区| 亚洲欧洲在线看| 亚洲欧美日韩一区| 欧美va日韩va| 久久gogo国模啪啪人体图| 国产精品jizz在线观看美国| 日韩视频一区二区在线观看| 美女露胸一区二区三区| 午夜精品一区二区三区在线视| 国产精品久久福利| 亚洲天堂成人| 一本大道久久a久久综合婷婷| 欧美激情精品久久久| 亚洲三级电影全部在线观看高清| 欧美a级一区二区| 久久久久久国产精品一区| 国产一区二区三区在线观看精品 | 欧美国产精品| 久久精品女人的天堂av| 国内揄拍国内精品久久| 久久影音先锋| 久久躁日日躁aaaaxxxx| 在线观看亚洲精品视频| 久久亚洲图片| 蜜臀久久99精品久久久画质超高清| 亚洲第一搞黄网站| 亚洲国产第一页| 欧美精品一区在线| 一本色道88久久加勒比精品| 亚洲精品中文字幕在线观看| 欧美午夜在线视频| 羞羞答答国产精品www一本 | 久久成人免费视频| 在线观看亚洲a| 亚洲三级国产| 国产欧美日韩一区二区三区在线观看 | 亚洲在线观看视频| 在线亚洲免费视频| 国产一区二区三区免费在线观看| 久久综合久久综合这里只有精品 | 999亚洲国产精| 99在线观看免费视频精品观看| 国产精品久久久久久一区二区三区 | 亚洲国产精品久久久久| 欧美三级午夜理伦三级中视频| 翔田千里一区二区| 老司机精品导航| 一区二区三区欧美视频| 久久国产毛片| 欧美视频在线观看 亚洲欧| 亚洲欧美日韩爽爽影院| 久久精品欧美| 亚洲免费一在线| 久久精品综合网| 中文亚洲欧美| 久久一区二区三区四区| 亚洲一区视频| 蜜桃久久精品乱码一区二区| 亚洲午夜未删减在线观看| 久久九九99| 亚洲欧美日韩一区二区三区在线| 久久久久**毛片大全| 亚洲在线不卡| 欧美精品v日韩精品v国产精品 | 99视频精品全部免费在线| 国产一区二区丝袜高跟鞋图片 | 国内伊人久久久久久网站视频| 亚洲激情啪啪| 亚洲电影第1页| 亚洲欧美在线另类| 中文精品视频| 欧美精彩视频一区二区三区| 久久精品国产综合| 国产精品国产三级国产aⅴ无密码 国产精品国产三级国产aⅴ入口 | 国内一区二区三区| 亚洲夜晚福利在线观看| 99视频在线观看一区三区| 久久久久9999亚洲精品| 亚洲欧美卡通另类91av| 欧美女同在线视频| 欧美国产视频在线| 悠悠资源网久久精品| 小辣椒精品导航| 香蕉久久国产| 国产精品拍天天在线| 一区二区av| 亚洲综合导航| 国产精品婷婷午夜在线观看| 一区二区三区免费网站| av成人黄色| 欧美精品播放| 亚洲精品在线免费观看视频| 亚洲美洲欧洲综合国产一区| 美国三级日本三级久久99| 麻豆av福利av久久av| 亚洲国产经典视频| 美女久久一区| 亚洲国产合集| 在线亚洲伦理| 国产精品日韩一区二区| 亚洲网站在线播放| 欧美一区二区播放| 黄色成人在线免费| 乱中年女人伦av一区二区| 亚洲第一免费播放区| 亚洲精品中文字幕在线| 欧美日韩高清在线播放| 中文高清一区| 亚洲视频在线看| 久久福利精品| 巨乳诱惑日韩免费av| 激情欧美一区二区三区在线观看| 欧美自拍偷拍| 欧美好骚综合网| 亚洲最新视频在线| 国产精品久久久久免费a∨| 亚洲一区二区免费在线| 久久久久www| 亚洲精品视频一区| 欧美日韩在线综合| 久久国产乱子精品免费女 | 欧美一进一出视频| 蜜桃精品一区二区三区| 日韩网站在线观看| 国产精品视频yy9099| 久久久xxx| 夜夜嗨av色一区二区不卡| 久久精品道一区二区三区| 亚洲国产精品va| 国产精品毛片一区二区三区| 久久久久九九九九| 一区二区精品在线观看| 久久视频一区| 亚洲天堂偷拍| 在线观看日韩av电影| 欧美性猛交xxxx乱大交蜜桃| 欧美在线观看视频一区二区三区| 亚洲人成7777| 久热精品在线视频| 亚洲尤物在线| 日韩一区二区精品| 尤物网精品视频| 国产免费亚洲高清| 欧美第一黄网免费网站| 久久精品30| 亚洲资源在线观看| 亚洲美女中文字幕| 亚洲大片免费看| 麻豆91精品91久久久的内涵| 午夜在线视频一区二区区别| 亚洲精选大片| 伊人夜夜躁av伊人久久| 国产欧美综合一区二区三区| 欧美视频在线观看一区二区| 欧美大片在线观看一区二区| 久久综合狠狠综合久久激情| 欧美综合国产|