2个回答
展开全部
用下面的方法既可以实现
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
string[] fileArr = Directory.GetFiles("E:\\AAA");//文件必须不少于十个
int[] fileIndex = new int[10];
for (int i = 0; i < 10; i++)
{
Random random = new Random();
fileIndex[i] = random.Next(0, fileArr.Length);
for (int j = 0; j < i; j++)
{
if (fileIndex[i] == fileIndex[j])
{
i--;
}
}
}
long fileCount = 0;
for (int i = 0; i < 10; i++)
{
FileStream fileStream = new FileStream(fileArr[fileIndex[i]], FileMode.Open);
fileCount += fileStream.Length;
fileStream.Close();
}
fileCount = fileCount / 1024;
MessageBox.Show("文件总共 " + fileCount.ToString() + "K");
}
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
string[] fileArr = Directory.GetFiles("E:\\AAA");//文件必须不少于十个
int[] fileIndex = new int[10];
for (int i = 0; i < 10; i++)
{
Random random = new Random();
fileIndex[i] = random.Next(0, fileArr.Length);
for (int j = 0; j < i; j++)
{
if (fileIndex[i] == fileIndex[j])
{
i--;
}
}
}
long fileCount = 0;
for (int i = 0; i < 10; i++)
{
FileStream fileStream = new FileStream(fileArr[fileIndex[i]], FileMode.Open);
fileCount += fileStream.Length;
fileStream.Close();
}
fileCount = fileCount / 1024;
MessageBox.Show("文件总共 " + fileCount.ToString() + "K");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询