Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I followed a tutorial on youtube: http://www.youtube.com/watch?v=XWlUw_x4ejg&feature=related[^]

I followed all the steps and copied all the code correctly but I keep getting an error in the code in Line 48 and 64 (both column 48). The error says "Array creation must have array size or array initializer", how do I fix that? Here is the code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace Commander_Uzzy_Scanner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int viruses = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            label1.Text = folderBrowserDialog1.SelectedPath;
            viruses = 0;
            label2.Text = "Viruses: " + viruses.ToString();
            progressBar1.Value = 0;
            listBox1.Items.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] search = Directory.GetFiles(@folderBrowserDialog1.SelectedPath, "*.*");
            progressBar1.Maximum = search.Length;
            foreach (string item in search)
            {
                try
                {
                    StreamReader stream = new StreamReader(item);
                    string read = stream.ReadToEnd();
                    string[] virus = new string[] ("trojan", "virus", "hacker");
                    foreach (string st in virus)
                    {
                        if (Regex.IsMatch(read, st))
                        {
                            MessageBox.Show("Virus Detected!");
                            viruses += 1;
                            label2.Text = "Viruses: " + viruses.ToString();
                            listBox1.Items.Add(item);
                        }
                        progressBar1.Increment(1);
                    }
                }
                catch
                {
                    string read = item;
                    string[] virus = new string[] ("trojan", "virus", "hacker");
                    foreach (string st in virus)
                    {
                        if (Regex.IsMatch(read, st))
                        {
                            MessageBox.Show("Virus Detected!");
                            viruses += 1;
                            label2.Text = "Viruses: " + viruses.ToString();
                            listBox1.Items.Add(item);
                        }
                        progressBar1.Increment(1);
                    }
                }
            }
        }
    }
}
I don't know if I have been specific enough! Please don't hesitate to ask questions about the code provide, although I'm really new to C#
Posted

This error is due to the source code:
C#
string[] search = Directory.GetFiles(@folderBrowserDialog1.SelectedPath, "*.*");


And the problem with this line is that you create a new array 'search' but you don't define a size. When you create an array you have to either define a size for the array, or declare its elements.

Hope, it clarifies.
 
Share this answer
 
Comments
LanFanNinja 24-Feb-12 11:07am    
No! This code is correct. See solution 2 for the correct answer.

NOTE: I did not down vote you but someone else could.
Try
string[] virus = new string[3] {"trojan", "virus", "hacker"};
 
Share this answer
 
Comments
LanFanNinja 24-Feb-12 10:59am    
+5
=== NOTE to the OP ===
On this line

string[] virus = new string[] ("trojan", "virus", "hacker");

The OP is using parentheses instead of curly braces !!

Change that line of code to this

string[] virus = new string[] { "trojan", "virus", "hacker" };

Note the use of curly braces rather that parentheses ;)

Also I see you are creating this array twice!! Once in the try block and again in the catch block?!?

While this is not really an error it is super redundant and makes no sense because it does exactly the same thing in both blocks. You should create this array outside of the try catch and then you can use it within both.

Actually I would change the code to something more like this:

string read = "";
string[] virus = new string[] { "trojan", "virus", "hacker" };
try
{
using (StreamReader stream = new StreamReader(item))
{
read = stream.ReadToEnd();
}
}
catch
{
read = item;
}

foreach (string st in virus)
{
if (Regex.IsMatch(read, st))
{
MessageBox.Show("Virus Detected!");
viruses += 1;
label2.Text = "Viruses: " + viruses.ToString();
listBox1.Items.Add(item);
}
progressBar1.Increment(1);
}
Commander Uzzy 24-Feb-12 16:42pm    
Thanks I changed the brackets and it worked!!!

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