Pre-cache FileInfo.Length for transfer of info
I have built a program which serializes and transmits FileInfos across a network.
It worked good until I tried getting the FileInfo.Length on the computer which has received a FileInfo that is about a file on another computer. It fails saying the file doesn't exist since the Length isn't stored in the FileInfo when declaring it.
To simplify it:
Is it possible to pre-store the Length of the file before sending the FileInfo to another computer where the file doesn't exist?
Or will I need to write my own FileInfoX Class which kinda just copies a FileInfo, and then stores all the variables?
Re: Pre-cache FileInfo.Length for transfer of info
The FileInfo class has an internal structure that stores the file information. If you get a property such as Length then it will presumably populate that structure. Assuming binary serialisation, that data should then make the trip. I'd say just try it for yourself and see.
Re: Pre-cache FileInfo.Length for transfer of info
I don't know if I understand what you mean completely, but I added a line before the serialization (yes binary) like this:
Code:
long size = info.Length;
Though even if I remove that line the FileInfo.Length property contains a value if I put a breakpoint before the serialization.
But somehow, that value is either lost when deserializing or the new fileinfo just wants to refresh :/
Re: Pre-cache FileInfo.Length for transfer of info
can you post the entire code? And could you also explain what it is you are trying to achieve here?
Re: Pre-cache FileInfo.Length for transfer of info
I am trying to send the FileInfo, with the code in my other thread you answered to recently, to another computer (first I serialize it). I am making a remote file browser to learn more about networking in C# :)
So yeah, the code basically works like this:
Create a list of FileInfo which we populate with a loop with all the files in a specific dir.
Then we binary serialize that list.
Then we transfer the bytes (converted to a string) to the other computer.
That computer deserializes it. When I look in the fileInfos in that list, there is no length info. It just says the error "Can't find file" or something like that, which is logical.
So I have at least one option: I create my own FileInfo class who uses FileInfo on the first computer to get all the info, but then it statically stores the info, instead of dynamically retrieving properties such as the Length.
Re: Pre-cache FileInfo.Length for transfer of info
it probably is the best thing to do - to create your own implementation of FileInfo but holds just informational values which you then serialize/deserialize.
Re: Pre-cache FileInfo.Length for transfer of info
I see, well to work then. Thx for the answer :)