顯示具有 檔案 標籤的文章。 顯示所有文章
顯示具有 檔案 標籤的文章。 顯示所有文章

2013年8月21日 星期三

寫入XML檔案

寫入XML檔案

using System;
using System.IO;
using System.Xml;

public class Sample
{
  private const string filename = "sampledata.xml";

  public static void Main()
  {

     XmlTextWriter writer = new XmlTextWriter (filename, null);
     //Use indenting for readability.
     writer.Formatting = Formatting.Indented;

     writer.WriteComment("sample XML fragment");

     //Write an element (this one is the root).
     writer.WriteStartElement("bookstore");

     //Write the namespace declaration.
     writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

     writer.WriteStartElement("book");

     //Lookup the prefix and then write the ISBN attribute.
     string prefix = writer.LookupPrefix("urn:samples");
     writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
     writer.WriteString("1-861003-78");
     writer.WriteEndAttribute();     

     //Write the title.
     writer.WriteStartElement("title");
     writer.WriteString("The Handmaid's Tale");
     writer.WriteEndElement();

     //Write the price.
     writer.WriteElementString("price", "19.95");

     //Write the style element.
     writer.WriteStartElement(prefix, "style", "urn:samples");
     writer.WriteString("hardcover");
     writer.WriteEndElement();

     //Write the end tag for the book element.
     writer.WriteEndElement();

     //Write the close tag for the root element.
     writer.WriteEndElement();

     //Write the XML to file and close the writer.
     writer.Flush();
     writer.Close();

     //Read the file back in and parse to ensure well formed XML.
     XmlDocument doc = new XmlDocument();
     //Preserve white space for readability.
     doc.PreserveWhitespace = true;
     //Load the file
     doc.Load(filename);

     //Write the XML content to the console.
     Console.Write(doc.InnerXml);  

  }

}

另一種寫入XML檔案的方法

程式會在C槽下,產生檔案myXmlFile.xml
using System;
using System.Xml;

namespace ReadingXML2
{
    class Class1
    {
        static void Main(string[] args)
        {
            // Create a new file in C: dir
            XmlTextWriter textWriter = new XmlTextWriter("C:myXmFile.xml", null);
            textWriter.Formatting = Formatting.Indented;
            textWriter.Indentation = 4;
            // Opens the document
            textWriter.WriteStartDocument();
            // Write comments
            textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
            textWriter.WriteComment("myXmlFile.xml in root dir");
            // Write first element
            textWriter.WriteStartElement("Student");
            textWriter.WriteStartElement("r", "RECORD", "urn:record");
            // Write next element
            textWriter.WriteStartElement("Name", "");
            textWriter.WriteString("Student");
            textWriter.WriteEndElement();
            // Write one more element
            textWriter.WriteStartElement("Address", ""); textWriter.WriteString("Colony");
            textWriter.WriteEndElement();
            // WriteChars
            char[] ch = new char[3];
            ch[0] = 'a';
            ch[1] = 'r';
            ch[2] = 'c';
            textWriter.WriteStartElement("Char");
            textWriter.WriteChars(ch, 0, ch.Length);
            textWriter.WriteEndElement();
            // Ends the document.
            textWriter.WriteEndDocument();
            // close writer
            textWriter.Close();
        }
    }
}

2012年9月6日 星期四

寫入文字檔案

using System.IO;
private void WriteText(String filePath,String text)
{
 FileStream fs=null ;
 StreamWriter writer = null;
 try
 {
  if( File.Exists(filePath))
  {
       fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
  }
  else
  {
       fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  }
  writer = new StreamWriter(fs);
  writer.WriteLine(text);
  writer.Flush();
  fs.Flush();
 }
 catch (Exception Ex)
 {
      Console.WriteLine(Ex.Message);
 }
 finally
 {
      if(fs!=null) fs.Close();
 }
}


2010年12月31日 星期五

開啟Zip壓縮檔並讀取

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

using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.Zip.Compression;

