博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#读写文件的详细信息

Posted on 2011-01-10 12:16  roby  阅读(1788)  评论(0)    收藏  举报
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using DSOFile;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
      //注册
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = "regsvr32";
                p.StartInfo.Arguments = "DSOFile.dll";
                p.Start();
                p.WaitForExit();
                p.Close();
                p.Dispose();
      //写入
                string path = "c:/Book1.xlsx";
                DSOFile.OleDocumentProperties dso = new DSOFile.OleDocumentProperties();
                dso.Open(path, false, DSOFile.dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
                dso.SummaryProperties.Title = "This is the Title";
                dso.SummaryProperties.Subject = "This is the Subject";
                dso.SummaryProperties.Company = "RTDev";
                dso.SummaryProperties.Author = "Ron T.";
                dso.Save();
      //读取
                Console.WriteLine(dso.SummaryProperties.Title);
                Console.WriteLine(dso.SummaryProperties.Subject);
                Console.WriteLine(dso.SummaryProperties.Company);
                Console.WriteLine(dso.SummaryProperties.Author);
                dso.Close(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

需要将dsofile.dll文件复制到debug目录下,再在项目里引用。
dsofile.dll属于com组件,需要注册。