2012-08-07 2 views
1

이 작은 문제가 DriveInfo 클래스에 있습니다. C# - 드라이브가 준비되지 않았습니다. (DriveInfo)

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

어떤 도움

크게 감사하겠습니다 .. 나는 오류가 "isReady 상태"속성에 고유 한 것을 알고 있지만 난 그냥 그것을 정의하는 방법을 모르겠어요.

+0

당신의 "작은 문제"는 무엇입니까? 코드에서 예상되는 동작이나 실제로 * 수행하는 동작을 설명하지 않았습니다. 또한, 왜 코드가 C# 인 것처럼 보일 때이 태그에 "c"가 표시됩니까? –

+0

제목에 "드라이브가 준비되지 않았습니다"라는 메시지가 표시되면 일반 게시물 상단에 "IsReady"가 강조 표시됩니다 드라이브가 준비되지 않았기 때문에 코드가 실행되지 않습니다 " IsReady "라고 부릅니다. – Ghostyy

+0

@Ghostyy 플로피 및 광학 드라이브와 같은 이동식 미디어가있는 드라이브에서이 문제가 발생할 것으로 생각됩니다. 각 서버에서이 코드를 실행할 계획입니까? – Jodrell

답변

3

변경 다음 줄이 가장 좋습니다, 드라이브 목록을 가져오고 DriveInfo를 쿼리 사이의 드라이브 상태가 변경 당신이 시도 - 캐치 할 때를 사용하는 경우 당신은 여전히 ​​IOException를 얻을 수

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

주 DriveInfo에 액세스합니다.

+0

Ahh 건배 : – Ghostyy

+0

드라이브를 "깨우는"방법이 있습니까? Windows 8.1에서는 백업 하드 드라이브를 일종의 수면 모드에 둡니다. 그것은 Windows 탐색기에서 볼 수 있지만 처음 액세스하면로드가 들립니다. C#에서 내 드라이브는 "IsReady = false"입니다. – juFo

+1

@juFo 시도하지는 않았지만 드라이브에 액세스하는 작은 도우미 메소드 (예 : WakeUpDrive)를 작성할 수 있습니다 (빈 텍스트 파일을 드라이브에 직접 기록하고 즉시 삭제할 수 있음). 너는 try-catch에서 이것을 할 것이다. –

관련 문제