C#System.IO.IOException

时间:2016-12-08 20:52:23

标签: c#

我有以下代码:

using System;
using System.Collections.Generic;
using System.IO;
using VirusTotalNET;
using VirusTotalNET.Objects;
using System.Linq;
using System.Security.Permissions;

namespace VirusTotalNETClient
{
    class Program
    {
        private const string ScanUrl = "http://www.google.com/";

        static void Main(string[] args)
        {
            VirusTotal virusTotal = new VirusTotal("5d8684f50946c2bdeaf5c4fd966f61f3661de808e9d7324b99788d6f4fb7ad57");

            //Use HTTPS instead of HTTP
            virusTotal.UseTLS = true;

            //creating folder for programs reliqies and output log
            string folderName = "C:\\OnlineScanner";
            System.IO.Directory.CreateDirectory(folderName);

            //get list of files to analyse
            var paths = Traverse("C:\test");
            File.WriteAllLines("C:\\OnlineScanner\\test.txt", paths);

            foreach (string line in File.ReadLines("C:\\test.txt"))
            {
                //Define what file you want to analyse
                FileInfo fileInfo = new FileInfo(line);

                //Check if the file has been scanned before.
                FileReport fileReport = virusTotal.GetFileReport(fileInfo);

                bool hasFileBeenScannedBefore = fileReport.ResponseCode == ReportResponseCode.Present;

                //If the file has been scanned before, the results are embedded inside the report.
                if (hasFileBeenScannedBefore)
                {
                    int detekce = fileReport.Positives;

                    if (detekce >= 1)
                    {
                        using (var writer = new StreamWriter("C:\\OnlineScanner\\OnlineScannerLog.txt"))
                        {
                            writer.WriteLine(line);
                            writer.WriteLine("URL to test: " + fileReport.Permalink);
                            writer.WriteLine("Detect ratio: " + fileReport.Positives + "/54");
                            writer.WriteLine("Message: " + fileReport.VerboseMsg);
                            writer.WriteLine();
                            writer.WriteLine();
                        }
                    }
                    System.Threading.Thread.Sleep(16000);
                }
                else
                {
                    ScanResult fileResult = virusTotal.ScanFile(fileInfo);
                    int detekce = fileReport.Positives;

                    if (detekce >= 1)
                    {
                        using (var writer = new StreamWriter("C:\\OnlineScanner\\OnlineScannerLog.txt"))
                        {
                            writer.WriteLine(line);
                            writer.WriteLine("URL to test: " + fileReport.Permalink);
                            writer.WriteLine("Detect ratio: " + fileReport.Positives + "/54");
                            writer.WriteLine("Message: " + fileReport.VerboseMsg);
                            writer.WriteLine();
                            writer.WriteLine();
                        }
                    }
                    System.Threading.Thread.Sleep(16000);
                }
            }
        }

        private static IEnumerable<string> Traverse(string rootDirectory)
        {
            IEnumerable<string> files = Enumerable.Empty<string>();
            IEnumerable<string> directories = Enumerable.Empty<string>();
            try
            {
                // The test for UnauthorizedAccessException.
                var permission = new FileIOPermission(FileIOPermissionAccess.PathDiscovery, rootDirectory);
                permission.Demand();

                files = Directory.GetFiles(rootDirectory);
                directories = Directory.GetDirectories(rootDirectory);
            }
            catch
            {
                // Ignore folder (access denied).
                rootDirectory = null;
            }

            foreach (var file in files)
            {
                yield return file;
            }

            // Recursive call for SelectMany.
            var subdirectoryItems = directories.SelectMany(Traverse);
            foreach (var result in subdirectoryItems)
            {
                yield return result;
            }
        }

    }
}

此代码运行一段时间(大约15秒)但程序崩溃。

错误是

  

System.IO.IOException,进程无法访问文件C:\ hiberfil.sys。   http://upnisito.cz/images/2016_12/319crasherrror.png

你知道如何解决它吗?

0 个答案:

没有答案