using Microsoft.VisualBasic;
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Diagnostics;
public class StreamHelper
{
// 'See also FxLib Author: Kamal Patel, Rick Hodder
// 'Find the first entry of sToFing and returns the string after it
// 'See also FxLib StringExtract (and StuffString)
// Methods
public StreamHelper()
{
}
#region "Stream and Resources Functions"
//C# version of FxLib: Extended Functions Library for .NET FXLib
public static string FileToString(string cFileName)
{
StreamReader oReader = System.IO.File.OpenText(cFileName);
string lcString = oReader.ReadToEnd();
oReader.Close();
return lcString;
}
public static string StreamToString(Stream strm)
{
if (strm.CanSeek)
{
strm.Seek(0, SeekOrigin.Begin);
}
StreamReader reader1 = new StreamReader(strm);
return reader1.ReadToEnd();
}
//'another implementation(similar) in http://blog.steeleprice.net/archive/2004/04/06/202.aspx
public static string ResourceToString(string ResName)
{
Assembly assembly1 = Assembly.GetExecutingAssembly();
return ResourceToString(ResName, assembly1);
}
// Assembly assembly1 = Assembly.GetExecutingAssembly();
public static string ResourceByFullNameToString(string ResourceName,Assembly asm)
{
Stream stream = asm.GetManifestResourceStream(ResourceName);
if (stream == null)
{
throw new ApplicationException("Couldn't find embedded resource " + ResourceName);
}
return StreamToString(stream);
}
public static string ResourceToString(string ResName, Assembly Asm)
{
Stream stream1 = GetManifestResourceStream(ResName, Asm);
return StreamToString(stream1);
}
// ' Asm - the current caller assembly.
public static void ResourceToFile(string ResName, Assembly Asm, string FileName)
{
//object obj1;
Stream stream1 = GetManifestResourceStream(ResName, Asm);
StreamReader reader1 = new StreamReader(stream1);
StreamWriter writer1 = new StreamWriter(FileName, false);
writer1.Write(reader1.ReadToEnd());
writer1.Close();
//return obj1;
}
///<summary>
/// Inserts Assembly name in fornt of ResName, Should be called from FB assembles only
///</summary>
///<param name="ResName"></param>
///<param name="Asm"></param>
///<returns></returns>
public static string ResourceNameWithNamespace(string ResName, Assembly Asm)
{ //NOTE: If resource is located in subfolder of C# project, path of subfolder should be specified (it is included as part of namespace)
//' Resources are named using a fully qualified name (including namespace).
//assumed that namespace is the same as assembly names
//TODO use http://www.developersdex.com/gurus/articles/828.asp
//to find resource even if namespace is not specified
string sFullName = Asm.GetName().Name + "." + ResName;
return sFullName;
}
//'if ThrowException=true, then Throw exception if resource not found
public static bool EnsureManifestResourceExist(string ResName, Assembly Asm, bool ThrowException)
{ //NOTE: If resource is located in subfolder of C# project, path of subfolder should be specified (it is included as part of namespace)
//' Resources are named using a fully qualified name ((including namespace).
bool bRet=true;
ManifestResourceInfo info = Asm.GetManifestResourceInfo(ResName);
if (info == null)
{
bRet=false;
string sMsg = "Couldn't find embedded resource " + ResName + " in assembly " + Asm.FullName;
if (ThrowException)
{
throw new ApplicationException(sMsg);
}
else
{
Debug.Assert(false, sMsg);
}
}
return bRet;
}
//'Throw exception if resource not found
public static Stream GetManifestResourceStream(string ResName, Assembly Asm)
{ //NOTE: If resource is located in subfolder of C# project, path of subfolder should be specified (it is included as part of namespace)
//' Resources are named using a fully qualified name ((including namespace).
//Assume that Default Namespace is the same as Assemebly name
string sName = Asm.GetName().Name + "." + ResName;
Stream stream2 = Asm.GetManifestResourceStream(sName);
if (stream2 == null)//according to ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref10/html/M_System_Reflection_Assembly_GetManifestResourceStream_2_42a1d385.htm
{// .Net 2 throws exception by itself
throw new ApplicationException("Couldn't find embedded resource " + sName);
}
return stream2;
}
public static void SaveToFile(byte[] data, string FileName)
{
StreamWriter writer1 = new StreamWriter(FileName, false);
writer1.BaseStream.Write( data,0,data.Length);
writer1.Close();
}
public static void SaveToFile(Stream strm, string FileName)
{
StreamReader reader = new StreamReader(strm);
StreamWriter writer = new StreamWriter(FileName, false);
writer.WriteLine(reader.ReadToEnd());
writer.Close();
}
public static void SaveStringToFile(string sData, string FileName)
{
StreamWriter writer1 = new StreamWriter(FileName, false);
writer1.Write( sData );
writer1.Close();
}
//From http://winfx.msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_fxwebservices/html/9cbb7564-b257-4e33-a8c0-72db159e2820.asp
void Copy(Stream fromStream, Stream toStream)
{
StreamReader reader = new StreamReader(fromStream);
StreamWriter writer = new StreamWriter(toStream);
writer.WriteLine(reader.ReadToEnd());
writer.Flush();
}
#endregion //"Stream and Resources Functions"
}