C#ASP.NET StreamWriter无法正确释放文件 [英] C# ASP.NET StreamWriter not properly releasing file

查看:185
本文介绍了C#ASP.NET StreamWriter无法正确释放文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im''使用XP Pro和Visual Web Developer 2010 Express Edition

如果不存在文件,则其工作方式与我认为的一样.如果目录中已经有一个文件,则说明该文件正在被另一个进程使用,因此将无法打开(在调试模式下,我尚未部署它).我在MSDN和其他站点上进行了查找,除非忽略了某些内容,否则我完全可以得到他们的建议.我试过添加sw.Close();和sw.Dispose();我必须先关闭IDE,然后XP才能允许我删除该文件,即使我尚未成功打开它也是如此.这是代码.

Im'' using XP Pro and Visual Web Developer 2010 Express Edition

If no file exists, it works just like I think it should. If there is a file already in the directory it says that the file is in use by another process and therefore will not open (in debug mode, I haven''t deployed it yet). I looked it up on MSDN and other sites and unless I am overlooking something I have exactly what they recommend. I have tried adding sw.Close(); and sw.Dispose(); I have to close the IDE before XP will allow me to delete the file even if I haven''t successfully opened it. Here is the code.

private void storeComments(string week, string month, string year)
  {
      string signatureFile;
      if (month == DateTime.Now.Month.ToString() && year == DateTime.Now.Year.ToString())
      {
          string signatureFile = "C:\\Remote\\Scripts\\Logs\\SignatureLog.log";

      }
      else
      {
          signatureFile = "C:\\Remote\\Scripts\\Logs\\Archives\\SignatureLog" + month + year + ".log";
      }

      string log = week + "," + dateLabel.Text + " " + signerDropDownList.SelectedItem.Text;
      using (StreamWriter sw = new StreamWriter(signatureFile))
      {
          sw.WriteLine(log);
      }
  }



感谢您的帮助.



Thanks for your help.

推荐答案

我使用StreamWriter的程度不高,因此无法确定我在这里所说的话. :)

你试过了吗
I don''t use StreamWriter much and so cannot be definite about anything I might say here. :)

Have you tried
using (StreamWriter sw = new StreamWriter(signatureFile))
{
    sw.WriteLine(log);
}
 // append another line
using (StreamWriter sw = new StreamWriter(signatureFile, true))
{
    sw.WriteLine("Testing append");
}



对于不存在的文件.

如果可行,至少对我来说,这表明没有锁定".

[编辑-在n4faa发表评论后]

确定,尝试此操作,这将显式设置文件权限.



for a file that doesn''t exist.

Which, if it works would seem to indicate, to me at least, that there is no ''locking'' going on.

[Edit - after n4faa''s comment]

OK try this, which will explicitly set the file permissions.

using (FileStream fs = new FileStream(signatureFile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
    using (StreamWriter sw = new StreamWriter(fs))
    {
        sw.WriteLine(log);
    }
}


[/Edit]


[/Edit]


在您的情况下,我不确定下一个请求进入时系统句柄是否已经关闭.

只是我的两分钱:我永远不会让网页访问没有文件保护的共享资源(如文件).将对共享资源的操作封装在一个类中,每个资源只能存在一个实例.

仅通过包装对共享资源进行所有修改.像这样包装您的资源,也可以通过创建一个使用多个线程的包装程序的小程序来进行测试.

最好的问候,
曼弗雷德(Manfred)
In your case I''m not sure that the system handle will be already closed when the next request is rolling in.

Just my two cents: I''d never have a web page access a shared resource like a file with out any guards. Encapsulate your operations on the shared resource in a class where only one instance per resorce can exist.

Make all your modifications to the shared resource only through the wrapper. Wrapping your resource like that it''s also easy to test by creating a small program that uses the wrapper from several threads.

Best Regards,
Manfred


我不知道亨利的答案是否有用,但主要是使用using(){}语句作为答案或将文件共享模式更改为可读写.

因此,假设他们没有工作;

如果您有在计算机上运行的防病毒pprogram,请先将其关闭,然后再尝试看看会发生什么.还有另一种选择;您可能无法正确关闭导致该错误的应用程序. 该进程无法访问该文件,因为该文件正在被另一个进程使用."(主要是编码错误)请确保关闭应用程序后,该应用程序未在任务管理器中显示.
I dont know if Henry''s answer helped or not but mostly using the using () {} statement is the answer or changing the fileshare mode to readwrite.

So assuming that they are not working;

If you have an antivirus pprogram that running on your computer go ahead and turn it off and then give it a try and see what happens. Also another option that; you may not properly shut the app down which is causing that error. "The process cannot access the file because it is being used by another process".(Mostly coding error) Make sure that your application isn''t showing up in your Task Manager after shutting it down.


这篇关于C#ASP.NET StreamWriter无法正确释放文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