簡體   English   中英

System.ArgumentOutOfRangeException C#

[英]System.ArgumentOutOfRangeException C#

我的一個游戲更新器在這里有點問題。 基本上,它應該做的是解密一個已經加密的文件,讀取它,然后再次加密並運行游戲。 有時,它會下載一個更新,為此需要下載加密的更新,解密游戲文件夾中已經存在的文件,添加更新,再次加密文件,運行游戲。 通常,我必須制作一個新的解密文件,並在更新時將其放在文件夾中,我想避免這種情況,讓exe自行完成所有操作,而無需制作任何其他文件。 除了到達需要讀取解密數據的那一部分之外,我已經走了很遠,但是我不知道如果那里沒有文件,那么它應該如何讀取(並且我想這樣保存)

這是我獲取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;

任何幫助,將不勝感激。

那么您要檢查文件是否存在?

然后,您可以使用簡單的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