Gets a file's contents (Used primarily for text documents on an FTP)

   
 
/*
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 file&#039;s contents (Used primarily for text documents on an FTP)
        /// </summary>
        /// <param name="FileName">Name of the file (should include the server address)</param>
        /// <param name="UserName">User name to log in</param>
        /// <param name="Password">Password to log in</param>
        /// <returns>A string containing the file&#039;s contents</returns>
        public static string GetFileContents(Uri FileName, string UserName, string Password)
        {
            WebClient Client = null;
            StreamReader Reader = null;
            try
            {
                Client = new WebClient();
                Client.Credentials = new NetworkCredential(UserName, Password);
                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();
                }
            }
        }
    }
}

   
     


Gets a files' contents from a Url and save to an OutputStream

   
 
/*
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&#039; contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <param name="OutputStream">The output stream of the file</param>
        /// <returns>a string containing the file&#039;s contents</returns>
        public static void GetFileContents(Uri FileName,out Stream OutputStream)
        {
            WebClient Client = null;
            OutputStream = null;
            try
            {
                Client = new WebClient();
                OutputStream = Client.OpenRead(FileName);
            }
            catch
            {
            }
        }
    }
}

   
     


Gets a files' contents from a Url

   
 
/*
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&#039; contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <returns>a string containing the file&#039;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();
                }
            }
        }
    }
}

   
     


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&#039; 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&#039; contents
        /// </summary>
        /// <param name="FileName">File name</param>
        /// <returns>a string containing the file&#039;s contents</returns>
        public static string GetFileContents(string FileName)
        {
            try
            {
                return GetFileContents(FileName, 5000);
            }
            catch { throw; }
        }

        /// <summary>
        /// Gets a files&#039; 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&#039;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;
        }
    }
}