I am trying to overwrite a file's contents. That file is in use in most of the cases. I know how to do this in Java, but I am not sure how to do this in C#. That's what I currently have:
public bool ByteArrayToFile(string fileName, byte[] byteArray)
{
try
{
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(byteArray, 0, byteArray.Length);
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in process: {0}", ex);
return false;
}
}
I am trying to write a byte array to a file. I get the byte array from an URL, using a WebClient. When the file's in use, I get an IOException, which says that I can't access the file because it's being used by another process. In Java, I'd do it like this, using commons-io:
FileUtils.copyInputStreamToFile(new URL(url).openStream(), target);
The method above has worked for me, but I can't seem to replicate it in C#. Is there a way to do it?