Thursday, May 04, 2006 3:02 PM Christopher

Getting a temporary filename the easy way

If you need to generate temporary / semi-unique filenames, here is a little snippet that uses the framework:

using System.IO;

string GetTempFileName(){
	return Path.GetFileNameWithoutExtension(Path.GetTempFileName());
}

As the name implies, this will return the temporary name of a file without the extension, so it's up to you to add whatever filetype you may be trying to create. For example, let's say I wanted to generate a .gif:

string GetTempGifFileName(){
	return string.Format("{0}.{1}", GetTempFileName(), "gif");
}

I had overlooked this little piece of functionality because the component that I was using generated filenames with GUIDs, so I never really worried about it. Way to make my life easier, .netfx :)

[ Currently Playing : Mississippi Queen - Ozzy Osbourne - Under Cover (4:11) ]

Filed under:

Comments

# re: Getting a temporary filename the easy way

Thursday, May 04, 2006 4:07 PM by Jerry Pisk

Path.GetTempFileName() guarantees that you will get an available name, it does actually go ahead and create the file so it is guaranteed to be yours when you use it. If you strip the extension off and substitute your own you may end up overwriting an existing file. Not a smart thing to do.

# re: Getting a temporary filename the easy way

Thursday, May 04, 2006 4:41 PM by Paul Welter

Be aware the GetTempFileName() actually creates a zero byte file in the temp folder. Your code will leave the temp file behind in the temp folder. If you use that a lot, you will end up with a lot of files in the temp folder.

# re: Getting a temporary filename the easy way

Friday, May 05, 2006 12:51 PM by Jb Evain

string.Format("{0}.{1}", ...);

Wow,
This is such a waste of resource only to concatenate three strings.

# re: Getting a temporary filename the easy way

Friday, May 05, 2006 1:16 PM by Chris Frazier

How is it such a waste of resource? Enlighten me.

# re: Getting a temporary filename the easy way

Monday, May 08, 2006 8:45 AM by Jb Evain

Oops,

Sorry for the long time before answering.
Consider the following code:

using System;
using System.Text;

class Test {

static void Format ()
{
string.Format ("{0}.{1}", "foo", "bar");
}

static void Concat ()
{
string.Concat ("foo", ".", "bar");
}

static void Main ()
{
int run = 100000;

DateTime start = DateTime.Now;
for (int i = 0; i < run; i++)
Format ();
Console.WriteLine ("Format: {0}", (DateTime.Now - start));

start = DateTime.Now;
for (int i = 0; i < run; i++)
Concat ();
Console.WriteLine ("Concat: {0}", (DateTime.Now - start));
}
}

Run it and you'll see that the format takes much more time to do its task (mostly due to parsing).

Jb

# re: Getting a temporary filename the easy way

Monday, May 08, 2006 12:35 PM by Chris Frazier

:) thanks for enlightening me.

# re: Getting a temporary filename the easy way

Monday, February 15, 2010 8:35 AM by Rengenx

Где-то я это уже видел… А если по теме то спасибо.

# re: Getting a temporary filename the easy way

Tuesday, November 09, 2010 2:16 AM by codefoo

"This is such a waste of resource only to concatenate three strings."

string.Format("{0}.{1}", ...);

The resources it uses is negligible.  By this example string.Format should never be used.  

The example is off in that it doesn't take into account the article's context.  I guess it's possible to need 100,000 temporary file names, but there would probably be a slightly better way of doing this (possibly by modifying this function).

In addition, if a dynamic number of runs were made over and over, StringBuilder would be much faster than both examples .  string.Concat will probably win when the number of runs is static (because of the compiler, check the stringbuilder docs), but if it has to figure it out at runtime StringBuilder will win.  

# re: Getting a temporary filename the easy way

Saturday, November 20, 2010 9:59 AM by Kate Trider

Wow, rather nice article. How will I find that subscription?

Kate  Trider

<a href="www.wirelesscameradetectors.com/">spy stores</a>

# re: Getting a temporary filename the easy way

Saturday, March 26, 2011 4:59 AM by generic

Surprisingly! It is like you understand my mind! You seem to know so much about this, just like you wrote the book in it or something. I think that you can do with some pics to drive the content home a bit, but other than that, this is informative blog post. A good read. I’ll definitely revisit again.

# re: Getting a temporary filename the easy way

Tuesday, March 29, 2011 1:39 AM by sildenafil

Do you have any more info on this?

# re: Getting a temporary filename the easy way

Friday, September 23, 2011 3:54 PM by George Birbilis

seems you need a spam tool, that Kate Trider commenting above is pussing some spam link

# re: Getting a temporary filename the easy way

Wednesday, December 21, 2011 5:58 AM by BlooreMof

You live - you see , and tell me .

Leave a Comment

(required) 
(required) 
(optional)
(required)