Я пытаюсь написать программу для копирования файлов резервной копии базы данных SQL на NAS. Раньше он работал, но затем я добавил проверку, чтобы увидеть, существуют ли пути, и теперь, когда он пытается скопировать файл, он получает исключение, говорящее: «System.IO.IOException: процесс не может получить доступ к файлу» C:\Program Files \Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\AdventureWorks2012.bak», так как он используется другим процессом».
Я проверил проводник процессов, и никакая другая программа не касается файла.
Что я делаю не так?
Код ниже
namespace BackupCopy
{
class Program
{
static void Main(string[] args)
{
// Set Source Path
string sourcePath = @"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup";
// Verify Source Path Exists
if (!Directory.Exists(sourcePath))
{
Console.WriteLine("Source path does not exist.");
Console.ReadLine();
return;
}
// Backup each file in Array
foreach (string filename in Directory.EnumerateFiles(sourcePath))
{
Backup(filename);
}
}
public static void Backup(string filename)
{
// Pull filename from method input parameter
string fileName = filename;
// Set Paths
string sourcePath = @"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup";
string targetPath = @"\\prometheus\NAS\DB Backups\mercurys";
// Verify Target Path Exists
if (!Directory.Exists(targetPath))
{
Console.WriteLine("Target path does not exist.");
Console.ReadLine();
return;
}
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a file to another location and
// overwrite the destination file if it already exists.
try
{
System.IO.File.Copy(sourceFile, destFile, true);
}
catch (Exception exc)
{
Console.WriteLine(exc);
Console.ReadLine();
return;
}
}
}
}
openfiles
(команда Windows) иhandle
(часть пакета Winternals). Сделайте поиск как можно менее ограничительным. - person user2864740   schedule 30.03.2015