我需要用.csv扩展名生成一个唯一的临时文件。

我现在做的是

string filepath = System.IO.Path.GetTempFileName().Replace(".tmp", ".csv");

但是,这并不能保证.csv文件是唯一的。

我知道发生碰撞的可能性非常低(特别是如果您考虑到我没有删除.tmp文件的话),但是这段代码对我来说不太好。

当然,我可以手动生成随机文件名,直到我最终找到一个唯一的文件名(这应该不是问题),但我很想知道其他人是否已经找到了处理这个问题的好方法。


唯一的:保证(统计上)唯一的:

string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".csv"; 

(引用维基上关于碰撞概率的文章:

...one's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion [19], that means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs

编辑:请参阅JaredPar的评论。

试试这个功能…

public static string GetTempFilePathWithExtension(string extension) {
  var path = Path.GetTempPath();
  var fileName = Path.ChangeExtension(Guid.NewGuid().ToString(), extension);
  return Path.Combine(path, fileName);
}

它将返回一个完整的路径和您选择的扩展名。

注意,它不能保证产生唯一的文件名,因为从技术上讲,其他人可能已经创建了该文件。然而,有人猜出你的应用产生的下一个guid并创建它的机会非常非常低。假设这是唯一的是很安全的。

为什么不检查文件是否存在?

string fileName;
do
{
    fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".csv";
} while (System.IO.File.Exists(fileName));

您还可以执行以下操作

string filepath = Path.ChangeExtension(Path.GetTempFileName(), ".csv");

这也符合预期

string filepath = Path.ChangeExtension(Path.GetTempPath() + Guid.NewGuid().ToString(), ".csv");

你也可以选择使用System.CodeDom.Compiler.TempFileCollection。

string tempDirectory = @"c:\\temp";
TempFileCollection coll = new TempFileCollection(tempDirectory, true);
string filename = coll.AddExtension("txt", true);
File.WriteAllText(Path.Combine(tempDirectory,filename),"Hello World");

这里我使用txt扩展名,但你可以指定任何你想要的。我还将keep标志设置为true,以便在使用后保留临时文件。不幸的是,TempFileCollection为每个扩展名创建一个随机文件。如果需要更多临时文件,可以创建TempFileCollection的多个实例。

如何:

Path.Combine(Path.GetTempPath(), DateTime.Now.Ticks.ToString() + "_" + Guid.NewGuid().ToString() + ".csv")

计算机在同一时刻生成相同的Guid是极不可能的。我在这里看到的唯一缺点是DateTime.Now.Ticks会增加性能影响。

这就是我所做的:

string tStamp = String.Format("{0:yyyyMMdd.HHmmss}", DateTime.Now);
string ProcID = Process.GetCurrentProcess().Id.ToString();
string tmpFolder = System.IO.Path.GetTempPath();
string outFile = tmpFolder + ProcID + "_" + tStamp + ".txt";

这对你来说可能很方便…它是创建一个temp。文件夹并在VB.NET中以字符串的形式返回。

易于转换为c#:

Public Function GetTempDirectory() As String
    Dim mpath As String
    Do
        mpath = System.IO.Path.Combine(System.IO.Path.GetTempPath, System.IO.Path.GetRandomFileName)
    Loop While System.IO.Directory.Exists(mpath) Or System.IO.File.Exists(mpath)
    System.IO.Directory.CreateDirectory(mpath)
    Return mpath
End Function

这对我来说似乎工作得很好:它检查文件是否存在,并创建文件以确保它是一个可写的位置。 应该可以正常工作,你可以将其更改为直接返回FileStream(这通常是临时文件所需要的):

private string GetTempFile(string fileExtension)
{
  string temp = System.IO.Path.GetTempPath();
  string res = string.Empty;
  while (true) {
    res = string.Format("{0}.{1}", Guid.NewGuid().ToString(), fileExtension);
    res = System.IO.Path.Combine(temp, res);
    if (!System.IO.File.Exists(res)) {
      try {
        System.IO.FileStream s = System.IO.File.Create(res);
        s.Close();
        break;
      }
      catch (Exception) {

      }
    }
  }
  return res;
} // GetTempFile

我认为你应该试试这个:

string path = Path.GetRandomFileName();
path = Path.Combine(@"c:\temp", path);
path = Path.ChangeExtension(path, ".tmp");
File.Create(path);

它生成一个唯一的文件名,并在指定位置用该文件名创建一个文件。

public static string GetTempFileName(string extension)
{
  int attempt = 0;
  while (true)
  {
    string fileName = Path.GetRandomFileName();
    fileName = Path.ChangeExtension(fileName, extension);
    fileName = Path.Combine(Path.GetTempPath(), fileName);

    try
    {
      using (new FileStream(fileName, FileMode.CreateNew)) { }
      return fileName;
    }
    catch (IOException ex)
    {
      if (++attempt == 10)
        throw new IOException("No unique temporary file name is available.", ex);
    }
  }
}

注意:这类似于Path.GetTempFileName。创建一个空文件以保留文件名。它会尝试10次,以防Path.GetRandomFileName()生成的冲突;

c++的GetTempFileName的MSDN文档讨论了你的问题并回答了它:

GetTempFileName不能保证文件名是唯一的。 只使用uUnique参数的下16位。如果lpPathName和lpPrefixString参数保持不变,这将限制GetTempFileName最多为65,535个唯一的文件名。 由于用于生成文件名的算法,当使用相同的前缀创建大量文件时,GetTempFileName的性能可能很差。在这种情况下,建议您基于guid构造惟一的文件名。

在我看来,这里提出的大多数答案都是次优的。最接近的是最初由Brann提出的。

临时文件名必须是

独特的 无冲突(不存在) 原子(在同一操作中创建名称和文件) 很难猜测

由于这些需求,独自编写这样一个野兽并不是一个好主意。聪明的人写IO库时会担心像锁(如果需要的话)等事情。 因此,我认为没有必要重写System.IO.Path.GetTempFileName()。

这个,即使看起来很笨拙,也应该做到:

//Note that this already *creates* the file
string filename1 = System.IO.Path.GetTempFileName()
// Rename and move
filename = filename.Replace(".tmp", ".csv");
File.Move(filename1 , filename);

这是生成增量文件名的一种简单但有效的方法。它将直接查看当前文件(您可以轻松地将其指向其他地方),并搜索以YourApplicationName*.txt为基础的文件(同样,您可以轻松地更改该文件)。它将从0000开始,因此第一个文件名将是YourApplicationName0000.txt。如果由于某种原因,文件名在左右部分之间(即不是数字)有垃圾,那么这些文件将通过tryparse调用被忽略。

    public static string CreateNewOutPutFile()
    {
        const string RemoveLeft = "YourApplicationName";
        const string RemoveRight = ".txt";
        const string searchString = RemoveLeft + "*" + RemoveRight;
        const string numberSpecifier = "0000";

        int maxTempNdx = -1;

        string fileName;
        string [] Files = Directory.GetFiles(Directory.GetCurrentDirectory(), searchString);
        foreach( string file in Files)
        {
            fileName = Path.GetFileName(file);
            string stripped = fileName.Remove(fileName.Length - RemoveRight.Length, RemoveRight.Length).Remove(0, RemoveLeft.Length);
            if( int.TryParse(stripped,out int current) )
            {
                if (current > maxTempNdx)
                    maxTempNdx = current;
            }
        }
        maxTempNdx++;
        fileName = RemoveLeft + maxTempNdx.ToString(numberSpecifier) + RemoveRight;
        File.CreateText(fileName); // optional
        return fileName;
    }

简单的c#函数:

public static string GetTempFileName(string extension = "csv")
{
    return Path.ChangeExtension(Path.GetTempFileName(), extension);
}

根据我在网上找到的答案,我得到了我的代码如下:

public static string GetTemporaryFileName()
{       
    string tempFilePath = Path.Combine(Path.GetTempPath(), "SnapshotTemp");
    Directory.Delete(tempFilePath, true);
    Directory.CreateDirectory(tempFilePath);
    return Path.Combine(tempFilePath, DateTime.Now.ToString("MMddHHmm") + "-" + Guid.NewGuid().ToString() + ".png");
}

正如Jay Hilyard的c#烹饪书,Stephen Teilhet在应用程序中使用临时文件中指出的那样:

无论何时需要存储,都应该使用临时文件 暂时供以后检索的信息。 您必须记住的一件事是删除这个临时文件 在创建它的应用程序终止之前。 如果它没有被删除,它将保留在用户的临时文件中 目录,直到用户手动删除它。

我混合@Maxence和@Mitch小麦答案记住,我想要GetTempFileName方法的语义(文件名是创建的新文件的名称)添加扩展名首选。

string GetNewTempFile(string extension)
{
    if (!extension.StartWith(".")) extension="." + extension;
    string fileName;
    bool bCollisions = false;
    do {
        fileName = Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString() + extension);
        try
        {
            using (new FileStream(fileName, FileMode.CreateNew)) { }
            bCollisions = false;
        }
        catch (IOException)
        {
            bCollisions = true;
        }
    }
    while (bCollisions);
    return fileName;
}

在这里,我们可以首先找到文件的扩展名 它来自于文件,在找到扩展名之后。然后,我们 可以创建文件的临时名称,之后我们可以改变 扩展前一个,它将工作。

var name = Path.GetTempFileName();
var changename = Path.GetFileName(name);
var fileName = Path.ChangeExtension(changename, fileExtension);