namespace ICSharpCode.SharpZipLib.Zip
{

/// 
/// This class represents a Zip archive.  You can ask for the contained
/// entries, or get an input stream for a file entry.  The entry is
/// automatically decompressed.
///
/// This class is thread safe:  You can open input streams for arbitrary
/// entries in different threads.
/// 

/// 
Author of the original java version : Jochen Hoenicke
/// 
/// 
/// 
/// using System;
/// using System.Text;
/// using System.Collections;
/// using System.IO;
///
/// using ICSharpCode.SharpZipLib.Zip;
///
/// class MainClass
/// {
///         static public void Main(string[] args)
///         {
///                 ZipFile zFile = new ZipFile(args[0]);
///                 Console.WriteLine("Listing of : " + zFile.Name);
///                 Console.WriteLine("");
///                 Console.WriteLine("Raw Size    Size      Date     Time     Name");
///                 Console.WriteLine("--------  --------  --------  ------  ---------");
///                 foreach (ZipEntry e in zFile) {
///                         DateTime d = e.DateTime;
///                         Console.WriteLine("{0, -10}{1, -10}{2}  {3}   {4}", e.Size, e.CompressedSize,
///                                                                             d.ToString("dd-MM-yy"), d.ToString("t"),
///                                                                             e.Name);
///                 }
///         }
/// }
/// 
/// 
public class ZipFile : IEnumerable
{
string     name;
string     comment;
Stream     baseStream;
ZipEntry[] entries;

/// 
/// Opens a Zip file with the given name for reading.
/// 
/// 
/// An i/o error occurs
/// 
/// 
/// The file doesn't contain a valid zip archive.
/// 
public ZipFile(string name) : this(File.OpenRead(name))
{
}

/// 
/// Opens a Zip file reading the given FileStream
/// 
/// 
/// An i/o error occurs.
/// 
/// 
/// The file doesn't contain a valid zip archive.
/// 
public ZipFile(FileStream file)
{
this.baseStream  = file;
this.name = file.Name;
ReadEntries();
}

/// 
/// Opens a Zip file reading the given Stream
/// 
/// 
/// An i/o error occurs
/// 
/// 
/// The file doesn't contain a valid zip archive.

/// The stream provided cannot seek
/// 
public ZipFile(Stream baseStream)
{
this.baseStream  = baseStream;
this.name = null;
ReadEntries();
}

/// 
/// Read an unsigned short in little endian byte order.
/// 
/// 
/// An i/o error occurs.
/// 
/// 
/// The file ends prematurely
/// 
int ReadLeShort()
{
return baseStream.ReadByte() | baseStream.ReadByte() << 8;
}

/// 
/// Read an int in little endian byte order.
/// 
/// 
/// An i/o error occurs.
/// 
/// 
/// The file ends prematurely
/// 
int ReadLeInt()
{
return ReadLeShort() | ReadLeShort() << 16;
}

/// 
/// Search for and read the central directory of a zip file filling the entries
/// array.  This is called exactly once by the constructors.
/// 
/// 
/// An i/o error occurs.
/// 
/// 
/// The central directory is malformed or cannot be found
/// 
void ReadEntries()
{
// Search for the End Of Central Directory.  When a zip comment is
// present the directory may start earlier.
//
// TODO: The search is limited to 64K which is the maximum size of a trailing comment field to aid speed.
// This should be compatible with both SFX and ZIP files but has only been tested for Zip files
// Need to confirm this is valid in all cases.
// Could also speed this up by reading memory in larger blocks?

if (baseStream.CanSeek == false) {
throw new ZipException("ZipFile stream must be seekable");
}

long pos = baseStream.Length - ZipConstants.ENDHDR;
if (pos <= 0) {
throw new ZipException("File is too small to be a Zip file");
}

long giveUpMarker = Math.Max(pos - 0x10000, 0);

do {
if (pos < giveUpMarker) {
throw new ZipException("central directory not found, probably not a zip file");
}
baseStream.Seek(pos--, SeekOrigin.Begin);
} while (ReadLeInt() != ZipConstants.ENDSIG);

int thisDiskNumber            = ReadLeShort();
int startCentralDirDisk       = ReadLeShort();
int entriesForThisDisk        = ReadLeShort();
int entriesForWholeCentralDir = ReadLeShort();
int centralDirSize            = ReadLeInt();
int offsetOfCentralDir        = ReadLeInt();
int commentSize               = ReadLeShort();

byte[] zipComment = new byte[commentSize];
baseStream.Read(zipComment, 0, zipComment.Length);
comment = ZipConstants.ConvertToString(zipComment);

/* Its seems possible that this is too strict, more digging required.
if (thisDiskNumber != 0 || startCentralDirDisk != 0 || entriesForThisDisk != entriesForWholeCentralDir) {
throw new ZipException("Spanned archives are not currently handled");
}
*/

entries = new ZipEntry[entriesForWholeCentralDir];
baseStream.Seek(offsetOfCentralDir, SeekOrigin.Begin);

for (int i = 0; i < entriesForWholeCentralDir; i++) {                                 if (ReadLeInt() != ZipConstants.CENSIG) {                                         throw new ZipException("Wrong Central Directory signature");                                 }                                 int versionMadeBy      = ReadLeShort();                                 int versionToExtract   = ReadLeShort();                                 int bitFlags           = ReadLeShort();                                 int method             = ReadLeShort();                                 int dostime            = ReadLeInt();                                 int crc                = ReadLeInt();                                 int csize              = ReadLeInt();                                 int size               = ReadLeInt();                                 int nameLen            = ReadLeShort();                                 int extraLen           = ReadLeShort();                                 int commentLen         = ReadLeShort();                                 int diskStartNo        = ReadLeShort();  // Not currently used                                 int internalAttributes = ReadLeShort();  // Not currently used                                 int externalAttributes = ReadLeInt();                                 int offset             = ReadLeInt();                                 byte[] buffer = new byte[Math.Max(nameLen, commentLen)];                                 baseStream.Read(buffer, 0, nameLen);                                 string name = ZipConstants.ConvertToString(buffer, nameLen);                                 ZipEntry entry = new ZipEntry(name, versionToExtract, versionMadeBy);                                 entry.CompressionMethod = (CompressionMethod)method;                                 entry.Crc = crc & 0xffffffffL;                                 entry.Size = size & 0xffffffffL;                                 entry.CompressedSize = csize & 0xffffffffL;                                 entry.DosTime = (uint)dostime;                                 if (extraLen > 0) {
byte[] extra = new byte[extraLen];
baseStream.Read(extra, 0, extraLen);
entry.ExtraData = extra;
}

if (commentLen > 0) {
baseStream.Read(buffer, 0, commentLen);
entry.Comment = ZipConstants.ConvertToString(buffer, commentLen);
}

entry.ZipFileIndex           = i;
entry.Offset                 = offset;
entry.ExternalFileAttributes = externalAttributes;

entries[i] = entry;
}
}

/// 
/// Closes the ZipFile.  This also closes all input streams managed by
/// this class.  Once closed, no further instance methods should be
/// called.
/// 
/// 
/// An i/o error occurs.
/// 
public void Close()
{
entries = null;
lock(baseStream) {
baseStream.Close();
}
}

/// 
/// Returns an enumerator for the Zip entries in this Zip file.
/// 
/// 
/// The Zip file has been closed.
/// 
public IEnumerator GetEnumerator()
{
if (entries == null) {
throw new InvalidOperationException("ZipFile has closed");
}

return new ZipEntryEnumeration(entries);
}

/// 
/// Return the index of the entry with a matching name
/// 
///
Entry name to find
///
If true the comparison is case insensitive
/// The index position of the matching entry or -1 if not found
/// 
/// The Zip file has been closed.
/// 
public int FindEntry(string name, bool ignoreCase)
{
if (entries == null) {
throw new InvalidOperationException("ZipFile has been closed");
}

for (int i = 0; i < entries.Length; i++) {
if (string.Compare(name, entries[i].Name, ignoreCase) == 0) {
return i;
}
}
return -1;
}

/// 
/// Indexer property for ZipEntries
/// 
[System.Runtime.CompilerServices.IndexerNameAttribute("EntryByIndex")]
public ZipEntry this[int index] {
get {
return (ZipEntry) entries[index].Clone();
}
}

/// 
/// Searches for a zip entry in this archive with the given name.
/// String comparisons are case insensitive
/// 
///

/// The name to find. May contain directory components separated by slashes ('/').
/// 
/// 
/// The zip entry, or null if no entry with that name exists.
/// 
/// 
/// The Zip file has been closed.
/// 
public ZipEntry GetEntry(string name)
{
if (entries == null) {
throw new InvalidOperationException("ZipFile has been closed");
}

int index = FindEntry(name, true);
return index >= 0 ? (ZipEntry) entries[index].Clone() : null;
}

/// 
/// Checks, if the local header of the entry at index i matches the
/// central directory, and returns the offset to the data.
/// 
/// 
/// The start offset of the (compressed) data.
/// 
/// 
/// The stream ends prematurely
/// 
/// 
/// The local header signature is invalid, the entry and central header file name lengths are different
/// or the local and entry compression methods dont match
/// 
long CheckLocalHeader(ZipEntry entry)
{
lock(baseStream) {
baseStream.Seek(entry.Offset, SeekOrigin.Begin);
if (ReadLeInt() != ZipConstants.LOCSIG) {
throw new ZipException("Wrong Local header signature");
}

short shortValue = (short)ReadLeShort();     // version required to extract
if (shortValue > ZipConstants.VERSION_MADE_BY) {
throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", shortValue));
}

shortValue = (short)ReadLeShort();                  // general purpose bit flags.
if ((shortValue & 0x30) != 0) {
throw new ZipException("The library doesnt support the zip version required to extract this entry");
}

if (entry.CompressionMethod != (CompressionMethod)ReadLeShort()) {
throw new ZipException("Compression method mismatch");
}

// Skip time, crc, size and csize
long oldPos = baseStream.Position;
baseStream.Position += ZipConstants.LOCNAM - ZipConstants.LOCTIM;

if (baseStream.Position - oldPos != ZipConstants.LOCNAM - ZipConstants.LOCTIM) {
throw new EndOfStreamException();
}

// TODO make test more correct...  cant compare lengths as was done originally as this can fail for MBCS strings
int storedNameLength = ReadLeShort();
if (entry.Name.Length > storedNameLength) {
throw new ZipException("file name length mismatch");
}

int extraLen = storedNameLength + ReadLeShort();
return entry.Offset + ZipConstants.LOCHDR + extraLen;
}
}

/// 
/// Creates an input stream reading the given zip entry as
/// uncompressed data.  Normally zip entry should be an entry
/// returned by GetEntry().
/// 
/// 
/// the input stream.
/// 
/// 
/// The ZipFile has already been closed
/// 
/// 
/// The compression method for the entry is unknown
/// 
/// 
/// The entry is not found in the ZipFile
/// 
public Stream GetInputStream(ZipEntry entry)
{
if (entries == null) {
throw new InvalidOperationException("ZipFile has closed");
}

int index = entry.ZipFileIndex;
if (index < 0 || index >= entries.Length || entries[index].Name != entry.Name) {
index = FindEntry(entry.Name, true);
if (index < 0) {
throw new IndexOutOfRangeException();
}
}
return GetInputStream(index);
}

/// 
/// Creates an input stream reading the zip entry based on the index passed
/// 
/// 
/// An input stream.
/// 
/// 
/// The ZipFile has already been closed
/// 
/// 
/// The compression method for the entry is unknown
/// 
/// 
/// The entry is not found in the ZipFile
/// 
public Stream GetInputStream(int entryIndex)
{
if (entries == null) {
throw new InvalidOperationException("ZipFile has closed");
}

long start = CheckLocalHeader(entries[entryIndex]);
CompressionMethod method = entries[entryIndex].CompressionMethod;
Stream istr = new PartialInputStream(baseStream, start, entries[entryIndex].CompressedSize);

switch (method) {
case CompressionMethod.Stored:
return istr;
case CompressionMethod.Deflated:
return new InflaterInputStream(istr, new Inflater(true));
default:
throw new ZipException("Unknown compression method " + method);
}
}

/// 
/// Gets the comment for the zip file.
/// 
public string ZipFileComment {
get {
return comment;
}
}

/// 
/// Gets the name of this zip file.
/// 
public string Name {
get {
return name;
}
}

/// 
/// Gets the number of entries in this zip file.
/// 
/// 
/// The Zip file has been closed.
/// 
public int Size {
get {
if (entries != null) {
return entries.Length;
} else {
throw new InvalidOperationException("ZipFile is closed");
}
}
}

class ZipEntryEnumeration : IEnumerator
{
ZipEntry[] array;
int ptr = -1;

public ZipEntryEnumeration(ZipEntry[] arr)
{
array = arr;
}

public object Current {
get {
return array[ptr];
}
}

public void Reset()
{
ptr = -1;
}

public bool MoveNext()
{
return (++ptr < array.Length);                         }                 }                 class PartialInputStream : InflaterInputStream                 {                         Stream baseStream;                         long filepos, end;                         public PartialInputStream(Stream baseStream, long start, long len) : base(baseStream)                         {                                 this.baseStream = baseStream;                                 filepos = start;                                 end = start + len;                         }                         public override int Available                         {                                 get {                                         long amount = end - filepos;                                         if (amount > Int32.MaxValue) {
return Int32.MaxValue;
}

return (int) amount;
}
}

public override int ReadByte()
{
if (filepos == end) {
return -1; //ok
}

lock(baseStream) {
baseStream.Seek(filepos++, SeekOrigin.Begin);
return baseStream.ReadByte();
}
}

public override int Read(byte[] b, int off, int len)
{
if (len > end - filepos) {
len = (int) (end - filepos);
if (len == 0) {
return 0;
}
}
lock(baseStream) {
baseStream.Seek(filepos, SeekOrigin.Begin);
int count = baseStream.Read(b, off, len);
if (count > 0) {
filepos += len;
}
return count;
}
}

public long SkipBytes(long amount)
{
if (amount < 0) {                                         throw new ArgumentOutOfRangeException();                                 }                                 if (amount > end - filepos) {
amount = end - filepos;
}
filepos += amount;
return amount;
}
}
}
}

