有谁知道为什么下面的代码会引发System.ArgumentException?
using (var tfc = new TempFileCollection())
{
var fn = tfc.AddExtension("tmp");
Console.WriteLine(fn);
}
这是确切的例外:
System.ArgumentException: The file name 'C:\Users\pczapla\AppData\Local\Temp\iqulrqva.tmp' was already in the collection.
Parameter name: fileName.
最佳答案
一个小的Reflector动作将揭示以下TempFileCollection
中的有趣片段:
new FileIOPermission(FileIOPermissionAccess.AllAccess, basePath).Demand();
path = this.basePath + ".tmp";
using (new FileStream(path, FileMode.CreateNew, FileAccess.Write))
{
}
flag = true;
...
this.files.Add(path, this.keepFiles);
这在
TempFileCollection.EnsureTempNameCreated
中,由TempFileCollection.BasePath
调用,由TempFileCollection.AddExtension
调用。我猜占位符使用“ .tmp”,所以您不能。关于c# - 为什么TempFileCollection在AddExtension(“tmp”)处引发异常?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1307225/