Temporary File Names

  • Thread starter Thread starter Bob Morris
  • Start date Start date
B

Bob Morris

I need to be able to create unique new filenames in a directory set by me,
with an extension that I set. The two commands I need are:-

Path.SetTempFilePath(string path);

and

Path.GetTempFile(string extension);

Unfortunately MS haven't provided them. Any ideas?

Bob
 
Bob Morris said:
I need to be able to create unique new filenames in a directory set by me,
with an extension that I set. The two commands I need are:-

Path.SetTempFilePath(string path);

and

Path.GetTempFile(string extension);

Unfortunately MS haven't provided them. Any ideas?

Bob

You could always write your own:
1) create a filename, either random or something like "fileNNN.ext" where
nnn is a number
2) check if that file already exists, if it does, repeat step 1 (increase
the number)
3) return the name that you finally ended up with

Hans Kesting
 
Bob Morris said:
I need to be able to create unique new filenames in a directory set by me,
with an extension that I set. The two commands I need are:-

Path.SetTempFilePath(string path);

and

Path.GetTempFile(string extension);

Create a Guid, and append the file extension to it. Near guaranteed
uniqueness, and easy to do. Works like a charm for me!

public string MakeUniqueName(string Ext)
{
string mvarFileName
Guid guid = Guid.NewGuid();
mvarFileName = guid.ToString().Replace("-", "");
mvarFileName += "." + Ext;
return mvarFileName;
}
 
Back
Top