I'm trying to move some files. For each file containing the word "TEST" in your body, my application indicates that it is an item found. The goal is to move them to another directory. Here is the code below:
string[] Lista = new string[] { "TESTE"};
string dir = @"C:\ORIGEM";
string dir2 = @"C:\DESTINO";
private void btnQuarentena_Click(object sender, EventArgs e)
{
List<string> busca= Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories).ToList();
foreach (string item in busca)
{
StreamReader stream = new StreamReader(item);
string ler = stream.ReadToEnd();
foreach (string st in Lista)
{
if (Regex.IsMatch(ler, st))
{
try
{
stream.Close();
stream = null;
GC.Collect();
File.SetAttributes(dir, FileAttributes.Normal);
File.Move(item, dir2);
}
catch (Exception ex)
{
lblvirus.Text = "Alguns arquivos não puderam ser movidos! " + ex;
}
}
}
}
}
As you can see, the stream method inside my try catch block has been disabled so there are no process errors. Visual Studio returns me an unauthorized access error, and it is not a system or unauthorized volume access directory. The test was also performed with removable storage devices, and the same error occurs, being:
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Additional information: Access denied path.