Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a temp file in a directory other than temp?

Tags:

c#

.net

I've written some code that is supposed to write a file to the temp directory, then copy it to a permanent location, but find that doing so creates a permission-related error on the copy command. The code looks like this:

 string tempPath = Path.GetTempFileName();
 Stream theStream = new FileStream(tempPath, FileMode.Create);

  // Do stuff.

  File.Copy(tempPath, _CMan.SavePath, true);
  File.Delete(tempPath);

I dimly remember that there's an API call I can make to create a temp file in a specified directory, passed as a parameter. But, that's a dim memory from my VB 6 days.

So, how do I create a temp file in a directory other than the temp directory defined by Windows?

like image 898
Yes - that Jake. Avatar asked Mar 23 '09 17:03

Yes - that Jake.


People also ask

How do I create a TMP folder?

Use mktemp -d . It creates a temporary directory with a random name and makes sure that file doesn't already exist. You need to remember to delete the directory after using it though.

How do I change the location of a .TMP file?

To permanently set the temporary directory for a Windows user account, go to Control Panel->System->Advanced, and enter the "Environment Variables" dialog window to find and change the TEMP and TMP variable settings in the "User variables".


1 Answers

Path.Combine(directoryYouWantTheRandomFile, Path.GetRandomFileName())
like image 162
Greg Dean Avatar answered Oct 13 '22 02:10

Greg Dean