PCLStorage FileSystem is only available in Android, not iOS - c#

I'm writing a cross platform application in xamarin, for Android and iOS, and I need to save some text files to local storage. I'm using PCLstorage, but whenever I mouse over any PCLStorage code, it says "MyApplication.Android: available, MyApplication.iOS not available". How can I use PCLStorage to store files on both platforms? Or is there another way I can do this? Here's an example of some of my code:
public async Task CreateRealFileAsync(string filename, string filebody)
{
// get hold of the file system
IFolder rootFolder = FileSystem.Current.LocalStorage;
// create a folder, if one does not exist already
IFolder folder = await rootFolder.CreateFolderAsync("DadAppFiles", CreationCollisionOption.OpenIfExists);
// create a file, overwriting any existing file
IFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
// populate the file with some text
await file.WriteAllTextAsync(filebody);
}

Related

How can I upload and read a file on a raspberry pi running Windows 10 IoT

I have built a app that read a config file and load a dictionary based on the xml.
What I want is to not need to add that file to the program. Instead I want to be able to upload to the pi3 and then tell the program to refresh and read the file I uploaded. It loads the file included with the code to an obscure folder.
How can I upload and specify the path in my code to a folder that is easier to get to.
Thanks in advance
Use the Windows.Storage namespace and following the below code for creating,accessing and other operations like these in Folder and Files on UWP.
//Get Installation Folder for current application
StorageFolder rootFolder = ApplicationData.Current.LocalFolder;
//Create a folder in the root folder of app if not exist and open if exist.
StorageFolder Folder = await rootFolder.CreateFolderAsync(yourFolderName, CreationCollisionOption.OpenIfExists);
//Craete a file in your folder if not exist and replace it with new file if exist
StorageFile sampleFile = await Folder.CreateFileAsync("samplefile.xml", CreationCollisionOption.ReplaceExisting);
//Create your text and write it on your configuaration file
string yourText = "Your Sample Text or Configuration";
await Windows.Storage.FileIO.WriteTextAsync(sampleFile, yourText);
After that you can get access the file again read your configuration value from that form ReadTextAsync method.
//Read data from file
await Windows.Storage.FileIO.ReadTextAsync(sampleFile);

Writing a file into the Downloads folder with UWP goes into Isolated Storage?

I need to create a file in the downloads folder for a UWA on Windows 10 and copy the content of an existing file into it. I use the following code:
StorageFile cleanFile = await Windows.Storage.DownloadsFolder.CreateFileAsync(cleanFileName);
await file.CopyAndReplaceAsync(cleanFile);
This works ok, but the folder the file is stored is this:
C:\Users\MyUser\Downloads\e15e6523-22b7-4188-9ccf-8a93789aa8ef_t8q2xprhyg9dt!App\WordComment-clean.docx
I assume this is some type of Isolated Storage. But really, that is not what I need. Since the user can't see the file like this.
From MSDN:
User’s Downloads folder. The folder where downloaded files are saved by default.
By default, your app can only access files and folders in the user's Downloads folder that your app created. However, you can gain access to files and folders in the user's Downloads folder by calling a file picker (FileOpenPicker or FolderPicker) so that users can navigate and pick files or folders for your app to access.
If you don't use a file picker, you are saving the file in your app folder in Downloads.
Souce
Using the Download folder
For using the download folder, the user needs to select the download folder manually.
FolderPicker picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.Downloads };
picker.FileTypeFilter.Add("*");
StorageFolder folder = await picker.PickSingleFolderAsync();
if (folder != null) {
await folder.CreateFileAsync("Hello1.txt");
}
I hope this can help you.
Try this:
await file.CopyAndReplaceAsync(cleanFile);
await file.RenameAsync("the name you want")

Creating and accessing a folder with in the application for windows phone 8.1

