我已经制作了一个可以将文件从源复制到目标的工具。但是,在复制过程中,该软件遇到了由反病毒软件(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);
        });

    }

最佳答案

发生的情况很可能是防病毒软件知道了病毒文件,因此当它检测到文件系统发生了变化(移动文件)时,它终止了程序,因为将病毒移动到了计算机的其他位置,它可能会引起问题(因为它是病毒)。它被标记为“Dropper”,基本上是一种旨在安装病毒的程序。

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

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

10-11 03:57