Thursday, May 04, 2006 5:11 PM Christopher

Getting a temporary filename the easy way - feedback

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.

In my case, I just really want the filename. Whether or not I change the extension, that file is mine to do with what I will. Also, since it's not in the same folder as the originating file, it can be the exact same name, extension and all, and it will still be a different file that what was generated by the framework. I should take care of file cleanup, and that zero byte file is still in the temp directory...

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.

true! So, maybe I was a little quick to post about this one, but it was one of those things that struck me as particularly useful. If you just want a temporary filename, and want to clean up the file that is created by GetTempFileName(), perhaps this will work a little better:

string GetTempFileName(){
	string filename = Path.GetTempFileName();

	File.Delete(filename);

	return Path.GetFileNameWithoutExtension(filename);
}

Okay y'all...rip it to shreds :) If I've missed something else obvious, call me out. Or, if you have a preferred method of generating temporary file names, show the way.

Filed under:

Comments

# re: Getting a temporary filename the easy way - feedback

Friday, May 05, 2006 10:40 AM by Raymond

I think you missed Jerry Pisk's point.

The file that is yours to do with what you will is the one with the extension. But once you remove/change the extension, then you're operating on a different file - one that is not guaranteed unique.

E.g. %TEMP% contains the file "ABC001.GIF".

Path.GetTempFileName returns the file "ABC001.TMP".

You strip off the .tmp and change it to .gif - now you're overwriting an existing file.

# re: Getting a temporary filename the easy way - feedback

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

Possible! However, by using Path.GetFileNameWithoutExtension, I am no longer working in %TEMP%, I am using the filename only. Again, I am interested solely in generating a semi-unique temporary file name - are you (and Jerry by extension I guess) saying that I should also first make sure that the file does not exist on disk before returning the filename? That kind of destroys the purpose of an "easy" way to generate a filename, right? On the other hand, the code *is* assuming that the filename that is generated is not in its destination (be that %TEMP% or elsewhere). That is a pretty big assumption, but this is just one function, not an entire system.

What would you do to improve this code?

# re: Getting a temporary filename the easy way - feedback

Friday, May 05, 2006 5:41 PM by Chris D

How about just generating a guid... Performing IO operations is a horribly inefficient method of just getting a string representing a unique file name.

Chris

# re: Getting a temporary filename the easy way - feedback

Friday, May 05, 2006 7:42 PM by Eric Newton

If you truly want a unique filename, just use a GUID... no reason to use the Path.GetTempFilename() if you don't truly want a temp filename already created and guaranteed to be yours.

string fileName = myGuid.ToString() + ".tmp"; that'll be pretty darn unique! (hopefully default ToString on guids generates a valid filename)

# re: Getting a temporary filename the easy way - feedback

Friday, May 05, 2006 10:15 PM by MartinJ

I use the GUID structure. I'm fairly certain that that would generate something unique. I tend to create temp directories so that I can use friendly file names when launching external programs (such as Acrobat when I've generated a report).

# re: Getting a temporary filename the easy way - feedback

Sunday, May 07, 2006 4:24 PM by Raymond Chen

If you don't mind the possibility of overwriting an existing file, then more power to you, but as a courtesy to people who may stumble across your code, you should call it out, because the person who copies your function might care even though you don't.

# re: Getting a temporary filename the easy way - feedback

Friday, September 14, 2007 6:57 AM by superza

Get information about VIAGRA, an erectile dysfunction (ED) treatment.  Licensed Physicians Prescribe FDA Approved Viagra , Online Diagnosis.

<a href=italmed.eamped.com/>comprare cialis online </a>

Sildenafil citrate, sold under the names Viagra, Revatio and under .... The preparation steps for synthesis of Viagra (sildenafil citrate) are as follows:

<a href=italmed.forka.eu/>acquisto levitra generico </a>

# re: Getting a temporary filename the easy way - feedback

Monday, May 12, 2008 8:22 AM by Mojtaabaa

i think that using the .net framework builtin functions and library is the best solution

# re: Getting a temporary filename the easy way - feedback

Tuesday, December 02, 2008 6:05 AM by liamlow

It looks like you're over complicating this idea. I think your solution offers a simple and easy to understand method, which is good. However it may miss the point when it comes to simplifying your choices.

If you only need the filename, presumably you know where you're storing your files. I'm also guessing you have reasonable control over this location without (much) fear of conflicting with another application?

Therefore you should chose a convention that just works in your context. For example a sequential one. Remember, you don't know how Path.GetTempFileName makes its decisions so you can't guarantee it won't create conflicts in your chosen directory.

You should aim to start with something simplistic and resolve issues as they arise. For example tracking an indices or incorporating the date and time generally results in unique file names (Especially when using fractions of a second).

Also unless performance is a real consideration, you should always check that the File.Exists() before hand.

Use a random number when collisions do occur.

GUID's are another suitable [good] choice (format depending of course) however they are perhaps a little overkill in that it is sometimes useful to be able to identify your file without having the program tell you (A sequential mechanism like date and time is helpful preferable here).

# re: Getting a temporary filename the easy way - feedback

Tuesday, December 09, 2008 6:57 AM by Alexey

DateTime.Now.Millisecond as part of filename are another choice for create temp file name.

# re: Getting a temporary filename the easy way - feedback

Monday, December 29, 2008 10:11 PM by Penisa

outgoing line buddy. trust to get more from your side :)

# re: Getting a temporary filename the easy way - feedback

Monday, September 14, 2009 8:44 PM by carrepossesseion

Keep working ,great job!

# re: Getting a temporary filename the easy way - feedback

Sunday, October 11, 2009 4:24 PM by shelpblethy

Super-Duper site! I am loving it!! Will come back again - taking you feeds also, Thanks.

# re: Getting a temporary filename the easy way - feedback

Saturday, October 17, 2009 1:12 AM by shelpblethy

You made some good points there. I did a search on the topic and found most people will agree with your blog.

# re: Getting a temporary filename the easy way - feedback

Tuesday, October 20, 2009 1:48 PM by RPR

If you simply create a _subfolder_ with a GUID name in the temp directory, then you can name your files within this subfolder any way you like. You can even hard-code your own GUID in your code (instead of generating a new one every time your app is run), so that you can easily identify which folder belongs to your app.

# re: Getting a temporary filename the easy way - feedback

Wednesday, November 11, 2009 2:16 PM by Craig Turner

This makes me remember something my father pretty much always said...

Of course its totally not appropriate at this time...

Leave a Comment

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