Can some one help me with creating and accessing folder with in the application (at the same place where we have our assets,html folders). My requirement is to download a file in that folder and then access it.
I have used:
StorageFolder destinationFolder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");
StorageFile localFile = await destinationFolder.CreateFileAsync(localFileName, CreationCollisionOption.ReplaceExisting);
when I checked it showing the path as: Assets folder path: C:\Data\SharedData\PhoneTools\AppxLayouts\785bb4a5-5b27-4720-918e-7ceaeeb58c52VS.Debug..
If your file saved is not being for any use to end user directly then you should use App local folders. If you have very few amount of data then you can use LocalSettings also. for more available storage options and process of accessing check this link.

Creation of a File

I have created simple UWP application, where I simply want to store serialized data to a file, which can be accessed later (when user reopens the application after a while).
The place, where I want to store the file is the current installed location and my serialization code looks like following:
private void SerializeData()
{
XmlSerializer ser = new XmlSerializer(typeof(ObservableCollection<MyTask>));
Windows.Storage.StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
using (var writer = new StreamWriter(File.Open(Path.Combine(installedLocation.Path,sFileName), FileMode.OpenOrCreate)))
{
try
{
ser.Serialize(writer, sData);
}
catch (Exception ex) { }
}
}
This is MyTask model:
public class MyTask:ViewModelBase
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
}
The error that I've been getting is UnauthorizedAccessException. I remember that when I was creating my regular Windows 7 apps under WPF, I had no problem with permission. This is my first UWP app, and therefore I might have forgotten to do something with permissions.
The questions is - is it possible to store simple file in installed directory, or do I need to store all my data files under some Shared location?
Package.InstalledLocation is a place where your app is installed and it's read-only - so you cannot write files there. Use ApplicationData.LocalFolder instead - this is the folder where your app should store local data. For more info how to store (and where), please take a look at MSDN.
Apps can access certain file system locations by default. Apps can also access additional locations through the file picker, or by declaring capabilities.
The locations that all apps can access
When you create a new app, you can access the following file system locations by default:
Application install directory. The folder where your app is installed on the user’s system.
There are two primary ways to access files and folders in your app’s install directory:
You can retrieve a StorageFolder that represents your app's install directory, like this:
Windows.Storage.StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
You can retrieve a file directly from your app's install directory by using an app URI, like this:
using Windows.Storage;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///file.txt");
The app's install directory is a read-only location. You can’t gain access to the install directory through the file picker.
Application data locations. The folders where your app can store data. These folders (local, roaming and temporary) are created when your app is installed.
There are two primary ways to access files and folders from your app’s data locations:
Use ApplicationData properties to retrieve an app data folder.
For example, you can use ApplicationData.LocalFolder to retrieve a StorageFolder that represents your app's local folder like this:
using Windows.Storage;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
you can retrieve a file directly from your app's local folder by using an app URI, like this:
using Windows.Storage;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appdata:///local/file.txt");
User’s Downloads folder:
using Windows.Storage;
StorageFile newFile = await DownloadsFolder.CreateFileAsync("file.txt");
and much much more ... for further reference goto dev.windows.com and you will get a ton of resources.

'System.UnauthorizedAccessException' on reading file no extension

I am trying to read the chrome bookmark file within a windows 8 app. Problem is I am getting the 'System.UnauthorizedAccessException' exception. So basically you have to register a file type association in the app manifest but the file has no extension.
File: C:\Users\<User>\AppData\Local\Google\Chrome\User Data\Default\Bookmarks
Is this even possible in windows 8 apps?
UPDATE
Here is my file access code:
public async Task<string> ReadFile(string filename)
{
if (await FileExists(filename))
{
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(filename);
var stream = await file.OpenReadAsync();
var reader = new StreamReader(stream.AsStream());
return await reader.ReadToEndAsync();
}
else return string.Empty;
}
Windows 8 app cannot access any random file system location. It can only access
Application Folder (which is specific to your app)
Common Folders like My Pictures, My Video and similar
See this article for complete details
A workaround is to use the file picker. This allows the end user to manually select files on the filesystem. Once you have the FileStorage object from the file picker, you can then open and read the file.
Optionally, you can then save the StorageFile for later use, meaning that your app can access the file later, without the end user having to select the file again.
More info on MSDN:
File Open Picker
How to track recently used files and folders

Categories