笔记7

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;

class Program
{
      
static void Main(string[] args)
      {
            
// Create a Store for the current user
            IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForAssembly();

            IsolatedStorageFileStream userStream 
= new IsolatedStorageFileStream("UserSettings.set", FileMode.Create, userStore);
            StreamWriter userWriter 
= new StreamWriter(userStream);
            userWriter.WriteLine(
"User Prefs");
            userWriter.Close();

            Console.Read();
      }

}

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;

class Program
{
  
static void Main(string[] args)
  {
    
// Create a Store for the current user
    IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForAssembly();

    IsolatedStorageFileStream userStream 
= new IsolatedStorageFileStream("UserSettings.set", FileMode.Create, userStore);
    StreamWriter userWriter 
= new StreamWriter(userStream);
    userWriter.WriteLine(
"User Prefs");
    userWriter.Close();

    
// Check to see if the file exists
    string[] files = userStore.GetFileNames("UserSettings.set");
    
if (files.Length == 0)
    {
      Console.WriteLine(
"No data saved for this user");
    }
    
else
    {
      
// Read the data from the Store
      userStream = new IsolatedStorageFileStream("UserSettings.set", FileMode.Open, userStore);
      StreamReader userReader 
= new StreamReader(userStream);
      
string contents = userReader.ReadToEnd();

      Console.WriteLine(contents);
    }

    Console.Read();
  }

}

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace CS
{
  
class Program
  {
    
static void Main(string[] args)
    {
        CompressFile(
@"c:\boot.ini"@"c:\boot.ini.gz");//boot.ini是系统的隐藏文件
        DecompressFile(@"c:\boot.ini.gz"@"c:\boot.ini.txt");

        Console.ReadKey();
    }

    
static void CompressFile(string inFilename, 
                             
string outFilename)
    {

          FileStream sourceFile 
= File.OpenRead(inFilename);
          FileStream destFile 
= File.Create(outFilename);

          
// Create the Compressed stream
          GZipStream compStream =
            
new GZipStream(destFile, CompressionMode.Compress);

          
// Write the data
          int theByte = sourceFile.ReadByte();
          
while (theByte != -1)
          {
            compStream.WriteByte((
byte)theByte);
            theByte 
= sourceFile.ReadByte();
          }

          
// Clean it up
          sourceFile.Close();
          compStream.Close();
          destFile.Close();
    }

    
static void DecompressFile(string inFilename, string outFilename)
    {
          FileStream sourceFile 
= File.OpenRead(inFilename);
          FileStream destFile 
= File.Create(outFilename);

          
// Create the Compressed stream
          GZipStream compStream =
            
new GZipStream(sourceFile, CompressionMode.Decompress);

          
// Write the data
          int theByte = compStream.ReadByte();
          
while (theByte != -1)
          {
            destFile.WriteByte((
byte)theByte);
            theByte 
= compStream.ReadByte();
          }

          
// Clean it up
          sourceFile.Close();
          compStream.Close();
          destFile.Close();
    }
  }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CS
{
  
class Program
  {
    
static void Main(string[] args)
    {
      DirectoryInfo dir 
= new DirectoryInfo(Environment.SystemDirectory);
      DriveInfo[] drives 
= DriveInfo.GetDrives();

      ShowDirectory(dir);
      ShowDirve(drives);

      Console.ReadKey();
  }

    
static void ShowDirectory(DirectoryInfo dir)
    {
      
// Show Each File
      foreach (FileInfo file in dir.GetFiles())
      {
        Console.WriteLine(
"File: {0}", file.FullName);
      }

      
////递归显示子目录和子目录下的文件(Go through sub-directories )
      //foreach (DirectoryInfo subDir in dir.GetDirectories())
      
//{
      
//  ShowDirectory(subDir);
      
//}

    }
      
static void ShowDirve(DriveInfo[] drives)
      {
          
// Show Each File
          foreach (DriveInfo drive in drives)
          {
              
//IsReady 指示驱动器是否已准备好。例如,它指示 CD 是否在 CD 驱动器中,或者可移动存储设备是否已准备好执行读/写操作。如果您未测试驱动器是否已准备好,而它没有准备好,则使用 DriveInfo 查询驱动器会引发 IOException。
              if (drive.IsReady)
              {
                  
string strDriveInfo;
                  strDriveInfo 
= string.Format("Drive:{0} Type:{1} 总空间容量:{2} 剩余空间总容量:{3} 可用空间容量:{4} 文件系统名称:{5} 驱动器卷标:{6}", drive.Name, drive.DriveType, drive.TotalSize.ToString(), drive.TotalFreeSpace.ToString(), drive.AvailableFreeSpace,drive.DriveFormat,drive.VolumeLabel);
                  
//strDriveInfo = string.Format("Drive:{0} | Type:{1}", drive.Name, drive.DriveType);
                  Console.WriteLine(strDriveInfo);
              }
              
else
              {
                  
string strDriveInfo;
                  strDriveInfo 
= string.Format("Drive:{0} Type:{1}", drive.Name, drive.DriveType);
                  Console.WriteLine(strDriveInfo);
              }
          }
      }
  }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CS
{
  
class Program
  {
    
static void Main(string[] args)
    {
      FileSystemWatcher watcher 
=
        
new FileSystemWatcher(Environment.SystemDirectory);

      
// Watch all .ini files for
      
// changes to size or attributes only
      
// but in every subdirectory
      watcher.Filter = "*.ini";
      watcher.IncludeSubdirectories 
= true;
      watcher.NotifyFilter 
=
        NotifyFilters.Attributes 
| NotifyFilters.Size|NotifyFilters.;

      
// Register for event
      watcher.Changed +=
        
new FileSystemEventHandler(watcher_Changed);

      
// Start the Events
      watcher.EnableRaisingEvents = true;

      
// Read for a keypress to exit the app
      Console.ReadKey();
    }

    
static void watcher_Changed(object sender,
      FileSystemEventArgs e)
    {
      Console.WriteLine(
"Changed: {0}", e.FullPath);
    }
  }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CS
{
  
class Program
  {
    
static void Main(string[] args)
    {
      StreamWriter writer 
= File.CreateText(@"c:\newfile.txt");
      writer.WriteLine(
"This is my new file");
      writer.WriteLine(
"Do you like its format?");
      writer.Close();

      StreamReader reader 
= File.OpenText(@"c:\newfile.txt");
      
string contents = reader.ReadToEnd();
      reader.Close();
      Console.WriteLine(contents);
    }
  }
}
posted @ 2008-04-18 13:57  李涛  阅读(194)  评论(0)    收藏  举报