Which Environment.SpecialFolder.XXX will give me Context.getFilesDir()? - xamarin

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

Related

Issue getting a file with Laravel

Hi I do not know what is happening but I want to get a file from the storage folder and it says that it does not existe but it exists I mean I go to the folder and it is there..
I retrieve the file like this:
$path = storage_path('app/public/' . $settlement->support);
$settlement_file = Storage::disk('local')->get($path);
And I get this error:
message: "File not found at path:
home/jysparki/newjis.jisparking.com/storage/app/public/06152617_8-2025.pdf"
and If I go to the folder it exists so I wonder what am I doing wrong? Thanks.
If you look at config/filesystems.php file and find disks array, you may notice that root under the local disk key is set to storage_path('app'). Well it's up to what the value may be in your case you're free to change it, but what does this mean is whenever you call Storage::disk('local') method it will try to find files relative to storage_path('app'), so there is no need to specify full path. Try this instead
$settlement_file = Storage::disk('local')->get('/public/' . $settlement->support);
or
$settlement_file = Storage::disk('public')->get($settlement->support);
as Laravel by default has public disk which pointed into /app/public storage directory

What is common practice for the location of temporary files after Windows XP [duplicate]

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.

Visual Studio Setup Project - How to Obtain the Directory Path from a File-Search Launch Condition

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)));
}

Where do the files created with File.new actually get stored in Ruby?

I am creating files from within Ruby scripts and adding stuff to them. But where are these files stored that I am creating?
I'm very new to this, sorry!
The files are created at whatever location you specified. For instance:
f = File.new("another_test.txt","w+")
that will create the file in the current working directory. You specify the path along with the file name. For example:
f = File.new("~/Desktop/another_test.txt","w+") # will create the file on the desktop.
For more details, check the File documentation.
Updated:
Included mu is too short correction.

Where should my windows app store cache and tempfiles? [duplicate]

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.

Resources