//http://tinyerp.codeplex.com/ //GNU Library General Public License (LGPL) //----------------------------------------------------------------------- // <copyright file="SysUtil.cs" company="Pyramid Consulting"> // Copyright (c) Pyramid Consulting. All rights reserved. // </copyright> //----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Text; namespace Bamboo.Core.Common { public class SysUtil { /// <summary> /// read all the content from a file as string in default encoding /// </summary> /// <param name="strFilePath">source file path</param> /// <returns>dest string in default encoding</returns> public static String ReadFile(String strFilePath) { System.Text.Encoding encDefault = System.Text.Encoding.GetEncoding(0); System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); String strResult = null; try { byte[] bData = new byte[fs.Length]; br.Read(bData, 0, bData.Length); strResult = encDefault.GetString(bData); } finally { br.Close(); fs.Close(); } return strResult; } } }
File Stream
Read all the content from a file as byte array
//http://tinyerp.codeplex.com/ //GNU Library General Public License (LGPL) //----------------------------------------------------------------------- // <copyright file="SysUtil.cs" company="Pyramid Consulting"> // Copyright (c) Pyramid Consulting. All rights reserved. // </copyright> //----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Text; namespace Bamboo.Core.Common { public class SysUtil { /// <summary> /// read all the content from a file as byte array /// </summary> /// <param name="strFilePath">source file path</param> /// <returns>dest byte array on succced</returns> public static byte[] ReadFileAsBytes(String strFilePath) { System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); byte[] baResult = null; try { baResult = new byte[fs.Length]; br.Read(baResult, 0, baResult.Length); } finally { br.Close(); fs.Close(); } return baResult; } } }
Hex value Dump
using System;
using System.IO;
class HexDump {
public static void Main(string[] astrArgs) {
Stream stream = new FileStream(“c:a.txt”, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] abyBuffer = new byte[16];
long lAddress = 0;
int count;
while ((count = stream.Read(abyBuffer, 0, 16)) > 0) {
ComposeLine(lAddress, abyBuffer, count);
lAddress += 16;
}
}
public static void ComposeLine(long lAddress, byte[] abyBuffer, int count) {
Console.WriteLine(String.Format(“{0:X4}-{1:X4} “, (uint)lAddress / 65536, (ushort)lAddress));
for (int i = 0; i < 16; i++) {
Console.WriteLine((i < count) ? String.Format("{0:X2}", abyBuffer[i]) : " ");
Console.WriteLine((i == 7 && count > 7) ? “-” : ” “);
}
Console.WriteLine(” “);
for (int i = 0; i < 16; i++) { char ch = (i < count) ? Convert.ToChar(abyBuffer[i]) : ' '; Console.WriteLine(Char.IsControl(ch) ? "." : ch.ToString()); } } } [/csharp]
Demonstrate random access
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate random access.
using System;
using System.IO;
public class RandomAccessDemo {
public static void Main() {
FileStream f;
char ch;
try {
f = new FileStream(“random.dat”, FileMode.Create);
}
catch(IOException exc) {
Console.WriteLine(exc.Message);
return ;
}
// Write the alphabet.
for(int i=0; i < 26; i++) {
try {
f.WriteByte((byte)('A'+i));
}
catch(IOException exc) {
Console.WriteLine(exc.Message);
return ;
}
}
try {
// Now, read back specific values
f.Seek(0, SeekOrigin.Begin); // seek to first byte
ch = (char) f.ReadByte();
Console.WriteLine("First value is " + ch);
f.Seek(1, SeekOrigin.Begin); // seek to second byte
ch = (char) f.ReadByte();
Console.WriteLine("Second value is " + ch);
f.Seek(4, SeekOrigin.Begin); // seek to 5th byte
ch = (char) f.ReadByte();
Console.WriteLine("Fifth value is " + ch);
Console.WriteLine();
// Now, read every other value.
Console.WriteLine("Here is every other value: ");
for(int i=0; i < 26; i += 2) {
f.Seek(i, SeekOrigin.Begin); // seek to ith double
ch = (char) f.ReadByte();
Console.Write(ch + " ");
}
}
catch(IOException exc) {
Console.WriteLine(exc.Message);
}
Console.WriteLine();
f.Close();
}
}
[/csharp]
Copy a file
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* Copy a file. To use this program, specify the name of the source file and the destination file. For example, to copy a file called FIRST.DAT to a file called SECOND.DAT, use the following command line. CopyFile FIRST.DAT SECOND.DAT */ using System; using System.IO; public class CopyFile { public static void Main(string[] args) { int i; FileStream fin; FileStream fout; try { // open input file try { fin = new FileStream(args[0], FileMode.Open); } catch(FileNotFoundException exc) { Console.WriteLine(exc.Message + " Input File Not Found"); return; } // open output file try { fout = new FileStream(args[1], FileMode.Create); } catch(IOException exc) { Console.WriteLine(exc.Message + " Error Opening Output File"); return; } } catch(IndexOutOfRangeException exc) { Console.WriteLine(exc.Message + " Usage: CopyFile From To"); return; } // Copy File try { do { i = fin.ReadByte(); if(i != -1) fout.WriteByte((byte)i); } while(i != -1); } catch(IOException exc) { Console.WriteLine(exc.Message + "File Error"); } fin.Close(); fout.Close(); } }
Write to a file
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Write to a file.
using System;
using System.IO;
public class WriteToFile {
public static void Main(string[] args) {
FileStream fout;
// open output file
try {
fout = new FileStream(“test.txt”, FileMode.Create);
} catch(IOException exc) {
Console.WriteLine(exc.Message + ”
Error Opening Output File”);
return;
}
// Write the alphabet to the file.
try {
for(char c = 'A'; c <= 'Z'; c++)
fout.WriteByte((byte) c);
} catch(IOException exc) {
Console.WriteLine(exc.Message + "File Error");
}
fout.Close();
}
}
[/csharp]
Display a text file
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* Display a text file. To use this program, specify the name of the file that you want to see. For example, to see a file called TEST.CS, use the following command line. ShowFile TEST.CS */ using System; using System.IO; public class ShowFile { public static void Main(string[] args) { int i; FileStream fin; try { fin = new FileStream(args[0], FileMode.Open); } catch(FileNotFoundException exc) { Console.WriteLine(exc.Message); return; } catch(IndexOutOfRangeException exc) { Console.WriteLine(exc.Message + " Usage: ShowFile File"); return; } // read bytes until EOF is encountered do { try { i = fin.ReadByte(); } catch(Exception exc) { Console.WriteLine(exc.Message); return; } if(i != -1) Console.Write((char) i); } while(i != -1); fin.Close(); } }