.NET 2.x Копирование файла, использования потока, и ошибка "Файл занят" - C#
Формулировка задачи:
Ошибка
при выполнения пишет процесс занят другим процессом
как решить
static void copy_file (string old_file,string new_file)
{
int n = 0, numbytestoread = 0, numbytesread=0,progress_all=0;
FileInfo olds=new FileInfo(old_file);
FileInfo news=new FileInfo(new_file);
try
{
using (FileStream old_stream = new FileStream(olds.Name, FileMode.Open, FileAccess.Read))
{
numbytestoread = (int)olds.Name.Length;
numbytesread = 0;
byte[] bytes = File.ReadAllBytes(olds.Name);
progress_all = bytes.Length;
while (numbytestoread > 0)
{
n = old_stream.Read(bytes, 0, numbytestoread);
if (n == 0)
{
break;
}
numbytesread += n;
numbytestoread -= n;
}
numbytestoread = bytes.Length;
using (FileStream new_stream = new FileStream(news.Name, FileMode.Create, FileAccess.Write))
{
new_stream.Write(bytes, 0, numbytestoread);
Console.WriteLine("Progress: " + numbytestoread / progress_all + "%");
new_stream.Close();
}
old_stream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"ОШИБКА",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}Решение задачи: «.NET 2.x Копирование файла, использования потока, и ошибка "Файл занят"»
textual
Листинг программы
byte[] data = new byte[] {
0x47, 0x45, 0x4E, 0x44, 0x41, 0x4C, 0x46,
0x5F, 0x49, 0x53, 0x54, 0x41, 0x52, 0x49};