2 개의 프로그램 : C# GUI 응용 프로그램과 동일한 텍스트 파일에 액세스하는 C# Windows 서비스.이 프로세스는 스트림 작성기를 사용하는 다른 프로세스에서 사용 중이기 때문에 파일에 액세스 할 수 없습니다.
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)
----는 C# 윈도우 서비스 부분 ----------
다음과 같다 : 작업이 동시에 발생하는 경우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.
, 나는 오류 메시지가 아래와 같이 있어요
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");
}
}
-------는 C# 윈도우 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();
}
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);
}
}
windows-service는 파일 복사 시작시기를 어떻게 결정합니까? 절차를 설명하거나 더 많은 코드를 보여주십시오. –
오류는 파일이 사용 중임을 나타냅니다. 그 것이었다? 아마 GUI가 다른 것을 추가하고 싶을 때 복사되고있을 것입니다. – luk32
안녕하세요 luk32 예, Windows 서비스가 어딘가에 복사하려고 시도하는 동안 GUI가 파일에 추가 할 때 오류가 발생했습니다. 어떻게 든이 예외를 피할 수 있습니까? 파일을 추가하기 전에 사용 중인지 여부를 감지하는 것과 같습니까? 또한 C# 'using'문에 의해 예외가 이미 처리됩니까? – sqr