I have a slightly strange problem with an application that writes database connection settings into a file located int the users temp directory.
I use the standard call – GetTempPath and then save the file using this path. I build my app and deploy. 99/100 machines work – and save their files in the users temp directory, including XP and Win7 users.
However, I have noticed one user (Win 7 Pro 64bit), every time the program goes to create the connection file in the users temp path, it creates a new folder (with about 5/6 random characters) in the temp directory and saves the file in there!. This means every time the user restarts the app, the settings cannot be retrieved (because there is no capability for saving the last used random folder!), the user effectively loses their settings. So the program just keeps creating new random folders in the temp path time and time again. Bizarre!
Has anyone else had this problem, or does anyone know what might be going on here? I suspect its something to do with the OS, not my code.
Path.GetTempPath only gets the folder of the temp files. It sounds like you’re using Path.GetTempFileName.
Its by design, the first sentence says “Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.”. It will not return a temp files that already exist. If you want to have temporary setting its a fine function to call but remember the function will not return the same location unless it can make the file zero byte long.
If you like to access the file again put the setting file elsewhere or use a more permanent solution (this allows you to set default values and change/save/load at runtime). If you want to be especially lazy create a guid or mash random letters on your keyboard (sdhfuisdhfusdhc) and use that as a hardcoded filename. Example:
Path.GetTempPath()+"sdhfuisdhfusdhc";