使用(FileStream theStream = File.Open(fileName +_tmp,FileMode.Create)){ ProtoBuf.Serializer.Serialize< MyObject>(theStream,inObjectToSerialize); } File.Copy(fileName +_tmp,fileName,true);
没有_tmp文件,只是主文件。该文件的大小是非零,这使我相信信息是完整的。有没有办法恢复这些数据?
更新:
我试过了Marc对ProtoReader的建议并且问题中的文件导致在 reader.ReadFieldHeader()引发异常。 ProtoException读取:源数据中的无效字段:0
解决方案我没有太多的去那里。 ..我想这个简短的版本是:它取决于文件中的内容。
你可以做的一件事就是走该文件,看看有什么: $ $ $ p $使用(var input = File.OpenRead(path)) using( var reader = new ProtoReader(input,RuntimeTypeModel.Default,null)) { while(reader.ReadFieldHeader()> 0) { Console.WriteLine(offset {0},字段{1},类型{2}, reader.Position,reader.FieldNumber,reader.WireType); reader.SkipField(); $ b $ p $ b pre $ b $如果你知道一些字段的布局(即字段3是一个字符串,字段7是一个子对象,等等),那么你可以使输出更详细。
The machine power was cut while, I assume, my application was updating a file. When turned back on and my application started it attempted to deserialize the file. The call to Serializer.Deserialize did not fail, but the resulting object has default values for every property.
My file updating/saving:
using (FileStream theStream = File.Open(fileName + "_tmp", FileMode.Create)) { ProtoBuf.Serializer.Serialize<MyObject>(theStream, inObjectToSerialize); } File.Copy(fileName + "_tmp", fileName, true);There is no _tmp file, just the main file. The size of the file is non-zero which makes me believe the information is intact. Is there a way to recover this data?
Update:
I've tried the Marc's suggestion with ProtoReader and the file in questions causes an exception the be thrown at reader.ReadFieldHeader(). The ProtoException reads: "Invalid field in source data:0"
解决方案There isn't much for me to go on there... I guess the short version would be: it depends what is left in the file.
One thing you could do would be to walk the file to see what is there:
using (var input = File.OpenRead(path)) using (var reader = new ProtoReader(input, RuntimeTypeModel.Default, null)) { while (reader.ReadFieldHeader() > 0) { Console.WriteLine("offset {0}, field {1}, type {2}", reader.Position, reader.FieldNumber, reader.WireType); reader.SkipField(); } }That would at least allow you to see how much data is currently processing.
If you know the layout of some fields (i.e. "field 3 is a string, field 7 is a sub-object, etc"), then you could make the output more detailed.
更多推荐
恢复损坏的文件序列化与Protobuf网
发布评论