我有 ac# 代码,我需要在单击按钮后将信息记录在 txt 文件中。文件被创建,但 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 maybeFullPath) 在 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 System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) at System.IO.StreamWriter.. (String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)DisplayClass1.b _0() 在 Microsoft.SharePoint.SPSecurity.<>c_ DisplayClass4.b _2() 在 Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) 在 Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param ) 在 Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) 在 Blocked_listupdate.VisualWebPart1.VisualWebPart1UserControl.btnupdate_Click(Object sender, EventArgs e)