Click here to Skip to main content
16,005,038 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to upload a file virus scanning

What I have tried:

public void scanfile()
   {
       string c;
           bool IsValid = true;
       //foreach (FileInfo file in fileuploadExcel.Unload)
       //{
       try
       {
           //do av check here
           Process myProcess = new Process();
           FileUpload1.SaveAs(Server.MapPath("~\\uploadphoto\\") + FileUpload1.FileName);
           //address of command line virus scan exe
           myProcess.StartInfo.FileName = Server.MapPath("~\\AVG10\\avgscanx.exe");


           string path = '"' + "" + Server.MapPath("~\\uploadphoto\\") + "" + FileUpload1.FileName + "" + '"';
           string report = '"' + "" + Server.MapPath("~\\uploadphoto\\Report.txt") + "" + '"';
           string myprocarg = "/SCAN=" + path + " /REPORT=" + report;
           //" /REPORT=C:\\Upload\\Temp\\Report.txt";
           myProcess.StartInfo.Arguments = myprocarg;
           myProcess.StartInfo.UseShellExecute = false;
           myProcess.StartInfo.RedirectStandardOutput = true;

           myProcess.Start();
           myProcess.WaitForExit(); //wait for the scan to complete
                                    //add some time for report to be written to file
           int j = 0;
           int y = 0;
           for (j = 0; j <= 1000000; j++)
           {
               y = y + 1;
           }
           //Get a StreamReader class that can be used to read the file
           StreamReader objStreamReader = default(StreamReader);
           objStreamReader = File.OpenText(Server.MapPath("~\\VirusScan\\Report.txt"));
           String reportVerbose = objStreamReader.ReadToEnd().Trim();
           if (reportVerbose.Length > 0 && !reportVerbose.Contains("Found infections    :    0"))
           {
               IsValid = false;
               File.Delete(Server.MapPath("~\\VirusScan\\temp\\") + "" + FileUpload1.FileName);
           }
           objStreamReader.Close();
           if (IsValid)
           {
               try
               {
                   if (!Directory.Exists(Server.MapPath("~/DataFiles/")))
                       Directory.CreateDirectory(Server.MapPath("~/DataFiles/"));
                   if (!File.Exists(Server.MapPath("~/DataFiles/" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName))))
                   {
                       FileUpload1.SaveAs(Server.MapPath("~/DataFiles/") + FileUpload1.FileName);
                   }
                   else
                   {
                       try
                       {
                           File.Delete(Server.MapPath("~/DataFiles/" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)));
                           FileUpload1.SaveAs(Server.MapPath("~/DataFiles/") + FileUpload1.FileName);
                       }
                       catch (System.IO.IOException)
                       {

                       }
                   }
               }
               catch (System.IO.IOException)
               {

               }

           }
       }
       catch (System.IO.IOException)
       {

       }
Posted
Updated 4-Jun-18 1:00am
v2

1 solution

And what is your exact problem?

Some general tips:
  • Report errors. Catching exceptions without reporting errors does not really help. Especially during development the error messages will tell you what went wrong so that there is no need for debugging sessions in most cases.
  • Test each of your tasks separately. Start with uploading. Is the file uploaded?
  • Check your pathes: Is the AV scanner located in the web server directory Server.MapPath("~\\AVG10\\avgscanx.exe")? Probably not.
  • Check your pathes: You have set the report path to "~\\uploadphoto\\Report.txt" but trying to read the report from "~\\VirusScan\\Report.txt".
  • Check the scanning using an EICAR test file - Wikipedia[^] stored at a know location.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900