私は次のような方法を持っています
private string sLogFormat;
private string sErrorTime;
public void CreateLogFile(string path, string msg)
{
//sLogFormat used to create log files format :
// dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
//this variable used to create log filename format "
//for example filename : ErrorLogYYYYMMDD
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear + sMonth + sDay;
StreamWriter sw = new StreamWriter(path + sErrorTime, true);
sw.WriteLine(sLogFormat + msg);
sw.Flush();
sw.Close();
}
ここで Onchanged ハンドラーをトリガーする別のメソッド run() (フォルダーの内容がより正確に変更されたときにトリガーされます)
public static void Run()
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:/model_RCCMREC";
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// watch wav files.
watcher.Filter = "*.wav";
// Add event handlers.
watcher.Created += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
および OnChanged ハンドラーを次のように
public static void OnChanged(object source, FileSystemEventArgs e)
{
try
{
//access last folder(newly created one is suposedly at last position)
var directory = new DirectoryInfo("C:/model_RCCMREC");
var myFile = (from f in directory.GetFiles() orderby f.LastWriteTime descending select f).First();
//2.Split the file name.
}
catch
{
//I WANT TO ACCESS THE METHOD CreateLogFile(x,y) HERE
}
}
OnChanged ハンドラから CreateLogFile() メソッドに正常にアクセスできません。どうすればアクセスできますか?