<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WriteLargeFile.aspx.cs"
Inherits="Phil_SandBox.DocPubSandBox.WriteLargeFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
File (Relative to Server):
</td>
<td>
<asp:TextBox ID="txtFileInputLocation" runat="server" />
</td>
</tr>
<tr>
<td>
File Write location:
</td>
<td>
<asp:TextBox ID="txtFileOutputLocation" runat="server" />
</td>
</tr>
</table>
<br />
<asp:Button ID="btnAsyncWrite" Text="Async Write" runat="server" OnClick="btnAsyncWrite_Click" />
<asp:Button ID="btnTempWriteAndCopy" Text="Temp Write and Copy" runat="server" OnClick="btnTempWriteAndCopy_Click" />
<br />
<br />
Results:
<asp:Label ID="lblResults" runat="server" />
</div>
</form>
</body>
</html>
<%-- Code Behind --%>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Threading;
using System.Security.Principal;
namespace Phil_SandBox.DocPubSandBox
{
public partial class WriteLargeFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblResults.Text = string.Empty;
}
protected void btnAsyncWrite_Click(object sender, EventArgs e)
{
writeFile(new AsyncFileWriter(false));
}
protected void btnTempWriteAndCopy_Click(object sender, EventArgs e)
{
writeFile(new TempWriteAndCopy());
}
private void writeFile(ILargeFileWriter fileWriter)
{
string fileReadPath = txtFileInputLocation.Text;
string fileWritePath = txtFileOutputLocation.Text;
byte[] bytesOfFile;
#region Read in File
try
{
DateTime fileReadStartTime = DateTime.Now;
bytesOfFile = File.ReadAllBytes(fileReadPath);
lblResults.Text += "Read file at [" + fileReadPath + "] in [" +
DateTime.Now.Subtract(fileReadStartTime).Milliseconds + "] ms. " +
"File Size: " + bytesOfFile.Length + " bytes <br /><br />";
}
catch (Exception exc)
{
lblResults.Text += "Failed Reading File [" + fileReadPath + "]: <br />" +
exc.Message + "<br /> " + exc.Source + "<br />" + exc.StackTrace;
return;
}
#endregion
#region Write File
try
{
DateTime fireWriteStartTime = DateTime.Now;
fileWriter.WriteFile(fileWritePath, bytesOfFile);
lblResults.Text += "Using [" + fileWriter.GetType().FullName + "] <br/>" +
"Wrote file at [" + fileWritePath + "] in [" +
DateTime.Now.Subtract(fireWriteStartTime).Milliseconds + "] ms. " +
"<hr />";
}
catch (Exception exc)
{
lblResults.Text += "Failed Writing File [" + fileWritePath + "]: <br />" +
exc.Message + "<br /> " + exc.Source + "<br />" + exc.StackTrace;
return;
}
#endregion
}
}
public class AsyncFileWriter : ILargeFileWriter
{
public AsyncFileWriter() { }
public AsyncFileWriter(bool openFileStreamWithUseAsyncFlag)
{
OpenFileStreamWithUseAsyncFlag = openFileStreamWithUseAsyncFlag;
}
public bool OpenFileStreamWithUseAsyncFlag { get; set; }
private bool IsWriting { get; set; }
/// <summary>
/// Method based on example at
/// http://msdn.microsoft.com/en-us/library/7db28s3c.aspx
/// </summary>
public void WriteFile(string path, byte[] bytesOfFile)
{
FileStream fs = new FileStream(
path,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.None,
4096,
OpenFileStreamWithUseAsyncFlag);
fs.BeginWrite(bytesOfFile, 0, bytesOfFile.Length,
new AsyncCallback(EndWriteCallback),
fs);
this.IsWriting = true;
while (IsWriting)
{
//wait for fs.BeginWrite to finish
Thread.Sleep(500);
}
}
private void EndWriteCallback(IAsyncResult asyncResult)
{
FileStream fs = (FileStream)asyncResult.AsyncState;
fs.EndWrite(asyncResult);
this.IsWriting = false;
}
}
public class TempWriteAndCopy : ILargeFileWriter
{
public void WriteFile(string path, byte[] bytesOfFile)
{
string temfilePath = Path.GetTempPath() + Path.GetFileName(path);
File.WriteAllBytes(temfilePath, bytesOfFile);
File.Move(temfilePath, path);
}
}
public interface ILargeFileWriter
{
void WriteFile(string path, byte[] bytesOfFile);
}
}
Additionally, using the asynchronous methods will stop your application from hanging when doing large reads or writes.