Currently I am using following function to get the temporary folder path for current user:
string tempPath = System.IO.Path.GetTempPath();
On some machines it gives me temp folder path of current user like:
C:\Documents and Settings\administrator\Local Settings\Temp\
On some machines it gives me system temp folder path like:
C:\Windows\TEMP
MSDN Documentation also says that above API returns current system's temporary folder.
Is there any other API available which gives me current user's temporary folder path like this:
C:\Documents and Settings\administrator\Local Settings\Temp\
System.IO.Path.GetTempPath() is just a wrapper for a native call to GetTempPath(..) in Kernel32.
Have a look at http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx
Copied from that page:
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.
It's not entirely clear to me whether "The Windows directory" means the temp directory under windows or the windows directory itself. Dumping temp files in the windows directory itself sounds like an undesirable case, but who knows.
So combining that page with your post I would guess that either one of the TMP, TEMP or USERPROFILE variables for your Administrator user points to the windows path, or else they're not set and it's taking a fallback to the windows temp path.
DO NOT use this:
System.Environment.GetEnvironmentVariable("TEMP")
Environment variables can be overridden, so the TEMP variable is not necessarily the directory.
The correct way is to use System.IO.Path.GetTempPath() as in the accepted answer.
I have this same requirement - we want to put logs in a specific root directory that should exist within the environment.
public static readonly string DefaultLogFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
If I want to combine this with a sub-directory, I should be able to use Path.Combine( ... ).
The GetFolderPath method has an overload for special folder options which allows you to control whether the specified path be created or simply verified.
Related
I am working with customer using jBASE and I checked the MD item to determine where the file existed and there was no MD item for the file. How does jBASE determine where the file is?
jBASE uses the locations as defined in the JEDIFILEPATH environment variable to find your files:
https://docs.zumasys.com/jbase/environment-variables/jedifilepath/
But anything defined in the MD/VOC takes precedence.
It looks for the item (I suppose you mean a jBASE hashed file) either in VOC/MD (defined in JEDIFILENAME_MD environment variable) or HOME path or current working directory path.
Perhaps your MD or VOC (Vocabulary) is pointing somewhere else than where you expect it to be - check the JEDIFILENAME_MD variable.
I want to access a folder in which I can save temporary files and write there a file and where I don't need permission to write to the folder.
I currently use:
string targetBaseDir = Environment.GetFolderPath(
Environment.SpecialFolder.Personal,
Environment.SpecialFolderOption.Create
);
This gives me a private directory for which I need no permissions which is the directory that corresponds to Context.getFilesDir(), however as I don't need the file to exist permanently it would be cleaner if I could save them in the directory that corresponds to Context.getFilesDir(). What do I have to write instead of .Personal to get the directory?
You can use FileSystem.AppDataDirectory via Xamarin.Essentials in your .NetStd library to obtain what is the "getFilesDir()" location on Android.
Example:
var appData = Xamarin.Essentials.FileSystem.AppDataDirectory;
appData equals /data/user/0/com.sushihangover.FormsTestSuite/files
And the Android FilesDir:
Log.Debug("SO", FilesDir.ToString() );
Equals /data/user/0/com.sushihangover.FormsTestSuite/files
access a folder in which I can save temporary files
In that case use the Cache dir:
var cacheDir = Xamarin.Essentials.FileSystem.CacheDirectory;
cacheDir would equal: `/data/user/0/com.sushihangover.FormsTestSuite/cache
re: https://learn.microsoft.com/en-us/xamarin/essentials/
Both Environment.SpecialFolder.Personal and Environment.SpecialFolder.MyDocuments will give you the path of Context.getFilesDir().
However, if you want to save small temporary files, you can make use of the cache directory Context.getCacheDir().
There is no special folder enum for the cache directory though. You'll have to get the path from the current context Context.CacheDir.Path, or from Xamarin.Essentials.FileSystem.CacheDirectory if you use Xamarin.Essentials API.
Read more:
File access with Xamarin.Android with SpecialFolder
Writing cache file in internal storage
File system helpers in Xamarin.Essentials
Currently I am using following function to get the temporary folder path for current user:
string tempPath = System.IO.Path.GetTempPath();
On some machines it gives me temp folder path of current user like:
C:\Documents and Settings\administrator\Local Settings\Temp\
On some machines it gives me system temp folder path like:
C:\Windows\TEMP
MSDN Documentation also says that above API returns current system's temporary folder.
Is there any other API available which gives me current user's temporary folder path like this:
C:\Documents and Settings\administrator\Local Settings\Temp\
System.IO.Path.GetTempPath() is just a wrapper for a native call to GetTempPath(..) in Kernel32.
Have a look at http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx
Copied from that page:
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.
It's not entirely clear to me whether "The Windows directory" means the temp directory under windows or the windows directory itself. Dumping temp files in the windows directory itself sounds like an undesirable case, but who knows.
So combining that page with your post I would guess that either one of the TMP, TEMP or USERPROFILE variables for your Administrator user points to the windows path, or else they're not set and it's taking a fallback to the windows temp path.
DO NOT use this:
System.Environment.GetEnvironmentVariable("TEMP")
Environment variables can be overridden, so the TEMP variable is not necessarily the directory.
The correct way is to use System.IO.Path.GetTempPath() as in the accepted answer.
I have this same requirement - we want to put logs in a specific root directory that should exist within the environment.
public static readonly string DefaultLogFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
If I want to combine this with a sub-directory, I should be able to use Path.Combine( ... ).
The GetFolderPath method has an overload for special folder options which allows you to control whether the specified path be created or simply verified.
I am looking for a way to add File(s) to an existing directory that has a random name as part of a Visual Studio Setup Project and I hoped someone might be able to help me solve this puzzle please.
I have been attempting to obtain the discovered path property of the directory using a Launch Condition; Unfortunately this method returns the full file path including the filename, which cannot be used as a directory property.
The directory in question takes the form [AppDataFolder]Company\Product\aaaaaaaaaaaa\
where aaaaaaaaaaaa is a random installation string.
Within the Launch Condition Setup I check for the directory's existence by searching for a file that would appear inside it,
Search Target Machine
(Name): File marker
Filename: sample.txt
Folder: [AppDataFolder]Company\Product\
Property: DIRFILE
Launch Condition
(Name): File marker exists
Condition: DIRFILE
In the Setup Project I add the file I wish to insert, with the details
Condition: DIRFILE
Folder: 'Installation folder'
Then in File System Setup I add a new folder entry for the random directory aaaaaaaaaaaa
(Name): Installation folder
Condition: DIRFILE
DefaultLocation: [DIRFILE]\..\ *Incorrect*
Property [DIRLOCATION]
As you can see the installer detects the existence of the marker file but, instead of placing my file at the same location, when using [DIRFILE] the installer would incorrectly try and insert it INTO the file;
This is because the file path was returned
[AppDataFolder]Company\Product\aaaaaaaaaaaa\sample.txt
where I instead need the directory path
[AppDataFolder]Company\Product\aaaaaaaaaaaa
Therefore I was wondering if it was possible to return the directory the file was found in from Search Target Machine (as opposed to the file location of the file), if I could extract the directory path by performing a string replace of the filename on the file location DIRFILE within the DefaultLocation field in File System Setup, or if perhaps there is even another method I am missing?
I'm also very interested in a simple solution for this, inside the setup project.
The way I did solve it was to install the files to a temporary location and then copy them to the final location in an AfterInstall event handler. Not a very elegant solution! Since it no longer care about the user selected target path I removed that dialog. Also I needed to take special care when uninstalling.
public override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
// Get original file folder
string originDir = Context.Parameters["targetdir"];
// Get new file folder based on the dir of sample.txt
string newDir = Path.GetDirectoryName(Context.Parameters["dirfile"]);
// Application executable file name
// (or loop for all files on the path instead)
string filename = "ApplicationName.exe";
// Move or copy the file
File.Move(Path.Combine(originDir, filename), Path.Combine(newDir, filename)));
}
I am using vs 2010 in windows.
In my program, I want to create a new directory under the current directory.
I use
TCHAR szPath[MAX_PATH];
GetModuleFileName( NULL, szPath, MAX_PATH );
And the string of szPath is "E:\A20J\Bin\***.exe".
But when I use:
BOOL bol = CreateDirectory("Path", NULL);
bol becomes 1, which means successful.
But under the "E:\A20J\Bin\", I found no such directory as "Path" even after refreshing, why?
The path of the executable is not indicative of the directory in which the current process is executing. Use GetCurrentDirectory() to detemine the directory in which the process is executing, and the directory "Path" will have been created there. If you wish to create the directory in the same directory as the binary then some string manipulation is required to construct the path.
Note that CreateDirectory() returns non-zero only if it creates the directory. The directory may already exist which you may wish to not treat as a failure (see this old answer of mine Create a directory if it doesn't exist).