2010年12月30日 星期四

比較兩檔案之異同

使用下列敘述呼叫它:
CompFile FIRST.TXT SECOND.TXT 

using System;
using System.IO;  

public class CompFiles {
 public static void Main(string[] args) {
  int i=0, j=0;
  FileStream f1;
  FileStream f2;  

  try {
   // open first file
   try {
   f1 = new FileStream(args[0], FileMode.Open);
  } catch(FileNotFoundException exc) {
   Console.WriteLine(exc.Message);
   return;
  }  

  // open second file
  try {
   f2 = new FileStream(args[1], FileMode.Open);
  } catch(FileNotFoundException exc) {
   Console.WriteLine(exc.Message);
   return;
  }
  } catch(IndexOutOfRangeException exc) {
   Console.WriteLine(exc.Message + "\nUsage: CompFile f1 f2");
   return;
  }  

  // Compare files
  try {
   do {
    i = f1.ReadByte();
    j = f2.ReadByte();
    if(i != j) break;
   } while(i != -1 && j != -1);
  } catch(IOException exc) {
   Console.WriteLine(exc.Message);
  }
  if(i != j)
  Console.WriteLine("Files differ.");
  else
  Console.WriteLine("Files are the same."); 

  f1.Close();
  f2.Close();
 }
}

