我有一個使用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