Автор:
Джеймс Крейг | добавлено: 12.01.2011, 21:02 | просмотров: 8946 (1+) | комментариев:
0 | рейтинг:
x6
Готовая функция, которая позволяет сохранить массив байт в указанный файл.
/// <summary>
/// Saves a file
/// </summary>
/// <param name="Content">File content</param>
/// <param name="FileName">File name to save this as (should include directories if applicable)</param>
/// <param name="Append">Tells the system if you wish to append data or create a new document</param>
public static void SaveFile(byte[] Content, string FileName, bool Append)
{
FileStream Writer = null;
try
{
int Index = FileName.LastIndexOf('/');
if (Index <= 0)
{
Index = FileName.LastIndexOf('\\');
}
if (Index <= 0)
{
throw new Exception("Directory must be specified for the file");
}
string Directory = FileName.Remove(Index) + "/";
bool Opened = false;
while (!Opened)
{
try
{
if (Append)
{
Writer = File.Open(FileName, FileMode.Append, FileAccess.Write, FileShare.None);
}
else
{
Writer = File.Open(FileName, FileMode.Create, FileAccess.Write, FileShare.None);
}
Opened = true;
}
catch (System.IO.IOException e)
{
throw e;
}
}
Writer.Write(Content, 0, Content.Length);
Writer.Close();
}
catch (Exception a)
{
throw a;
}
finally
{
if (Writer != null)
{
Writer.Close();
Writer.Dispose();
}
}
}