Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i read each characters into an array from a file using C#
Posted

Use a StreamReader[^].
 
Share this answer
 
might help,

The contents of the temp.ini file,
[Setup]
ProductName=Microsoft Visual C++ 2008 Redistributable Package
ProductMsi=vc_red.msi

and after running this program,
C#
class Program
{
    static void Main(string[] args)
    {
        /*  replace C:\\temp.ini with your file*/
        var charArrays = ReadAllChar("C:\\temp.ini");

        /*To test you can ignore this line*/
        charArrays.ToList().ForEach(ch => Console.Write(ch));
    }

    static char[] ReadAllChar(string filename)
    {
        if (File.Exists(filename))
            return File.ReadAllText(filename).ToCharArray();
        throw new FileNotFoundException(filename);
    }
}

This program produces following output,
txt
[Setup]
ProductName=Microsoft Visual C++ 2008 Redistributable Package
ProductMsi=vc_red.msi


:)
 
Share this answer
 
byte[] file = System.IO.File.ReadAllBytes("FilePath");
 
Share this answer
 
Comments
Kuthuparakkal 9-Oct-12 22:00pm    
Byte array and char array are not same. Don't post whatever you like, think
Try this:
C#
System.IO.File.ReadAllText(yourfilename).ToCharArray();
 
Share this answer
 
C#
using (StreamReader sr = new StreamReader(FilePathString, true))
                        {
                            string SD_TextContent = sr.ReadToEnd();
                            sr.Dispose();
                        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900