I have created local folder for downloading files using followling code
StorageFile destinationFile;
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var dataFolder = await local.CreateFolderAsync("AppDownloads",
CreationCollisionOption.OpenIfExists);
destinationFile = await dataFolder.CreateFileAsync(destination,
CreationCollisionOption.GenerateUniqueName);
Now i need to access all downloaded files from the sub folder created.
I have tried using:
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
IStorageFolder dataFolder1 = await local.GetFolderAsync("AppDownloads");
IEnumerable<IStorageFile> files = await local.GetFilesAsync();
But this is not working.How to get all downloaded files from this folder ?
Thank you.
Related
I'm using FileIO to append Json data in a LocalStorage file.
public static async Task AppendToJsonLocalStorage<T>(string filename, T objectToWrite) where T : new()
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile saveFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite);
await FileIO.AppendTextAsync(saveFile, contentsToWriteToFile);
}
AppendTextAsync should only add new text at the end of the existing file, wright ?
Because when I check the file in my file explorer with a text editor it's always overwriting the former text in it.
Use CreationCollisionOption.OpenIfExists instead of CreationCollisionOption.ReplaceExisting when you create the file:
StorageFile saveFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite);
await FileIO.AppendTextAsync(saveFile, contentsToWriteToFile);
ReplaceExisting replaces any existing file as the name suggests. Please refer to the docs for more information.
I'm trying to copy a StorageFile to StorageFolder
StorageFile timeStampFile = timeStampFiles.First(f => f.Name == sourceFile.Name);
Debug.WriteLine("TS FILE DATE=" + timeStampFile.DateCreated.DateTime);
//TS FILE DATE=9/6/2018 7:14:43 PM
StorageFile createdCopy = await timeStampFile.CopyAsync(destinationFolder);
Debug.WriteLine("COPIED FILE DATE=" + createdCopy.DateCreated.DateTime);
//COPIED FILE DATE=9/6/2018 7:36:30 PM
But DateCreated.DateTime of the created copy is different from the original file. How can I copy file with same DateCreated.DateTime info?
I want to open a text file with Open File Picker and show in a RichEditBox, but when I select the file and push Ok, Visual Studio show "Access Denied", I want to know how to solve this please, there is my code:
var picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.FileTypeFilter.Add("*");
picker.FileTypeFilter.Add(".txt");
picker.FileTypeFilter.Add(".text");
picker.FileTypeFilter.Add(".bat");
picker.FileTypeFilter.Add(".js");
picker.FileTypeFilter.Add(".vbs");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile filepath = await StorageFile.GetFileFromPathAsync(file.Path);
string text = await FileIO.ReadTextAsync(filepath);
RichEditBox1.Document.SetText(Windows.UI.Text.TextSetOptions.None, text);
}
You don't need to call StorageFile.GetFileFromPathAsync(file.Path) since you already have this StorageFile in the file variable returned from PickSingleFileAsync:
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
string text = await FileIO.ReadTextAsync(file);
RichEditBox1.Document.SetText(Windows.UI.Text.TextSetOptions.None, text);
}
The unnecessary GetFileFromPathAsync probably throws an AccessDenied error since the FileOpenPicker provides access only through the returned StorageFile and doesn't give direct access to the file through its path. This behavior is version dependent and new versions of Windows 10 will allow more direct access through file system API (see the Build 2017 talk UWP Apps file access improvements
I am trying to load a pdf file from a folder in my win8 app. It is in the test folder. I try
StorageFile file = StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Test/example.pdf"));
That gives me a file not found. However, if I change the extension of example.pdf to jpg, then change the code to look for example.jpg, it does work properly. What is going on?
Try this:
public async Task<StorageFile> GetFileAsync(string directory, string fileName)
{
string filePath = BuildPath(directory, fileName);
string directoryName = Path.GetDirectoryName(filePath);
StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(directoryName);
StorageFile storageFile = await storageFolder.GetFileAsync(fileName);
return storageFile;
}
public string BuildPath(string directoryPath, string fileName)
{
string directory = Path.Combine(Package.Current.InstalledLocation.Path, directoryPath);
return Path.Combine(directory, fileName);
}
From your client, pass-in the directory (relative to the project) path of the file and then supply the file name for the second parameter.
In addition, it's probably good that you just create a library to manage various file operations.
I need to save Image from Local Storage Folder to file selected from SavePicker.
Should I use stream?
Here is my code:
StorageFile file = await savePicker.PickSaveFileAsync();
if (null != file)
{
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile current_photo = await localFolder.GetFileAsync(img.Tag.ToString());
// TODO Stream from current_photo to file
}
CopyAsync should do it:
await current_photo.CopyAndReplaceAsync(file);