System.IO.IOException:"该文件存在"使用System.IO.Path.GetTempFileName()的时候 - 的决议?决议、该文件、存在、时候

2023-09-04 06:59:28 作者:别急我来送纸

我的一个客户了,每当他试图用我的产品异常。予得到的所发生的异常的调用堆栈,其顶部是:

One of my customers got an exception whenever he tried to use my product. I obtained the callstack of the exception that had occurred, the top of which is:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.__Error.WinIOError()
   at System.IO.Path.GetTempFileName()
   at System.Windows.Input.Cursor.LoadFromStream(Stream cursorStream)
   at System.Windows.Input.Cursor..ctor(Stream cursorStream)

谷歌搜索,我找到了很多的blog帖子说明这个异常被抛出时,有在%TEMP%文件夹中超过65535临时文件,而该解决方案是简单地清除掉旧的临时文件。我可以问客户要做到这一点,但是这可能只是一个暂时的解决方案 - 如果他们经常运行一些其他的软件,将频繁调用GetTempFileName,这会使问题重新出现遍地

Googling this, I found plenty of blog posts stating this exception is thrown when there are more than 65535 temp files in the %TEMP% folder, and that the solution is to simply clear out the old temp files. I can ask the customer to do that, but this might only be a temporary solution - what if they are regularly running some other piece of software that makes frequent calls to GetTempFileName, which will make the problem reoccur over and over?

我不能只以编程方式明确了%TEMP%文件夹中,因为这可能会以某种方式损害别的东西,我不能避免调用GetTempFileName(并用自己的临时文件夹代替),因为它不是我,但WPF code表示在呼唤它。

I can't just programmatically clear out the %TEMP% folder, as that might somehow damage something else, and I can't avoid calling GetTempFileName (and using my own temp folder instead) as it's not me but WPF code that's calling it.

有没有永久的解决方案呢?

Is there any permanent solution for this?

更新:我已经证实,其中%TEMP%文件夹中充满着日志文件的问题不是由我自己的code引起的,而必须由其他第三方造成的应用程序客户机上。我也看了成 Cursor.LoadFromStream 的实施,它肯定是没有错 - 它生成一个临时文件,但随后删除它最后块。

UPDATE: I've confirmed that the problem where the %TEMP% folder is overflowing with log files is not caused by my own code, and must be caused by some other 3rd party application on the customer's machine. I also looked into the implementation of Cursor.LoadFromStream and it surely isn't at fault - it generates a temp file, but then deletes it in finally block.

推荐答案

我在最后的评论中提到,我认为你的唯一安全的方式做到这一点是询问用户是否希望你删除的文件,然后再试一次。它的必须的,你得到的用户输入这个,这个事情是这样的,在他们自己的危险。在我的头上了类似的东西来。

As I mentioned in my last comment I think your only safe way to do this is to ask the user if they want you to delete files and try again. It is imperative that you get the users input into this, this way it is at their own peril. In my head its something similar to.

public Stream GetStream(Stream cursorStream)
{
    try
    {
       //getting stream
    }
    catch(IOE)
    {
        MessageBox.Show(this, "Unable to get stream, your temporary
                              folder may be full, do you want to try deleting 
                                some and try again?");
         if(yes)
         try
         {
             //delete and try again
             return GetStream(cursorStream);
         }
         catch(IOE)
          {
                //no luck
           }
          else
              return null;
    }

}

这是可选的检查,以确保可能是,

An optional check to make sure could be,

Directory.EnumerateFiles(Path.GetTempPath(), "*", SearchOption.TopLevelOnly)
  .Count() == ushort.MaxValue;