简体   繁体   English

System.ArgumentOutOfRangeException C#

[英]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. 通常,我必须制作一个新的解密文件,并在更新时将其放在文件夹中,我想避免这种情况,让exe自行完成所有操作,而无需制作任何其他文件。 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.ArgumentOutOfRange异常(非负数)的地方。

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

  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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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