views:

579

answers:

4

Currently, I use Path.GetTempPath() to figure out where to write my log files, but recently I came across a user's machine where the path returned was not what I expected.

Usually, the returned path is C:\Documents and Settings\[userid]\Local Settings\Temp but in this case, it was C:\Temp

This would not normally be a problem, but for some reason the user in question did not have access to write to C:\Temp

I double checked the environment variables, and the USER environment variable was pointing as expected to C:\Documents and Settings\[userid]\Local Settings\Temp, whilst the SYSTEM environment variable was pointing to C:\WINNT\Temp.

So... where is Path.GetTempPath() getting it's value from ? Group Policy? Registry? I have Googled, but to no avail...

Appreciate anyone's time on this...

Andy

+2  A: 

It calls the GetTempPath function. The documentation explains what environment variables it checks.

Arve
+11  A: 

(Using Reflector) Path.GetTempPath() ultimately calls the Win32 function GetTempPath (from kernel32.dll). The MDSN docs for this state:

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

  • The path specified by the TMP environment variable.
  • The path specified by the TEMP environment variable.
  • The path specified by the USERPROFILE environment variable.
  • The Windows directory.

Note that they also state that it doesn't check whether or not the path actually exists or can be written to, so you may end up trying to write your log files to a path that doesn't exist, or one that you cannot access.

adrianbanks
Great answer! Clear, concise and exactly the info I needed. Hadn't thought of checking the Win32 function docs... Many thanks. Andy
Andy Blackman
+1  A: 

I've noticed GetTempPath() can bring back the local user's Documents & Settings\user\Local Settings\Temp path if it's a console application, and noticed it can bring back C:\WINDOWS\Temp (on the server) if it's a web app being ran from a client. In the former case, no big deal - the account running the app has the rights to that folder. In the latter, maybe it is a big deal if the App Pool Identity account (or account you may be using to impersonate with in the Web.config file for the web app) doesn't have privileges to C:\WINDOWS\Temp on the server (which is a big chance it doesn't). So for my console apps, just so there's no question where temp files are written, hard-coding a string into an INI file is the best and easiest for me, and for a web app, hard-coding it in the web.config and getting it using ConfigurationManager.AppSettings["myKey"] works, or if it's a web app, use this function to send the file to the ASP Temporary Files folders and work with it there:

public static string findFileDirectory(string file)
{
    // Get the directory where our service is being run from
    string temppath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    // Ensure proper path notation so we can add the INI file name
    if (!temppath.EndsWith(@"\")) temppath += @"\";

    return temppath;
}

and call it like this:

    string tempFolderPath = findFileDirectory("Web.config");
    tempFolderPath = tempFolderPath.Replace(@"\\", @"\");

and just use "tempFolderPath" instead of where you used Path.GetTempPath() before. This function works awesome & I use it in my code in place of this evil GetTempPath() method so I know my app can do what it needs to do, since the ASP Temp Files folder should have all the permissions it needs for its operations (DOMAIN\NETWORK SERVICE and App Pool ID account need Full Control). tempFolderPath ends in a trailing slash, so just concat directly with your variable/file name to get the right path going.

-Tom

P.S. You need to add 2 namespaces to make that function work: System.IO and System.Reflection

Tom
+2  A: 

Please try using the following to determine good place for Your data:

Environment.GetFolderPath(Environment.SpecialFolder folder);

Where Specialfolder

// Summary:
//     Specifies enumerated constants used to retrieve directory paths to system
//     special folders.
[ComVisible(true)]
public enum SpecialFolder
{
  // Summary:
  //     The logical Desktop rather than the physical file system location.
  Desktop = 0,
  //
  // Summary:
  //     The directory that contains the user's program groups.
  Programs = 2,
  //
  // Summary:
  //     The directory that serves as a common repository for documents.
  Personal = 5,
  //
  // Summary:
  //     The "My Documents" folder.
  MyDocuments = 5,
  //
  // Summary:
  //     The directory that serves as a common repository for the user's favorite
  //     items.
  Favorites = 6,
  //
  // Summary:
  //     The directory that corresponds to the user's Startup program group.
  Startup = 7,
  //
  // Summary:
  //     The directory that contains the user's most recently used documents.
  Recent = 8,
  //
  // Summary:
  //     The directory that contains the Send To menu items.
  SendTo = 9,
  //
  // Summary:
  //     The directory that contains the Start menu items.
  StartMenu = 11,
  //
  // Summary:
  //     The "My Music" folder.
  MyMusic = 13,
  //
  // Summary:
  //     The directory used to physically store file objects on the desktop.
  DesktopDirectory = 16,
  //
  // Summary:
  //     The "My Computer" folder.
  MyComputer = 17,
  //
  // Summary:
  //     The directory that serves as a common repository for document templates.
  Templates = 21,
  //
  // Summary:
  //     The directory that serves as a common repository for application-specific
  //     data for the current roaming user.
  ApplicationData = 26,
  //
  // Summary:
  //     The directory that serves as a common repository for application-specific
  //     data that is used by the current, non-roaming user.
  LocalApplicationData = 28,
  //
  // Summary:
  //     The directory that serves as a common repository for temporary Internet files.
  InternetCache = 32,
  //
  // Summary:
  //     The directory that serves as a common repository for Internet cookies.
  Cookies = 33,
  //
  // Summary:
  //     The directory that serves as a common repository for Internet history items.
  History = 34,
  //
  // Summary:
  //     The directory that serves as a common repository for application-specific
  //     data that is used by all users.
  CommonApplicationData = 35,
  //
  // Summary:
  //     The System directory.
  System = 37,
  //
  // Summary:
  //     The program files directory.
  ProgramFiles = 38,
  //
  // Summary:
  //     The "My Pictures" folder.
  MyPictures = 39,
  //
  // Summary:
  //     The directory for components that are shared across applications.
  CommonProgramFiles = 43,
}
Turek