【问题标题】:C# - Drive is not Ready (DriveInfo)C# - 驱动器未就绪 (DriveInfo)
【发布时间】:2012-08-04 06:02:13
【问题描述】:

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();
        }

任何帮助将不胜感激。

【问题讨论】:

  • 你的“小问题”是什么?你没有描述你的代码应该有什么行为,或者它实际上确实有什么行为。另外,当代码看起来是 C# 时,为什么这会被标记为“c”?
  • 正如标题所述,错误是“驱动器未准备好”,在一般帖子的顶部,我突出显示“IsReady”代码不会运行,因为驱动器尚未准备好我还没有知道如何在其中实现“IsReady”..
  • @Ghostyy 我怀疑这种情况发生在带有可移动介质的驱动器上,例如软盘和光驱。您是否打算在每台服务器上运行此代码?

标签: c# sql driveinfo


【解决方案1】:

更改以下行:

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

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

【讨论】:

  • 有没有办法“唤醒”驱动器? Windows 8.1 将我的备份硬盘驱动器置于一种睡眠模式。它在 Windows 资源管理器中可见,但当您第一次访问它时,您会听到它正在加载。在 C# 中,我的驱动器是“IsReady=false”。
  • @juFo 我没试过这个,但是你可能可以写一个访问驱动器的小辅助方法(例如WakeUpDrive)(也许将一个空的文本文件写入驱动器并立即删除它)。您可以在 try-catch 中执行此操作。
猜你喜欢
  • 2020-04-30
  • 2022-12-03
  • 2021-07-10
  • 2012-01-20
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2021-01-21
  • 2020-09-22
相关资源
最近更新 更多