PDA

View Full Version : گرفتن لیست درایو ها و انتخاب یکی از آنها بطور رندوم



shaghayegh_ir
پنج شنبه 15 تیر 1391, 20:38 عصر
سلام
چطوری میتونم لیست درایوهام رو بگیرم و بطور رندوم یکیشون رو که آماده است انتخاب کنم و توی یه متغییر رشته ای قرار بدم ؟
driveinfo.getdrivers()
این باید توی یه آرایه ای از نوع DriveInfo قرار بگیره
بعدش یه متغییر از نوع رندوم تعریف میکنم
Random Disk= new random()
حالا چطوری از اون آرایه ای که درایوهام توشه یه رندوم بگیرم

از کجا بدونم اون درایو محلی هست (یعنی فقط دیسک سختم رو میخوام )

آیا متغییر رندوم به رشته ای تبدیل میشه ؟

tooraj_azizi_1035
پنج شنبه 15 تیر 1391, 20:56 عصر
سلام


using System;
using System.IO;

class Test
{
public static void Main()
{
DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);

Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);

Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
}
}
/*
This code produces output similar to the following:

Drive A:\
File type: Removable
Drive C:\
File type: Fixed
Volume label:
File system: FAT32
Available space to current user: 4770430976 bytes
Total available space: 4770430976 bytes
Total size of drive: 10731683840 bytes
Drive D:\
File type: Fixed
Volume label:
File system: NTFS
Available space to current user: 15114977280 bytes
Total available space: 15114977280 bytes
Total size of drive: 25958948864 bytes
Drive E:\
File type: CDRom

The actual output of this code will vary based on machine and the permissions
granted to the user executing it.
*/

shaghayegh_ir
پنج شنبه 15 تیر 1391, 21:05 عصر
مرسی ولی من میخوام از بین لیست درایوها یکی رو به طور تصادفی انتخاب کنه
نه اینکه همه رو چک کنه اگه آماده بودند مشخصاتشون رو انتخاب کنه

یعنی یه این ترتیب :
گرفتن لیست درایوها
انتخاب یکی از آنها به طور تصادفی
چک کردن درایو انتخاب شده به طور تصادفی که اگر آماده است اون درایو رو توی یه متغییر رشته ای قرار بده

مرسی

majidrezaei2007
جمعه 16 تیر 1391, 00:11 صبح
1- لیست درایو ها رو میگیریم و داخل یک آرایه میریزیم
2- یک عدد تصادفی به عنوان اندیس تولید می کنیم (اندیس آرایه ای که لیست درایو ها داخلش هستند)
3- اگر درایو آماده نبود یا اینکه دیسک سخت نبود (شرط داخل while) دوباره یک عدد تصادفی تولید کن (برو به 2)


DriveInfo[] allDrives = DriveInfo.GetDrives();
Random R = new Random();
int index = 0;
do
{
index = R.Next(0, allDrives.Length);
} while (!allDrives[index].IsReady || allDrives[index].DriveType != DriveType.Fixed);
Console.WriteLine("Drive {0}", allDrives[index].Name);
Console.WriteLine("File type: {0}", allDrives[index].DriveType);
Console.WriteLine("Volume label: {0}", allDrives[index].VolumeLabel);
Console.WriteLine("File system: {0}", allDrives[index].DriveFormat);
Console.WriteLine("Available space to current user:{0, 15} bytes", allDrives[index].AvailableFreeSpace);
Console.WriteLine("Total available space: {0, 15} bytes", allDrives[index].TotalFreeSpace);
Console.WriteLine("Total size of drive: {0, 15} bytes ", allDrives[index].TotalSize);