Hi I am Reading a file from the user’s computer then using specific network credentials to write it out to a network share this corrupts a very small percentage of the files.
when i open the files in a hex editor the hex is different on the first line
Header: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
Good File: 50 4b 03 04 14 00 08 00 00 00 37 57 51 41 6f 61
Bad File: 50 4b 03 04 14 00 08 00 00 00 b7 56 51 41 6f 61
The difference in columns 0a and 0b exist throughout the whole file,
If I am going about this the wrong way by all means correct me or if it is just something small that would be better. Any help is much appreciated. The code I am using is below
var fileStream =
new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.None);
var impersonationContext =
new WrapperImpersonationContext("myDomain", "myname", "myPass");
impersonationContext.Enter();
try
{
using (Stream file = File.OpenWrite(destination))
{
fileStream.CopyTo(file);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
impersonationContext.Leave();
if(fileStream != null)
{
fileStream.Close();
}
}
FileStream.CopyTo()will write n bytes into the destination file, but won’t erase the end of the destination file that previously existed. Opening the output stream withFileMode.Createshould guarantee an exact copy.Calling
file.SetLength(file.Position);at the end may also work, but I’m not sure how reliably…