Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been translating some full .net code onto the compact framwork. I've
had a problem with this
so how i can to do it on mobile application
thanks for nay help

OpenFileDialog op ;//= new OpenFileDialog();

op = new OpenFileDialog();
op.Filter = "ALL File|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
    byte[] bytes = File.ReadAllBytes(op.FileName);
    for (int i = 0; i < bytes.Length; i++)
    {
        bytes[i] += (byte)numericUpDown1.Value;
        bytes[i]  += (byte)(numericUpDown2.Value);
        bytes[i] -= (byte)numericUpDown3.Value;
    }
    SaveFileDialog sv = new SaveFileDialog();
    sv.Filter = Path.GetExtension(op.FileName).ToString().Replace(".", "").ToUpper() + "|*" + Path.GetExtension(op.FileName);
   if( sv.ShowDialog()== DialogResult .OK )
       File.WriteAllBytes(sv.FileName, bytes);
}


[edit]Code block added = OriginalGriff[/edit]
Posted
Updated 4-Feb-11 22:09pm
v2

1 solution

The compact framework does not include the whole of .NET - as I'm sure you know.

Unfortunately, File.ReadAllBytes and File.WriteAllBytes are two of the casualties.

They are simple to replace though:
C#
public static byte[] ReadAllBytes(string path)
    {
    byte[] data;
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
        int offset = 0;
        int count = (int) fs.Length;
        data = new byte[count];
        while (count > 0)
            {
            int bytesRead = fs.Read(data, offset, count);
            offset += bytesRead;
            count -= bytesRead;
            }
        }
    return data;
    }
WriteAllBytes is the same process in reverse: I'll leave it to you!



"mostafa.elsadany - 6 mins ago
ok can you tell me how i can write this bytes to file on compact thanks for help
Reply
mostafa.elsadany - 4 mins ago
not leave me because i know this code from other site
but i don't know the reverse
anyway thanks for help"


You are kidding, right?

It is kinda simple:
C#
public static void WriteAllBytes(string path, byte[] data)
    {
    using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
        {
        fs.Write(data, 0, data.Length);
        }
    }
Even a beginner should be able to work that one out... :laugh:
 
Share this answer
 
v2
Comments
Mostafa Elsadany 5-Feb-11 4:25am    
ok can you tell me how i can write this bytes to file on compact thanks for help
Mostafa Elsadany 5-Feb-11 4:27am    
not leave me because i know this code from other site
but i don't know the reverse
anyway thanks for help
OriginalGriff 5-Feb-11 4:40am    
Answer updated
Mostafa Elsadany 5-Feb-11 4:47am    
thanks for help
but
Do not make fun of a person learns
Thank you to qualify me less than beginner
OriginalGriff 5-Feb-11 4:53am    
If you can't work out how to write a three-line WriteAllBytes routine, then perhaps you need to start with something a bit simpler than .NetCF: there is a lot of foundation material you need to learn before you can cope with replacing the stuff that isn't included. Trust me, it gets a lot more complex than WriteAllBytes!

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