C#文件搜索/病毒扫描程序(代码不起作用) [英] C# File Search/Virus Scanner (code not working)
问题描述
我遵循了youtube上的教程: http://www.youtube.com/watch?v= XWlUw_x4ejg& feature = related [ ^ ]
我按照所有步骤操作,并正确复制了所有代码,但是在第48行和第64行(均为第48列)中,我始终遇到错误.该错误显示数组创建必须具有数组大小或数组初始化程序",我该如何解决?这是代码:
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:
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);
}
}
}
}
}
}
我不知道我是否足够具体!请不要犹豫,询问有关代码提供的问题,尽管我真的是C#的新手.
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#
推荐答案
此错误是由于源代码引起的:
This error is due to the source code:
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.
尝试
string[] virus = new string[3] {"trojan", "virus", "hacker"};
这篇关于C#文件搜索/病毒扫描程序(代码不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!