2012-04-10 28 views
-3

我有一個使用Drive.Info的小應用程序。我想做兩件事。檢查計算機上是否存在某個驅動器,如果它存在並且todaysdate不是存儲在文本文件中的許多日期之一,則運行一個小型應用程序。如果今天的日期是在文本文件中讀取的,則不要執行任何操作。我有一堆代碼工作,但與DateTime對象有問題。任何人都可以看看我有什麼,並建議我需要重組嗎?所有的邏輯都在那裏,我只是沒有把它放在一起。存儲在txt文件,我從閱讀比較DateTime for .NET應用程序

  1. 日期是像這樣每個 行:25/12/2010。
  2. 在catch語句中,「Console.WriteLine(e.Message);」是什麼在生成「字符串未被識別爲有效的日期時間」問題。
  3. 我希望的目標是:如果在文本文件中找不到今天的日期&在「(d.Name.Contains(」C「))」行中指定的驅動器存在於當前機器上,請運行calc.exe。
  4. 如果在文本文件中找到了今天的日期,則什麼也不做。

我的問題是:如何修改我的應用程序的結構,以便我可以:將日期成功與存儲在txt文件中的日期進行比較。其次,調整我的邏輯,以便我可以實現上面的部分。

道歉需要編輯,我應該更清楚我的第一篇文章。 謝謝。

編輯:夥計們我更新了下面的代碼。它現在似乎在工作。感謝您的幫助,並再次爲第一個問題提出錯誤的建議。然而,應用程序的行爲現在正在按照需要工作。該捕獲仍然被擊中(當今日期不在文件中,並且指定的驅動器不存在時)理解爲什麼會發生這將是很好的事情?

public static void Main() 
/* Goal of this application: Read a text file filled with public holiday dates  formatted as: 25/12/2011 
* Compare these to today's date. If not a match, run calc.exe ASSUMING THE SPECIFIED DRIVE ON LINE 78 
* IS FOUND ON THE COMPUTER. If the date matches, do nothing. 
*/ 
{ 
    Process Calculator = new Process(); 
    Calculator.StartInfo.FileName = "calc.exe"; 
    Calculator.StartInfo.Arguments = "ProcessStart.cs"; 
    DriveInfo[] allDrives = DriveInfo.GetDrives(); 

    // Create a StreamReader to read from file. 
    StreamReader sr = new StreamReader("file.txt"); 

     String DateFromFile; 
     DateTime todaysDate = DateTime.Today; 

     try 
     {    
      // Read and display lines from the file until the eof is reached. 
      while ((DateFromFile = sr.ReadLine()) != null) 
      { 
       Console.WriteLine(DateFromFile); 
       DateTime dt = Convert.ToDateTime(DateFromFile); 


       if (dt == todaysDate) 
       { 
        Console.WriteLine("File.text has todays date inside! Not gonna run calc.exe"); 
        Environment.Exit(0); 

       }//end if 

       else 
       { 
       }//end else 


      }//end while 
     }//end try 

     catch (Exception e) 
     { 
      // Let the user know what went wrong. 
      Console.WriteLine("The file.txt could not be read"); 
      Console.WriteLine(e.Message); 
     } 

     ////////// DO THE REST /////////// 
    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); 
      }//end if 
      if (d.Name.Contains("T")) 
      { 
       Console.WriteLine("\n"); 
       Console.WriteLine("** SUCCESS - LETTER FOUND **\n\n ** RUN CALC.EXE **"); 
       Console.WriteLine("\n"); 
       Calculator.Start(); 
      }//end if 
      else 
      { 
       Console.WriteLine("** LETTER NOT FOUND **"); 
       Console.WriteLine("\n"); 
      }//end else 
     }//end for 


}//end main 
}//end class 
+0

文本文件上的日期格式是什麼? – 2012-04-10 09:17:21

+0

*但是遇到了DateTime對象*的問題,您可以擴展它嗎 – V4Vendetta 2012-04-10 09:17:36

+0

您對DateTime objecj有什麼困難?可以提供更多的細節。 – 2012-04-10 09:18:49

回答

0

我認爲你必須從開始到特定日期格式獲取當前日期。

例如,使用

String todaysDate = DateTime.Now.ToShortDateString(); 

當你比較也將字符串轉換爲這種格式和比較

String dt = DateTime.Parse(DateFromFile).ToShortDateString(); 
+0

ToShortString創建一個字符串,而不是DateTime – 2012-04-10 09:28:54

+0

Adrian Iftode,thnx只是忘記改變 – las 2012-04-10 09:30:19

+0

然後你比較字符串? – 2012-04-10 09:31:02

2

當比較需要注意兩件事日期和字符串:

  1. 確保您'正在使用正確的數據類型
  2. 確保您使用正確的格式。

所以如果你想比較DateTime和字符串日期,你想要做的第一件事就是將字符串轉換爲DateTime。

做到這一點,最好的和最可靠的方法是知道字符串前期的格式和使用parseExact這樣的:

string myDateTimeString = "03/04/2012"; // Notice month and day are ambiguous! 
string format = "dd/MM/yyyy"; 
DateTime dateTime = DateTime.ParseExact(myDateTimeString, format, 
     CultureInfo.InvariantCulture); 

它使用的CultureInfo超載太是很重要的。只要做到這一點,你的代碼就會更可靠。

現在你有一個日期時間,你可以比較,但我不會用平等的運營商基礎對象,而不是我這樣做:

if (myDate.Date == DateTime.Today) 
{ 
    //Occurs on same day! 

} 

或在您的情況:

if (myDate.Date == DateFromTextFile.Date) 
{ 
    //Condition met 
} 
+0

很好的答案,謝謝。我改變了我的代碼來使用它,因爲它反映了良好的做法。 :) – GrumP 2012-04-10 10:12:38