ボタンをクリックした後にtxtファイルに情報を記録する必要があるac#コードがあります。ファイルは作成されますが、writeメソッドは例外エラーをスローします:
書き込み方法:
public static void writelogfile(string text)
{
StreamWriter sw = logfile.AppendText();
sw.WriteLine(text);
sw.Close();
}
ファイル作成方法:
string filename = "loging-" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".txt";
logfile = new FileInfo("D:Log\\" + filename); //for QA
logfile.Create();
情報をログに記録するメソッドで使用される実際の行:
writelogfile("userid: " + id);
別のプロセスによって使用されているため、プロセスはファイル'D:test.txt'にアクセスできません。System.IO .__ Error.WinIOError(Int32 errorCode、String mayFullPath)at System.IO.FileStream.Init(String path、FileMode mode、FileAccess access、Int32 rights、Boolean useRights、FileShare share、Int32 bufferSize、FileOptions options、SECURITY_ATTRIBUTES secAttrs 、String msgPath、Boolean bFromProxy)at System.IO.FileStream..ctor(String path、FileMode mode、FileAccess access、FileShare share、Int32 bufferSize、FileOptions options、String msgPath、Boolean bFromProxy)at System.IO.FileStream..ctor (String path、FileMode mode、FileAccess access、FileShare share、Int32 bufferSize、FileOptions options)at System.IO.StreamWriter..ctor(String path、Boolean append、Encoding encoding、Int32 bufferSize)atSystem.IO.StreamWriter。。DisplayClass1.b _0()atMicrosoft.SharePoint.SPSecurity。<>c_ DisplayClass4.b _2()at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode、Object param )Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)at Blocked_listupdate.VisualWebPart1.VisualWebPart1UserControl.btnupdate_Click(Object sender、EventArgs e)