How can I create a temp file with a specific extension with .NET?

后端 未结 17 2093
北恋
北恋 2020-11-28 01:49

I need to generate a unique temporary file with a .csv extension.

What I do right now is

string filename = System.IO.Path.GetTempFileName().Replace(         


        
17条回答
  •  心在旅途
    2020-11-28 02:05

    I think you should try this:

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

    It generates a unique filename and creates a file with that file name at a specified location.

提交回复
热议问题