7
I am trying to move some files. For each file containing the word "TEST" in its body, my application indicates being a found item. The goal is to move them to another directory. Follow 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 it turns out, the method stream inside of 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 directory to access the system volume or unauthorized. The test was performed also 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 to the path.
Thanks, but unfortunately it didn’t work. Same error occurred.
– Fernando
Have you tried moving the file purely and simply without doing anything with it? It seems that your problem is of permission on the operating system.
– Maniero
No, but I can erase it. If there are other files in the source directory that do not contain the word "TEST" inside its body, these are ignored, being deleted only those that contain it. By this logic, maybe it is not a permission problem. I will take the test, it does not cost to try.
– Fernando
Tried to replace by
FileInfo.MoveTo()
?– Maniero
Now that I realize
dir2
is just one path simple, and the parameter there should be a path complete. I’m guessing you’ll have to ride it before you pass as argument.– Maniero
It worked. Really what was missing was matching the directory in the way you edited it. Thank you very much, it has helped me a lot so far @bigown!
– Fernando