简体   繁体   中英

System.ArgumentOutOfRangeException C#

I've come in a bit of an issue here with one of my game updaters. Basically, what it should do is decrypt an already encrypted file, read it, encrypt it again and run the game. Sometimes it will download an update, to do that it needs to download the encrypted update, decrypt the files already in the game folder, add the update, encrypt the files again, run the game. Usually I would have to make a new decrypted file and put it in the folder while its updating, and I want to avoid that and have the exe do it all on its own, without the need of any extra files made. I got pretty far except I came to the part where it needs to read the decrypted data, but i don't know how it should read it if there's no file there ( and i want to keep it that way )

This is where i get the System.ArgumentOutOfRange Exception Non Negative Number Required.

System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: value at System.IO.FileStream.set_Position(Int64 value) at Update.SAH.Write_File(FILE f, SAH patch) in C:\........SAH.cs:line322


Line 322:
BinaryReader br = new BinaryReader(File.OpenRead(patch.SAF_Path));
            br.BaseStream.Position = (long)f.Start;
            byte[] file = br.ReadBytes((int)f.Length);
            br.Dispose();
            BinaryWriter bw = new BinaryWriter(File.OpenWrite(SAF_Path));
            bw.BaseStream.Position = (int)new FileInfo(SAF_Path).Length;
            f.Start = (ulong)bw.BaseStream.Position;
            bw.Write(file);
            bw.Dispose();
            current_folder.Files.Add(f);

                f.Start = BitConverter.ToUInt64(file, Offset);
                Offset += 8;

                f.Length = BitConverter.ToUInt64(file, Offset);
                Offset += 8;

Any help would be appreciated.

So you want to check file exist ?

Then you can use simple IF

  if(File.Exist(patch.SAF_Path))
{
BinaryReader br = new BinaryReader(File.OpenRead(patch.SAF_Path));
            br.BaseStream.Position = (long)f.Start;
            byte[] file = br.ReadBytes((int)f.Length);
            br.Dispose();
            BinaryWriter bw = new BinaryWriter(File.OpenWrite(SAF_Path));
            bw.BaseStream.Position = (int)new FileInfo(SAF_Path).Length;
            f.Start = (ulong)bw.BaseStream.Position;
            bw.Write(file);
            bw.Dispose();
            current_folder.Files.Add(f);

            f.Start = BitConverter.ToUInt64(file, Offset);
            Offset += 8;

            f.Length = BitConverter.ToUInt64(file, Offset);
            Offset += 8;
}
else
{
//do somethings else downloand this file maybe and try read it
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM