1

我对 DriveInfo 类有这个小问题。我知道该错误特定于“IsReady”属性,但我只是不知道如何定义它..

namespace Csp.Test.ConsoleApp
{
    public class Program
    {
        public static void Main()
        {
            //Create the server object - You will need create a list of the server objects.
            Server server = new Server();

            //Get all drives information
            List<DriveInfo> driveList = DriveInfo.GetDrives().ToList<DriveInfo>();

            //Insert information of one server - You will need get information of all servers
            server.ServerID = 0; //Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK.
            server.ServerName = string.Concat("Server ", driveList.Count);

            //Inserts information in the newServers object
            for (int i = 0; i < driveList.Count; i++)
            {
                ServerDrive serverDrives = new ServerDrive();

                //Put here all the information to obeject Server                
                serverDrives.DriveLabel = driveList[i].Name;
                serverDrives.TotalSpace = driveList[i].TotalSize;
                serverDrives.DriveLetter = driveList[i].VolumeLabel;
                serverDrives.FreeSpace = driveList[i].TotalFreeSpace;

                //      server.ListServerDrives.Add(serverDrives);
                server.ServerDrives.Add(serverDrives);
            }

            //Add the information to an SQL Database using Linq.
            DataClasses1DataContext db = new DataClasses1DataContext(@"sqlserver");
            //   db.Servers.InsertAllOnSubmit(server);
            db.Servers.InsertOnSubmit(server);
            db.SubmitChanges();
        }

任何帮助将不胜感激。

4

1 回答 1

3

更改以下行:

List<DriveInfo> driveList = DriveInfo.GetDrives().Where(x=>x.IsReady).ToList();

请注意,如果在获取驱动器列表和查询 DriveInfo 之间驱动器状态发生变化,您仍然可以获得 IOException,因此最好在访问 DriveInfo 时使用 try-catch。

于 2012-08-07T08:52:35.030 回答