Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello sir here is my code;
when i run this function there is error "cannot convert from long to int."
C#
public byte[] readfile(string FPath)
        {
            byte[] data = null;
            try
            {
                FileInfo FI = new FileInfo(FPath);

                long numbytes = FI.Length; // 

                FileStream fstream = new FileStream(FPath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fstream);
                //data = br.ReadBytes(Convert.ToInt64(numbytes));

                data = br.ReadBytes(Convert.ToInt64(numbytes));   

//At above line error comes like;
//Error	3	Argument 1: cannot convert from 'long' to 'int'

   fstream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());

            }
            return data;
        }


sir tell me how to do this
Posted
Updated 25-Aug-14 18:40pm
v3

Actually you got that error because BinaryReader.ReadBytes method is accepting argument as integer, and you are passing long into it.

so you can change code like this...

data = br.ReadBytes(Convert.ToInt32(numbytes));

will resolve your issue
 
Share this answer
 
Comments
vikinghunter 26-Aug-14 1:59am    
Will not "FI.length > int.MaxValue" happen?
Use "WriteByte" instead of "WriteBytes":
public byte[] readfile(string FPath)
{
    byte[] data = null;
    try
    {
        FileInfo FI = new FileInfo(FPath);

        long numbytes = FI.Length; // 

        FileStream fstream = new FileStream(FPath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fstream);

        MemoryStream ms = new MemoryStream();

        for (long i = 0; i < numbytes; i++)
        {
            ms.WriteByte(br.ReadByte());
        }

        ms.Close();
        fstream.Close();

        data = ms.ToArray();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());

    }
    return data;
}
 
Share this answer
 
v2

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