2013-03-21 3 views
1

Am I doing it right while reading a file?iCal 파일을 만드는 동안 처분하지 않는 것은 무엇입니까? 내가 바로 일을하거나 오류가 같은 것을 처리 할 필요가 있어요 경우 내 질문을 참조에서

나는

System.IO.IOException: The process cannot access the file 'C:\doc.ics' 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) 

코드 내가하는 iCal의 파일을 만들려면이 코드를 사용하지만 확신 오류가 StreamWriter 생성자에서 어떤 일이 일어나고으로

String filePath = paramUri.LocalPath.ToString().TrimStart("filePath:///".ToCharArray()); 
    int lastSlash = filePath.LastIndexOf('\\'); 
    if(lastSlash > 0) 
    { 
     String folderPath = filePath.Substring(0, lastSlash); 
     DirectoryInfo di = new DirectoryInfo(folderPath); 
     if(!di.Exists) 
      di.Create(); 
    } 

    //Use a streamwriter to create a file in the location specified in the URI 
    using (StreamWriter sw = new StreamWriter(filePath)) 
    { 
     //Autoflush enabled so we don't have to worry about closing the streamwriter 
     sw.AutoFlush = true; 
     sw.WriteLine("BEGIN:VCALENDAR"); 
     sw.WriteLine("PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN"); 
     sw.WriteLine("VERSION:2.0"); 
     sw.WriteLine("METHOD:PUBLISH"); 
     sw.WriteLine("X-MS-OLK-FORCEINSPECTOROPEN:TRUE"); 
     //I am adding more details but they are not relevant to post 
    } 

답변

0

, IS 사용하고, 파일은 아마 당신이 포함 한 코드에 의해 잠겨되지 않습니다.

나는 그것이 다른 프로세스에 의해 잠겨 있다고 추측 할 수 있습니다. 아마도 텍스트 편집기에서 열어 본 것입니까?

+0

내 첫 번째 질문에서 무엇을 요청했는지 구현하면 문제가 해결되었습니다. :) – Mathematics