Read Text File From Ressource : Text File Read Write « File Stream « C# / C Sharp






Read Text File From Ressource

     

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

namespace LateBindingApi.CodeGenerator.ComponentAnalyzer
{
    /// <summary>
    /// offers various helper functions
    /// </summary>
    internal static class Utils
    {   
        #region Ressource Methods

        /// <summary>
        /// Read RessourceText
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        internal static string ReadTextFileFromRessource(string ressourcePath)
        {
            System.IO.Stream ressourceStream = null;
            System.IO.StreamReader textStreamReader = null;
            try
            {
                string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                ressourcePath = assemblyName + "." + ressourcePath;
                ressourceStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(ressourcePath);
                if (ressourceStream == null)
                    throw (new System.IO.IOException("Error accessing resource Stream."));

                textStreamReader = new System.IO.StreamReader(ressourceStream);
                if (textStreamReader == null)
                    throw (new System.IO.IOException("Error accessing resource File."));

                string text = textStreamReader.ReadToEnd();
                return text;
            }
            catch (Exception exception)
            {
                throw (exception);
            }
            finally
            {
                if(null!=textStreamReader)
                    textStreamReader.Close();
                if (null != ressourceStream)
                    ressourceStream.Close();
            }
        }
        
        #endregion

        #region Xml Methods

        /// <summary>
        /// Gets friendly inner xml from node
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static String GetFormattedXml(XmlNode node)
        {
            string result = "";
            
            MemoryStream memStream = new MemoryStream();
            XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            textWriter.Formatting = Formatting.Indented;
            node.WriteContentTo(textWriter);
            textWriter.Flush();
            memStream.Flush();
            memStream.Position = 0;

            StreamReader sReader = new StreamReader(memStream);   
            String FormattedXML = sReader.ReadToEnd();
            result = FormattedXML;
            memStream.Close();
            textWriter.Close();

            return result;
        }

        #endregion

        #region Guid Methods

        /// <summary>
        /// returns a new xml encoded guid as string
        /// </summary>
        /// <returns></returns>
        public static string NewEncodedGuid()
        {
            return XmlConvert.EncodeName(Guid.NewGuid().ToString());
        }

        /// <summary>
        /// xml encode guid and remove the chars"{}" if exists
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static string EncodeGuid(string guid)
        {
            return XmlConvert.EncodeName(guid.Replace("{", "").Replace("}", ""));
        }

        #endregion

        #region IO Methods

        /// <summary>
        /// Removes bad chars from filePath
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string RemoveBadChars(string filePath)
        {
            if (filePath == null)
                return "";

            string validateFilePath = filePath.Replace("\0", "");

            char[] badChars = System.IO.Path.GetInvalidPathChars();
            char replaceChar  = new char();
            
            foreach (char item in badChars)
                validateFilePath = validateFilePath.Replace(item, replaceChar);

            return validateFilePath;
        }

        #endregion
    }
}

   
    
    
    
    
  








Related examples in the same category

1.Read and Write a Text File
2.Read text file line by line
3.Read ASCII string from byte bufferRead ASCII string from byte buffer
4.Reads and displays bytes until end-of-fileReads and displays bytes until end-of-file
5.Write string to a text file
6.Read whole text file to the end
7.Read text file line by line with exception catch
8.Text file Write with format and write boolean value to a text file
9.Action Text Reader Line
10.Open and Append to a Log File
11.Read and Write to a Newly Created Data File
12.Read Text from a File
13.Read text file with File.OpenText
14.Write Text to a File
15.Read a text file and obtain it's contents.
16.Reads text from a file.
17.Saves the text to a file.
18.Create a large file of 100 lines to upload
19.Creates or opens a file for writing and writes text to it.
20.Returns the raw number of the current line count.
21.Returns the zero-based line number where source appears in target.
22.Returns the number of lines appearing in target where a line is counted as a '\n'