2012-04-10 70 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