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

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>
              最新中文字幕亚洲| 久久久久久久一区| 亚洲一区网站| 亚洲一区二区三区久久| 亚洲免费观看视频| 欧美国产高清| 久久久国产一区二区| 国产精品综合色区在线观看| 99视频有精品| 亚洲欧洲一区二区天堂久久| 欧美国产日产韩国视频| 亚洲日本成人网| 91久久精品网| 欧美精品一区三区| 亚洲午夜小视频| 亚洲一区二区影院| 国产一区二区三区免费在线观看| 久久精品国产99| 久久精品久久99精品久久| 精品成人国产| 亚洲国产精品视频| 欧美性猛交视频| 久久疯狂做爰流白浆xx| 久久精品99国产精品| 亚洲国产高清一区| 日韩亚洲视频| 国产日本精品| 亚洲第一精品久久忘忧草社区| 欧美另类变人与禽xxxxx| 亚洲一区二区三区国产| 亚洲午夜av在线| 一区二区三区在线免费视频 | 亚洲一区二区三区午夜| 99re6热在线精品视频播放速度| 欧美日韩免费观看一区三区| 亚洲欧美日韩视频一区| 久久久之久亚州精品露出| 日韩亚洲精品在线| 欧美亚洲三级| 99精品视频免费| 午夜精品一区二区三区在线播放| 在线日韩中文字幕| 在线亚洲一区| 在线不卡亚洲| 一区二区三区三区在线| 在线观看亚洲一区| 中国成人在线视频| 一本一本久久a久久精品牛牛影视| 亚洲国产欧美日韩精品| 一区二区三区精品视频在线观看| 欧美成人国产| 欧美一区二区三区日韩视频| 国产精品美女主播| 亚洲欧美日本国产专区一区| 91久久一区二区| 欧美激情aaaa| 亚洲精品一品区二品区三品区| 免费高清在线视频一区·| 久久精品国产综合| 精品91在线| 麻豆精品一区二区综合av| 欧美在线观看天堂一区二区三区| 国产欧美一区二区精品性色| 亚洲欧美精品在线观看| 亚洲女同精品视频| 国产视频精品xxxx| 麻豆国产va免费精品高清在线| 久久精品综合网| 亚洲欧洲美洲综合色网| 亚洲国产视频直播| 国产精品国产福利国产秒拍| 欧美一区二区三区四区视频| 久久成人一区二区| 亚洲看片一区| 亚洲一区视频| 亚洲国产cao| 99国产精品| 国产精品午夜春色av| 久久久久久高潮国产精品视| 噜噜爱69成人精品| 亚洲图片欧美日产| 久久er精品视频| 亚洲精品视频在线播放| 一区二区成人精品 | 欧美一区1区三区3区公司| 99精品国产一区二区青青牛奶| 国产精品久久激情| 噜噜爱69成人精品| 欧美日韩在线亚洲一区蜜芽| 久久成年人视频| 欧美风情在线| 久久精品99| 欧美日韩一区在线视频| 久热精品视频在线免费观看| 欧美日韩在线播放一区二区| 美女诱惑一区| 国产欧美日韩亚州综合| 亚洲精品永久免费| 亚洲福利av| 欧美一区二区三区免费大片| 亚洲视频在线一区观看| 麻豆91精品| 久久精品国产亚洲一区二区三区| 欧美国产欧美综合| 亚洲电影欧美电影有声小说| 亚洲午夜精品一区二区| 久久九九精品99国产精品| 亚洲一区二区视频在线| 久久婷婷国产综合国色天香| 午夜精品久久久久久久白皮肤| 美玉足脚交一区二区三区图片| 欧美中日韩免费视频| 欧美视频日韩视频在线观看| 亚洲国产精品第一区二区三区| 国产一区二区三区观看| 亚洲小说欧美另类社区| 在线视频欧美精品| 欧美a级一区二区| 久久综合国产精品| 国产午夜精品一区二区三区视频 | 欧美诱惑福利视频| 亚洲综合色婷婷| 欧美人成免费网站| 亚洲高清自拍| 国内成人精品一区| 欧美在线不卡| 久久久精品欧美丰满| 国产女同一区二区| 性色av一区二区三区在线观看| 亚洲欧美日本在线| 国产精品免费看久久久香蕉| 亚洲色图自拍| 欧美与欧洲交xxxx免费观看| 国产欧美一区二区在线观看| 欧美亚洲尤物久久| 久久婷婷影院| 亚洲国产日韩一区二区| 麻豆久久婷婷| 亚洲精品一区久久久久久| 宅男精品视频| 国产精品久久久久久久浪潮网站 | 亚洲欧美一区二区三区久久| 亚洲女同同性videoxma| 欧美日韩专区| 亚洲性色视频| 久久久www免费人成黑人精品 | 欧美高清在线视频| 亚洲精品女人| 欧美视频一区二区三区在线观看| 亚洲免费观看高清在线观看 | 久久高清国产| 伊人久久亚洲美女图片| 欧美91精品| 9国产精品视频| 欧美在线免费视频| 在线观看亚洲| 欧美日韩国产综合视频在线观看 | 久久久水蜜桃av免费网站| 欧美 日韩 国产在线| 国产香蕉久久精品综合网| 欧美精品一区三区| 蜜月aⅴ免费一区二区三区| 久久国产精品网站| 久久久噜噜噜久久中文字幕色伊伊| 欧美激情五月| 午夜精品久久久久久久99热浪潮| 老司机午夜精品视频| 亚洲乱亚洲高清| 国产精品一区一区三区| 美日韩精品视频免费看| 亚洲一级黄色| 亚洲第一综合天堂另类专| 午夜免费电影一区在线观看 | 性18欧美另类| 亚洲国产高清一区| 国产精品网站在线| 欧美国产日本韩| 久久精品国产视频| 亚洲视频在线二区| 亚洲韩国精品一区| 毛片一区二区三区| 午夜视黄欧洲亚洲| 亚洲激情国产精品| 国产一区视频在线观看免费| 欧美日韩综合在线| 免播放器亚洲| 欧美影院成人| 亚洲无限av看| 亚洲精品亚洲人成人网| 欧美成人免费va影院高清| 久久激情综合| 欧美伊人久久大香线蕉综合69| 99精品福利视频| 亚洲国产欧美日韩精品| 国产一区二区三区奇米久涩| 国产精品久久久久久av下载红粉| 欧美成人精品高清在线播放| 午夜免费电影一区在线观看| 亚洲视频在线看| 亚洲精品一区二区三区不|