sqr*_*sqr 3 c# io file streamwriter
我有2个程序:一个C#GUI应用程序和一个访问同一文本文件的C#windows服务;
a) the C# GUI application will write/append to the text file
b) the windows service will copy the file to a network location every 20 mins.
Run Code Online (Sandbox Code Playgroud)
当动作同时发生时,我收到如下错误消息:
2014/09/08 21:15:56 mscorlib
The process cannot access the file 'C:\09082014.log' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
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)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at DataloggerUI.DataHelper.WriteDataLog(String msg, Int64& downTimeSince)
at DataloggerUI.Form1.ReceiveData(IAsyncResult asyncResult)
Run Code Online (Sandbox Code Playgroud)
---- C#windows服务部分如下所示----------
if (File.Exists(destination + @"\" + fi.Name))
{
FileInfo fi_dest = new FileInfo(destination + @"\" + fi.Name);
if (fi.LastWriteTime > fi_dest.LastWriteTime)
{
File.Copy(fi.FullName, destination + @"\" + fi.Name, true);
WriteLog("Send " + fi.FullName + " to server");
}
}
else
{
File.Copy(fi.FullName, destination + @"\" + fi.Name, true);
WriteLog("Send " + fi.FullName + " to server");
}
}
Run Code Online (Sandbox Code Playgroud)
------- C#windows GUI应用程序代码如下-------
string logfile = DataHelper.GetAppConfigString("MPRS_LogDir") + @"\" + DateTime.Now.ToString("MMddyyyy") + ".log";
using (StreamWriter sw = new StreamWriter(logfile, true))
{
sw.WriteLine(tick + " " + "KYEC" + Environment.MachineName + " " + msg);
sw.Close();
}
Run Code Online (Sandbox Code Playgroud)
GUI应用程序抛出错误消息.我的代码中是否有任何错误或不良做法?
------------按照彼得的建议将代码修改为以下内容--------------
try
{
using (StreamWriter sw = new StreamWriter(logfile, true))
{
sw.WriteLine(tick + " " + "KYEC" + Environment.MachineName + " " + msg);
}
}
catch (IOException ex)
{
WriteErrorLog("IOException " + ex.Message);
System.Threading.Thread.Sleep(2000); //2 secs
using (StreamWriter sw = new StreamWriter(logfile, true))
{
sw.WriteLine(tick + " " + "KYEC" + Environment.MachineName + " " + msg);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在共享读写模式下使用FileStream同时写入和复制文件.
请尝试以下代码:
//To write file use
using (FileStream fs = new FileStream(fileToReadPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using (StreamWriter StreamWriter = new StreamWriter(fs))
{
StreamWriter.WriteLine(tick + " " + "KYEC" + Environment.MachineName + " " + msg);
StreamWriter.Close();
}
}
//To copy file use
using (FileStream inStream = new FileStream(fileToReadPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (FileStream outStream = File.Create(fileToWritePath))
{
while (inStream.Position < inStream.Length)
{
outStream.WriteByte((byte)inStream.ReadByte());
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以在不使用任何其他进程错误的情况下完成任务.