Download file and save it in isolated storage - c#

I have following code in my Windows 8/RT app:
public static async Task<StorageFile> SaveAsync(Uri fileUri, StorageFolder folder, string fileName)
{
try
{
var file = await folder.CreateFileAsync(fileName);
var downloader = new BackgroundDownloader();
var download = downloader.CreateDownload(fileUri, file); // here Exeption is fired
var res = await download.StartAsync();
return file;
}
catch (Exception e)
{
Debug.WriteLine(ex.Message);
}
return null;
}
ex.message: Access denied
Note:
file - it's a correct StorageFile (ContentType=image/jpg) fileUri - it's correct image urifolder - it's correct storage folder. What I missed?

What is the value of folder when you call the code? You may need to add a capability in Package.appxmanifest to the appropriate folder (e.g. Pictures folder, Documents folder).
By default, the sandboxed nature of a Modern UI app means that it will have explicit access only to its own local folder (think about it like Isolated Storage). In order to access other locations, your manifest file must declare that it needs to access those other locations. The available locations are, however, quite limited.
If you use the Save File Picker, however, you have access to a larger range of locations in which to save your file.

I found little mistake:I forgot, that fileUri is Uri to local server. Just adding nested property to manifest fixed problem.

Related

Reading/Writing RichEditBox (.rtf) files without using picker (C# - Visual Studio - UWP)

I am struggling to create my first UWP program in C#. I have run into a problem with reading/writing .rtf files and have not been able to work out a solution nor find one in the forums.
My program is an application for reading and writing a daily journal. The daily journal entries are displayed and edited in a RichEditBox. Next to the RichEditBox is a CalendarDatePicker.
When the CalendarDatePicker value changes, the program creates a file name based on the CalendarDatePicker date. For example, if the CalendarDatePicker's value was 7/22/2018, my program would turn that into the following file name: "2018_7_22.rtf" and then assign that to a string named fileName.
All of the above is working as desired. Here is the problem I am having:
When the value of the CalendarDatePicker changes, the program is supposed to immediately load the .rtf file (if it exists) from storage into the RichEditBox. And, when I press the SAVE button, the program is
supposed to save the RichEditBox contents to storage. Both reading and writing the file is supposed to
use the name derived from the CalendarDatePicker value rather than opening up a picker for either
opening or saving the file.
I have not been able to find any documentation about how to read and write a .rtf file for the RichEditBox without using a picker. I do not want to use a picker because I want that the given journal entry can only be read or written according to the file name that is based on the current value of the CalendarDatePicker.
Here is what I am trying to do:
String filePath = Windows.Storage.ApplicationData.Current.LocalFolder.toString;
// this returns error: "cannot convert method group 'ToString' to non-delegate type string"
String fileName = "2018_22_7.rtf"
// this is an example of a string my program would create according to the CalendarDatePicker's value.
At the end of my question is the code from the RichEditBox documentation. I want do away with the portion that uses the picker and replace the following line:
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
with:
Windows.Storage.StorageFile file = filePath + fileName;
Is it possible to do this or am I forced to use a picker with the RichEditBox?
I will deeply appreciate any help I can get in solving this problem. I am
melting from frustration! Thank you!
Here is the code from the RichEditBox documentation:
private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
Windows.Storage.Pickers.FileSavePicker savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Rich Text", new List<string>() { ".rtf" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Document";
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
// Prevent updates to the remote version of the file until we
// finish making changes and call CompleteUpdatesAsync.
Windows.Storage.CachedFileManager.DeferUpdates(file);
// write to file
Windows.Storage.Streams.IRandomAccessStream randAccStream =
await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
editor.Document.SaveToStream(Windows.UI.Text.TextGetOptions.FormatRtf, randAccStream);
// Let Windows know that we're finished changing the file so the
// other app can update the remote version of the file.
Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
if (status != Windows.Storage.Provider.FileUpdateStatus.Complete)
{
Windows.UI.Popups.MessageDialog errorBox =
new Windows.UI.Popups.MessageDialog("File " + file.Name + " couldn't be saved.");
await errorBox.ShowAsync();
}
}
}
Universal Windows Apps (apps) can access certain file system locations by default.
Before window version 17134, if you don't want use a picker to access the file, your UWP app only can access the files in the Application install directory, Application data locations, Removable devices and some Locations that UWP apps can access with specific Capability such as the Music and Pictures Libraries. But you can not write into the Application install directory. In this case, you can try to save the file in above location except the Application install directory. Such as the Application data locations then you can create and get the file using the following code. Please see the File access permissions for more details.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
//Createa file
StorageFile fileToSave = await localFolder.CreateFileAsync("YourFileName");
//Get file
StorageFile file = await localFolder.GetFileAsync("YourFileName");
If your app target on version 17134 and later, you can access all files that the user has access to using the broadFileSystemAccess Capability, this capability works for APIs in the Windows.Storage namespace, you can get the file using the path as following code, here is a sample.
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);

