简单的问题因为我太愚蠢了.我正在使用streamreader和writer,它给了我一个例外,即该文件已被另一个进程使用.我知道我必须设置一个.dispose()地方.但我真的不知道在哪里.我太盲目了.
这是我的代码:
protected void btn_Geht_Click(object sender, EventArgs e)
{
string sPath = @"C:\VSTO\Projects\Zeiterfassung\Zeiterfassung\obj\Debug\Zeiten.txt";
cZeile Geht = new cZeile();
using (StreamReader sr = new StreamReader(sPath))
{
Geht = cZeiterfassung.GetZeileObjectFromZeileString(sr.ReadLine(), ";");
Geht.Geht = DateTime.Now.ToString("hh:mm");
Geht.dtGeht = DateTime.Now;
sr.Dispose();
using (StreamWriter sw = new StreamWriter(sPath))
{
File.WriteAllText(sPath, string.Format("{0:yyyyMMdd_hhmm};{1};{2:dd.MM.yyyy};{3:hh:mm};{4:hh:mm}", Geht.ID, Geht.User, Geht.Datum, Geht.Kommt, Geht.Geht));
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到错误:
File.WriteAllText(sPath, string.Format("{0:yyyyMMdd_hhmm};{1};{2:dd.MM.yyyy};{3:hh:mm};{4:hh:mm}", Geht.ID, Geht.User, Geht.Datum, Geht.Kommt, Geht.Geht));
进程无法访问该文件,因为它正由另一个进程使用