Hi

Strange requirement. Virus? :D . Something like

class Program
    {

        private static byte[] exe;

        static string GetFileName()
        {
            return
Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                                       "ConsoleApplication5.exe");
        }

        static void Read(string[] args)
        {
            using (var fs = new FileStream(GetFileName(),
FileMode.Open, FileAccess.Read))
            {
                var ms = new MemoryStream();
                int bl = 1024;
                byte[] buffer = new byte[bl];

                int bytesReaded = fs.Read(buffer, 0, bl);
                while (bytesReaded>0)
                {
                    ms.Write(buffer, 0, bytesReaded);
                    bytesReaded = fs.Read(buffer, 0, bl);
                }

                ms.Position = 0;
                exe = ms.ToArray();
                ms.Close();
                fs.Close();
            }
        }

        static void WriteExe()
        {
            var fileName = GetFileName();

            if (File.Exists(fileName))
                return;

            using (var fs = new FileStream(fileName, FileMode.Create,
FileAccess.Write))
            {
                fs.Write(exe, 0, exe.Length);
                fs.Close();
            }
        }
    }

2009/8/29 Marcus <[email protected]>:
>
> Hello
>
> I need to store a small exe file in memory. I want to do this because
> I want some sabotage protection in my app. If the user removes this
> essential file from the harddrive, my app will not work the next time
> it is to be launched.
>
> So I want to read this exe file into memory initially when my program
> starts up. Then I will use a timer that at some interval checks if
> this file is present on the file system. If not, then my app writes it
> back from memory to the harddrive.
>
> Could someone please give an example how to do this. It should be a
> pretty small piece of could I reccon, but I can not figure it out.
>
> Thanks
>
>

Reply via email to