UWP . Copy file from FileOpenPicker to localstorage

FileOpenPicker picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
picker.FileTypeFilter.Add(".txt");
A user chooses a file to open. How can I store/copy/save that file to localstorage for future use, so every time the app opens, it picks automatically that file?
After the user opens the file using the FileOpenPicker you can "cache" access to it using StorageApplicationPermissions API.
Once you have the StorageFile you want to open automatically, you can "cache" your access to it using the following code:
string token = StorageApplicationPermissions.FutureAccessList.Add( file );
What you get back is a string token, which you can save for example in the app settings. Next time the app is opened, you can retrieve the file again using the following code:
StorageFile file =
await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
Note that this API has limitation of at most 1000 items stored, so if you expect that more could be added, you will have to ensure the older files are removed otherwise you would not be able to add new files.
There is also alternative - StorageApplicationPermissions.MostRecentlyUsedList which you can use the same way as the FutureAccessList, but it has the advantage of automatically managing the list. It can store up to 25 items, but it is able to automatically remove the oldest ones when not needed anymore.
Also note, that this APIs can cache access not only to files but also to folders (StorageFolder).
Copying the file to AppData folder
If you just want to create a local copy of the picked file, you can copy it to the local folder of the app.
var file = await picker.PickSingleFileAsync();
if ( file != null )
{
await file.CopyAsync( ApplicationData.Current.LocalFolder );
}
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
var yourPath = file.Path;
}
but It won't work as you expect. But remember you can't open file from location you (your app) don't have access to.
edit: yeah, I see in comments that I have missed some part of the qestion ;)
the easiest way to store the information for future re-use would be propably to use LocalSettings
https://msdn.microsoft.com/library/windows/apps/windows.storage.applicationdata.localsettings.aspx
(sorry for the link, but there is no use in copying info from there)
You could:
1) Store the file name in your project settings;
YourNameSpace.Properties.Settings.fileToLoad;
2) write the file name in a local file (look at TextWriter namespace);
3) store the file name in your database if your application is data-driven
... and others.
I am presuming here that you're using WinForms or Console app. If you are using a webForm, you would need to store the file name in a cookie so you could attach the right file to the right user before they log in or give you credenstials. For Webforms, then, look into the use of cookies.
Just to add to the above suggestions, following example from Official Microsoft document shows how to Store file for future access:
var openPicker = new FileOpenPicker();
StorageFile file = await openPicker.PickSingleFileAsync();
// Process picked file
if (file != null)
{
// Store file for future access
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
}
else
{
// The user didn't pick a file
}

Cannot read any files or directories outside of my application directory

This is really annoying problem and it's going to drive me mad. I like to read information such like files, directories ect. but my app cannot find anything OUTSIDE its folder it runs in.
I'm using Visual Studio 2015 and developing Windows Universal apps.
This routine under works very well if I change the directory inside the folder my app run like "Assets" and any other folder. But outside of my app folder result is zero, not even any errors :-(
Ok, Here is the simple code, What I Do Wrong?
private void GetThem_Click(object sender, RoutedEventArgs e)
{
string myDir = #"c:\mydir\";
string[] files;
files = Directory.GetFiles(myDir,"*.jpg");
foreach (string stuff in files)
{
RESULT.Text = RESULT.Text + stuff + " , ";
}
}
A quick search would have given you the answer : It is not possible to access the file system like a classic desktop app. The answer of #Rico Suter explain you what you can acces and how :
Directories which are declared in the manifest file (e.g. Documents, Pictures, Videos folder)
Directories and files which the user manually selected with the FileOpenPicker or FolderPicker
Files from the FutureAccessList or MostRecentlyUsedList
Files which are opened with a file extension association or via sharing
Once a file is picked by the user, you can add it to MostRecentlyUsedList or FutureAccessList to use it again later using this snippet (C#) from MSDN :
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
// Add to MRU with metadata (For example, a string that represents the date)
string mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "20120716");
// Add to FA without metadata
string faToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
}
Then store the retrieved token because you will need it to access the file using GetFileAsync(token)

How to display an image from a network folder or local drive in a Windows Universal App

