我有一个使用Drive.Info的小应用程序。我想做两件事。检查计算机上是否存在某个驱动器,如果它存在并且todaysdate不是存储在文本文件中的许多日期之一,则运行一个小型应用程序。如果今天的日期是在文本文件中读取的,则不要执行任何操作。我有一堆代码工作,但与DateTime对象有问题。任何人都可以看看我有什么,并建议我需要重组吗?所有的逻辑都在那里,我只是没有把它放在一起。存储在txt文件,我从阅读比较DateTime for .NET应用程序
- 日期是像这样每个 行:25/12/2010。
- 在catch语句中,“Console.WriteLine(e.Message);”是什么在生成“字符串未被识别为有效的日期时间”问题。
- 我希望的目标是:如果在文本文件中找不到今天的日期&在“(d.Name.Contains(”C“))”行中指定的驱动器存在于当前机器上,请运行calc.exe。
- 如果在文本文件中找到了今天的日期,则什么也不做。
我的问题是:如何修改我的应用程序的结构,以便我可以:将日期成功与存储在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
文本文件上的日期格式是什么? – 2012-04-10 09:17:21
*但是遇到了DateTime对象*的问题,您可以扩展它吗 – V4Vendetta 2012-04-10 09:17:36
您对DateTime objecj有什么困难?可以提供更多的细节。 – 2012-04-10 09:18:49