Retrieve StorageFile with the help of path from Local Folder - window

I am making Windows 8.1 Universal App and want to retrieve file,
(local -> folder -> sub Folder -> file) which is stored in Local Folder.
One way is open Local Folder, then open folder, then open sub folder, then file.
But If I know the path, can I simply open the file?
(Like in Windows Phone 8, store.OpenFile())
Is there an API which would return Storage File when passed path?
Example: GetStorageFile("local/folder/subFolder/file") ?

You can use StorageFile.GetFileFromApplicationUriAsync and pass in a URI such as ms-appdata:///local/folder/subfolder/file.txt. This is preferred over GetFileFromPathAsync (#Parad1s3's suggestion) since it is relative to your data folder and won't change if the location of the app changes (eg, on a phone the user can move to or from the SD card).

string path = ApplicationData.Current.LocalFolder + "\\" + "YourFolder" + "\\" + "YourSubfolder" + "\\yourfile.xml";
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync(path);
Hope this helps.

Related

Xamarin.Forms - Save files out of app Directory

I'm using Xamarin.forms Media.Plugin and it works well. But I need to save the image out of app directory, like de Download Path or DCIM Path. How to do this ?
you could look at the documentation for External Storage
for Private External Storage:
var path = GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads);
string fileName = System.IO.Path.Combine(path.ToString(), "reservation.txt");
it will give a path like:
/storage/emulated/0/Android/data/package name/files/Download/reservation.txt
/storage/emulated/0/Android/data/package name/files/Download/reservation.txt
for Public External Storage:
var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
string fileName = System.IO.Path.Combine(path.ToString(), "reservation.txt");
it will give the path like:
/storage/emulated/0/Download/reservation.txt
so if you want to save files out of app Directory(Private External Storage),you could save them to Public External Storage

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

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

Copy folders through FTP in Windows

I have a Windows 2012 server where I am trying to copy a folder through FTP. The folder contains multiple folders inside it and the size is around 12 GB.
What command can be used to copy the whole tree structure including all the folders and files inside it.
I cannot zip this folder.
Also I have tried using mget* but it copies all files from all the
folders but doesn't created folder structure.
I am unable to use TAR command as the prompt shows "invalid command".
Windows command-line FTP client, the ftp.exe, does not support recursive directory transfers.
You have to use a 3rd party FTP client for that.
For example with WinSCP FTP client, you can use a batch-file like:
winscp.com /command ^
"open ftp://user:password#example.com/" ^
"get /folder/* c:\target\" ^
"exit"
It will automatically download all files and subfolders in the /folder.
For details, see WinSCP guide to automating file transfers from FTP server. There's also a guide for converting Windows ftp.exe script to WinSCP.
(I'm the author of WinSCP)
The target dir is a zip file. You can copy the full zip file into the ftp server using below code.
//Taking source and target directory path
string sourceDir = FilePath + "Files\\" + dsCustomer.Tables[0].Rows[i][2].ToString() + "\\ConfigurationFile\\" + dsSystems.Tables[0].Rows[j][0].ToString() + "\\XmlFile";
string targetDir = FilePath + "Files\\Customers\\" + CustomerName + "\\" + SystemName + "\\";
foreach (var srcPath in Directory.GetFiles(sourceDir))
{
//Taking file name which is going to copy from the sourcefile
string result = System.IO.Path.GetFileName(srcPath);
//If that filename exists in the target path
if (File.Exists(targetDir + result))
{
//Copy file with a different name(appending "Con_" infront of the original filename)
System.IO.File.Copy(srcPath, targetDir + "Con_" + result);
}
//If not existing filename
else
{
//Just copy. Replace bit is false here. So there is no overwiting.
File.Copy(srcPath, srcPath.Replace(sourceDir, targetDir), false);
}
}

Programmatically create shortcuts for folders in Windows

I have a folder A and a folder B. For each subfolder inside A I need to check if B contains a link to that subfolder. If not, I need to create it.
I'm currently creating the links using the code found on this page: Creating a file shortcut (.lnk)
My problem is that this code always creates a file shortcut, not a folder shortcut, so if I try to open the shortcut it does not open the corresponding folder. Anyone knows how to create folder shortcuts instead?
I'll answer my own question because I found lots of people asking this on the web and nobody providing an answer.
I found through trial and error that if you use the Path.GetFullPath method to calculate the value to assign to the TargetPath property, the output is in a format that's recognized as a folder path, so the system will automatically identify it as a folder shortcut and assign the corresponding icon.
FileSelectFolder, directory
if (directory != ""){
Loop, %directory%*.*, 1, 1
{
if (FileExist(A_LoopFileFullPath) = "D")
FileCreateDir, % A_Desktop "\myShortcuts" SubStr(A_LoopFileFullPath, StrLen(directory) + 1)
else
FileCreateShortcut, %A_LoopFileFullPath%, % A_Desktop "\myShortcuts" SubStr(A_LoopFileFullPath, StrLen(directory) + 1) A_LoopFileName ".lnk"
}
Run, % A_Desktop "\myShortcuts"
}
Esc::ExitApp

Firefox Add-on save to desktop

I'm trying to save a file in a folder other than the extensions folder. Right now I'm using:
var file = Components.classes["#mozilla.org/file/directory_service;1"].
getService(Components.interfaces.nsIProperties).
get("ProfD", Components.interfaces.nsIFile);
if( !file.exists() || !file.isDirectory() ) { // if it doesn't exist, create
file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
}
this.f = file.path+"\\text.txt";
How can I change the file path so that it saves it somewhere else on the machine? Or is this not possible?
Thanks!
You can use different "special folder" keys too. For a list of some common ones, see:
https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO#Getting_special_files
You can also initialize a file with any absolute path. See:
https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO#Creating_an_.0ansIFile.0a_object_%28.22opening.22_files%29

Resources