檔案之CheckSum範例

讀取指定檔案計算該檔案之CheckSum。
程式碼
using System;
using System.IO;
using System.Collections;

namespace ConsoleApplication1
{
public class CheckSum
{
private long sum = 0;

public CheckSum()
{
}

public void Add(int i)
{
sum += i;
}

public void Add(byte b)
{
sum += b;
}

public long GetCheckSum()
{
return sum;
}
}

public class MyClass
{
public static void Main()
{
string filename;
FileStream fs = null;
CheckSum sum = new CheckSum();

//Open the stream and read it back.
Console.Write("Please input filename:");
filename = Console.ReadLine();
Console.WriteLine("Trying to read {0}", filename);

if (File.Exists(filename))
Console.WriteLine("{0} is existed.", filename);

try
{
fs = File.Open(filename, FileMode.Open, FileAccess.Read);

if (fs.CanRead)
{
int value = 0;

while ((value = fs.ReadByte()) >= 0)
{
sum.Add(value);
}

Console.WriteLine("Checksum = {0,8:x}", sum.GetCheckSum());
}
else
Console.WriteLine("Cannot be read");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
fs.Close();
}

RL();
}

#region Helper methods
private static void RL()
{
Console.ReadLine();
}
#endregion
} 

}

檢查指定檔案是否正在被處理程序使用

檢查指定檔案是否正在被處理程序使用。
程式碼
public static bool CheckIfFileIsBeingUsed(string fileName)
{
FileStream s = null;
try
{
s=new FileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.Delete );
return false;
}
catch (System.IO.IOException)
{
return true;
}
finally
{
if (s != null)
s.Close();
}
}

寫入純文字檔

using System.IO;
private void WriteText(String filePath,String text)
{
FileStream fs=null ;
StreamWriter writer = null;
try
{
if( File.Exists(filePath))
{
fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
}
else
{
fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
}
writer = new StreamWriter(fs);
writer.WriteLine(text);
writer.Flush();
fs.Flush();
}
catch (Exception Ex)
{
Console.WriteLine(Ex.Message);
}
finally
{
if(fs!=null) fs.Close();
}
}

讀取純文字檔

using System.IO;
private void ReadFile(string fileName)
{
string line;
try
{
//Create an instance of StreamReader to read from a file.
using (StreamReader  sr= new StreamReader (fileName))
{
while(sr.Peek ()>-1)
{
line=sr.ReadLine();
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.Message);
}
}