【问题标题】:What am not disposing while creating a iCal file?创建 iCal 文件时没有处理什么?
【发布时间】:2013-03-21 18:53:56
【问题描述】:

参考我的问题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)

我使用的代码是,

    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
    }

【问题讨论】:

    标签: c# asp.net file .net-2.0 filestream


    【解决方案1】:

    由于StreamWriter 构造函数中发生错误,文件可能没有被您包含的代码锁定。

    我猜它被另一个进程锁定了,也许你在文本编辑器中打开它?

    【讨论】:

    • 当我实施第一个问题中提出的问题时,它解决了问题,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 2013-01-27
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多