Drive.Infoを使用した小さなアプリがあります。私は2つのことをしたいです。特定のドライブがマシンに存在することを確認し、存在し、todaysdateがテキストファイルに保存されている日付の1つではない場合は、小さなアプリケーションを実行します。今日の日付がテキストファイル内で読み取られている場合は、何もしません。たくさんのコードが機能していますが、DateTimeオブジェクトに問題があります。誰かが私が持っているものを見て、私が再構築する必要があるものをアドバイスできますか?すべての論理がそこにあります、私はそれを正しくまとめていないだけです。
- 私が読んでいるtxtファイルに保存されている日付は、各行に次のようになっています:25/12/2010。
- catchステートメント内の行「Console.WriteLine(e.Message);」「文字列が有効な日時として認識されませんでした」という問題を生成しているのはこの問題です。
- 私の希望する目標は次のとおりです。今日の日付がテキストファイルに見つからず、「(d.Name.Contains( "C"))」の行で指定されたドライブが現在のマシンに存在する場合は、calc.exeを実行します。
- 今日の日付がテキストファイルにある場合は、何もしません。
私の問題は次のとおりです。アプリケーションの構造を変更して、次のことができるようにするにはどうすればよいですか。日付をtxtファイルに保存されている日付と正常に比較します。次に、上記のパート3と4を達成できるように、ロジックを調整します。
編集が必要なことをお詫びします。最初の投稿でもっと明確にすべきでした。ありがとう。
編集:みんな私は以下のコードを更新しました。今は動いているようです。助けてくれてありがとう、そして最初の質問がうまくまとめられていなかったことをもう一度申し訳ありません。ただし、アプリケーションの動作は現在、希望どおりに機能しています。キャッチはまだヒットしています(今日の日付がファイルになく、指定されたドライブが存在しない場合)これが発生している理由を理解するとよいでしょう。
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