/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
public static class FileManager
{
/// <summary>
/// Gets a files' contents
/// </summary>
/// <param name="FileName">File name</param>
/// <returns>a string containing the file's contents</returns>
public static string GetFileContents(Uri FileName)
{
WebClient Client = null;
StreamReader Reader = null;
try
{
Client = new WebClient();
Reader = new StreamReader(Client.OpenRead(FileName));
string Contents = Reader.ReadToEnd();
Reader.Close();
return Contents;
}
catch
{
return "";
}
finally
{
if (Reader != null)
{
Reader.Close();
Reader.Dispose();
}
if (Client != null)
{
Client.Dispose();
}
}
}
}
}
Month: February 2011
Gets a files' contents with MemoryStream
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
public static class FileManager
{
/// <summary>
/// Gets a files' contents
/// </summary>
/// <param name="FileName">File name</param>
/// <param name="TimeOut">Amount of time in ms to wait for the file</param>
/// <param name="Output">Output of the file in bytes</param>
public static void GetFileContents(string FileName, out byte[]Output, int TimeOut)
{
FileStream Reader = null;
MemoryStream TempReader=null;
int StartTime = System.Environment.TickCount;
try
{
bool Opened = false;
while (!Opened)
{
try
{
if (System.Environment.TickCount - StartTime >= TimeOut)
throw new System.IO.IOException("File opening timed out");
Reader = File.OpenRead(FileName);
Opened = true;
}
catch (System.IO.IOException e)
{
throw e;
}
}
byte[] Buffer = new byte[1024];
TempReader=new MemoryStream();
while(Reader.Read(Buffer,0,1024)==1024)
{
TempReader.Write(Buffer,0,Buffer.Length);
}
Reader.Close();
Output=TempReader.ToArray();
TempReader.Close();
}
catch
{
Output = null;
return;
}
finally
{
if (Reader != null)
{
Reader.Close();
Reader.Dispose();
}
if (TempReader != null)
{
TempReader.Close();
TempReader.Dispose();
}
}
}
}
}
Gets a files' contents
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
/// <summary>
/// Utility class for managing files
/// </summary>
public static class FileManager
{
/// <summary>
/// Gets a files' contents
/// </summary>
/// <param name="FileName">File name</param>
/// <returns>a string containing the file's contents</returns>
public static string GetFileContents(string FileName)
{
try
{
return GetFileContents(FileName, 5000);
}
catch { throw; }
}
/// <summary>
/// Gets a files' contents
/// </summary>
/// <param name="FileName">File name</param>
/// <param name="TimeOut">Amount of time in ms to wait for the file</param>
/// <returns>a string containing the file's contents</returns>
public static string GetFileContents(string FileName, int TimeOut)
{
StreamReader Reader = null;
int StartTime = System.Environment.TickCount;
try
{
bool Opened = false;
while (!Opened)
{
try
{
if (System.Environment.TickCount - StartTime >= TimeOut)
throw new System.IO.IOException("File opening timed out");
Reader = File.OpenText(FileName);
Opened = true;
}
catch (System.IO.IOException e)
{
throw e;
}
}
string Contents = Reader.ReadToEnd();
Reader.Close();
return Contents;
}
catch
{
return "";
}
finally
{
if (Reader != null)
{
Reader.Close();
Reader.Dispose();
}
}
}
}
}
Read all the content from a file as string in default encoding
//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;
}
}
}
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]

