gpt4 book ai didi

c# - 复制病毒文件导致我的程序终止

转载 作者:太空狗 更新时间:2023-10-29 23:49:56 24 4
gpt4 key购买 nike

我制作了一个工具,可以将文件从源复制到目标。然而,在复制过程中,该软件遇到了被防病毒软件 (Symantec) 标记的病毒。

防病毒软件随后导致我的软件关闭,并将该程序作为“植入程序”隔离。

我是否可以优雅地处理这种情况,而不是完全关闭我的程序?

我知道该操作是反病毒的结果,但我可以做些什么来帮助解决这个问题?例如,Robocopy 不仅仅在遇到病毒时终止。

这是我的复制代码;

void CopyFileExactly(CopyParameterBundle cpb, bool overwrite)
{

string CTP = "", CFP = "";
CFP = cpb.SourcePath;

if (cpb.RenameFile)
CTP = cpb.DestPath ;
else
CTP = cpb.DestPath;

//Check firstly if the file to copy exists
if (!File.Exists(CFP))
{
throw new FileNotFoundException();
}

//Check if destination file exists
//If it does, make it not read only so we can update MAC times
if (File.Exists(CTP))
{
var target = GetFile(CTP);//new FileInfo(CTP);
if (target.IsReadOnly)
target.IsReadOnly = false;
}

var origin = GetFile(CFP);//new FileInfo(CFP);
GetFile(CTP).Directory.Create();
//(new FileInfo(CTP)).Directory.Create();

origin.CopyTo(CTP, (overwrite ? true : false));

if (!File.Exists(CTP))
{
throw new FileNotFoundException("Destination file not found!");
}
var destination = GetFile(CTP);//new FileInfo(CTP);
if (destination.IsReadOnly)
{
destination.IsReadOnly = false;
destination.CreationTime = origin.CreationTime;
destination.LastWriteTime = origin.LastWriteTime;
destination.LastAccessTime = origin.LastAccessTime;
destination.IsReadOnly = true;
}
else
{
destination.CreationTime = origin.CreationTime;
destination.LastWriteTime = origin.LastWriteTime;
destination.LastAccessTime = origin.LastAccessTime;
}

if (performMD5Check)
{
var md5Check = compareFileMD5(CFP, CTP);
cpb.srcMD5Hash = md5Check.Item2;
cpb.dstMD5Hash = md5Check.Item3;
if (!md5Check.Item1)
throw new MD5MismatchException("MD5 Hashes do NOT match!");
}

}

调用代码;

        void BeginCopy(int DegreeOfParallelism, int retryCount, int retryDelay)
{
object _lock;
//Setup cancellation token
po.CancellationToken = cts.Token;
//Set max number of threads
po.MaxDegreeOfParallelism = DegreeOfParallelism;
//Exceptio logging queue
var exceptions = new ConcurrentQueue<Exception>();
var completeItems = new ConcurrentQueue<CopyParameterBundle>();
var erroredItems = new ConcurrentQueue<CopyParameterBundle>();

//Logger logger = new Logger(sLogPath);

//logger.Write("Starting copy");
Task.Factory.StartNew(() =>
{
Parallel.ForEach(CopyParameters,
po,
(i, loopState, localSum) =>
{
localSum = retryCount;
do
{
try
{
//Go off and attempt to copy the file
DoWork(i);
//Incrememt total count by 1 if successfull
i.copyResults.TransferTime = DateTime.Now;
i.copyResults.TransferComplete = true;

completeItems.Enqueue(i);
//logger.Write("Copied file from: " + i.SourcePath + "\\" + i.SourceFile + " => " + i.DestPath + "\\" + i.SourceFile);

break;
}
catch (Exception ex)
{
//this.richTextBox1.AppendText("[-] Exception on: " + i.SourcePath + "\\" + i.SourceFile + " => " + ex.Message.ToString() + System.Environment.NewLine);
//Exception was thrown when attempting to copy file
if (localSum == 0)
{
//Given up attempting to copy. Log exception in exception queue
exceptions.Enqueue(ex);
this.SetErrorText(exceptions.Count());

//Write the error to the screen
this.Invoke((MethodInvoker)delegate
{
this.richTextBox1.AppendText("[-] Exception on: " + i.SourcePath + "\\" + i.SourceFile + " => " + ex.Message.ToString() + System.Environment.NewLine);
i.copyResults.TransferComplete = false;
i.copyResults.TransferTime = DateTime.Now;
i.copyResults.exceptionMsg = ex;
erroredItems.Enqueue(i);

//logger.Write("ERROR COPYING FILE FROM : " + i.SourcePath + "\\" + i.SourceFile + " => " + i.DestPath + "\\" + i.SourceFile + " => " + ex.Message.ToString() + " => " + ex.Source);
});
}
//Sleep for specified time before trying again
Thread.Sleep(retryDelay);
localSum--;
}

//Attempt to Repeat X times
} while (localSum >= 0);

//Check cancellation token
po.CancellationToken.ThrowIfCancellationRequested();

Interlocked.Increment(ref TotalProcessed);
this.SetProcessedText(TotalProcessed);

//Update Progress Bar
this.Invoke((MethodInvoker)delegate
{
this.progressBar1.Value = (TotalProcessed);
});
});


//aTimer.Stop();
this.Invoke((MethodInvoker)delegate
{
this.label9.Text = "Process: Writing Log";
});

WriteLog(sLogPath, completeItems, erroredItems);

this.Invoke((MethodInvoker)delegate
{
this.label9.Text = "Process: Done!";
});

if (exceptions.Count == 0)
MessageBox.Show("Done!");
else
MessageBox.Show("Done with errors!");

EnableDisableButton(this.button2, true);
EnableDisableButton(this.button4, false);
});

}

最佳答案

发生的事情很可能是防病毒软件发现了病毒文件,因此当它检测到文件系统发生变化(移动文件)时,它终止了程序,因为通过将病毒移动到文件中的不同位置你的电脑,它可能会导致问题(因为它是病毒)。它被标记为植入程序,基本上是一种旨在安装病毒的程序。

编辑:我忘了提到要解决您很可能需要许可您的程序的问题。

关于c# - 复制病毒文件导致我的程序终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com