I'm having trouble displaying an jpg in an Image control in a Windows Universal App. (I had the same problem trying to create a Windows 8 Store app as well)
I have a simple form with an Image control on it. All I want to do is be able to open images in a folder on my local drive or a network drive on my local network and display them. But I am not having any luck. The only thing I get is E_NETWORK_ERROR, with no additional information.
I assume it probably has something to do with security, but surely there must be a setting or permission to allow me to do it. I tried enabling Private Networks in the Capabilities tab of the manifest, but that didn't help. I don't see anything in Declarations that sounds like what I need.
I know UWP apps are somewhat sandboxed, but if you can't even access local files, what good are they?
Here is a sample of code I have tried. I've done other iterations as well, but they all have the same end result.
Xaml:
<Image Name="Image1"/>
Code behind:
public LoadImage()
{
var bitmap = new BitmapImage();
bitmap.ImageFailed += Bitmap_ImageFailed;
bitmap.UriSource = new Uri(#"D:\Users\Steve\Documents\SomeImage.JPG", UriKind.Absolute);
Image1.Source = bitmap;
}
private void Bitmap_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
Debug.Write(e.ErrorMessage);
}
When I run it, I end up in the Bitmap_ImageFailed event and the ErrorMessage property is simply "E_NETWORK_ERROR", and nothing is displayed in the Image. Not very helpful.
What am I missing? It has to be something simple and obvious that I am overlooking.
Update:
Thanks to all the suggestions here I was able to get it going. The part I was failing to get through my skull was that you can't just give it a folder and expect it to read a file, even as a "quick & dirty test". You have to go through "proper channels" to get there. I pieced it all together and came up with this example which displays the first image in the selected folder:
private async void Button_Click(object sender, RoutedEventArgs e)
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add(".jpg");
folderPicker.FileTypeFilter.Add(".tif");
folderPicker.FileTypeFilter.Add(".png");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
var files = await folder.GetFilesAsync();
var bitmap = new BitmapImage();
bitmap.ImageFailed += Bitmap_ImageFailed;
var stream = await files.First().OpenReadAsync();
await bitmap.SetSourceAsync(stream);
Image1.Source = bitmap;
}
}
In addition, I had to add the file types for .jpg, .tif and .png as well as File Open Picker to the Declarations.
You can figure out all necessary information in MSDN article File access permissions
In addition to the default locations, an app can access additional files and folders by declaring capabilities in the app manifest (see App capability declarations), or by calling a file picker to let the user pick files and folders for the app to access (see Open files and folders with a picker).
So if you want to read a file from users document folder you need to update your applications AppXManifest to request the Document Library Access capability.
You also need to update your AppXManifest by declaring what file type(s) you want to access. Then, even with access to the folders, you only have access to a limited set of file types. You have to specify supported files types on Declarations tab
I set a new file type (.txt) and let it role from there. And code example
async void Button_Click_2(object sender, RoutedEventArgs e)
{
var _Name = "HelloWorld.txt";
var _Folder = KnownFolders.DocumentsLibrary;
var _Option = Windows.Storage.CreationCollisionOption.ReplaceExisting;
// create file
var _File = await _Folder.CreateFileAsync(_Name, _Option);
// write content
var _WriteThis = "Hello world!";
await Windows.Storage.FileIO.WriteTextAsync(_File, _WriteThis);
// acquire file
try { _File = await _Folder.GetFileAsync(_Name); }
catch (FileNotFoundException) { /* TODO */ }
// read content
var _Content = await FileIO.ReadTextAsync(_File);
await new Windows.UI.Popups.MessageDialog(_Content).ShowAsync();
}

How to store roaming files in Windows 8?

I'm trying to store a file and roam it to other devices in Windows 8. The official documentation on this states:
Within its app data store, each app has system-defined root directories: one for local files, one for roaming files, and one for temporary files.
And further down, it states:
App files can be local or roaming. The files that your app adds to the local data store are present only on the local device. The system automatically synchronizes files your app adds to the roaming data store on all devices on which the user has installed the app.
However, it does not go on to state how files (not normal data) can be roamed.
Where do I go to find more about roaming files, and not just normal data?
Use the functions defined in: Windows.Storage.ApplicationData.Current.RoamingFolder
For example:
public async void RoamData()
{
var roamingFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;
var needToCreate = false;
try
{
var sampleFile = await roamingFolder.GetFileAsync("dataFile.txt");
string fooBar = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
}
catch (Exception)
{
// fooBar not found
needToCreate = true; // set a boolean to create the file. Cant be done here cause you cant await in a catch clause.
}
if (needToCreate)
{
var sampleFile = await roamingFolder.CreateFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "fooBar content of the file.");
}
}
MSDN: http://msdn.microsoft.com/en-us/lib...

Categories

Resources