1

頭がおかしくなる質問があります。エラーメッセージをオブジェクトの文字列に保存し、unloadContent() で文字列をファイルに書き込むプログラムがあります。何らかの理由で、サポートされていない例外が発生し続けます。unloadContent() のコードは次のとおりです。

        if (debug.getContent().Length > 0)
        {
            saveErrors save = new saveErrors();
            if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\Errors")) ;
                Directory.CreateDirectory(System.IO.Directory.GetCurrentDirectory() + "\\Errors");
            save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + (System.DateTime.Now.ToString().Replace("/", "_")).Replace(" ","") + ".txt");
        }

クラス保存エラーのコードは次のとおりです。

    public class saveErrors
    {
        private string mess = debug.getContent();

        public void save(string fileName)
        {
            Debug.WriteLine(fileName);
            using (StreamWriter sw = new StreamWriter(fileName))
            {
                sw.Write(mess);
                sw.Close();
            }
        }
    }

私はまだC#に少し慣れていないので、どんな助けでも大歓迎です!

ありがとう!

4

2 に答える 2

3

これを試して:

[Test]
public void SaveTextTest()
{
    string relativePath=@"Errors\errorLog_";
    string directoryPath = System.IO.Path.Combine( System.IO.Directory.GetCurrentDirectory() , relativePath);
    var directoryInfo = new DirectoryInfo(directoryPath);
    if(directoryInfo.Exists==false)
        directoryInfo.Create();
    string fileName = System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".txt";
    string path = System.IO.Path.Combine(directoryPath, fileName);
    string textToSave = "This will be saved";
    File.WriteAllText(path, textToSave);       
}

DateTime.ToString()を目的の形式で取得するには、 formatstringを渡すことができます

于 2012-10-06T23:40:09.153 に答える
1

save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + (System.DateTime.Now.ToString().Replace("/", "_")).Replace(" ", "").Replace(":", "") + ".txt");

それに変えてください。.Replace(":", "")コードの日付部分に含まれていますが、ファイル名では無効であるため、 が必要:です。そのため、削除するか、別のものに置き換える必要があります。

別の方法として、次のように日付をフォーマットすることもできます。

save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss"));

于 2012-10-06T23:29:17.517 に答える