DownloadsFolder crashes if using CreationCollisionOption.ReplaceExisting - c#

I create a file that I want to be automatically saved to the Downloads folder of a user. So I used:
StorageFile file = await DownloadsFolder.CreateFileAsync("filename.txt");
But if I create that file again, I get an error stating that file already exists. So I used:
StorageFile file = await DownloadsFolder.CreateFileAsync("filename.txt", CreationCollisionOption.ReplaceExisting);
However that also crashes with the error saying that the parameter does not fall within the expected range. But if I use:
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename.txt", CreationCollisionOption.ReplaceExisting);
Then it does create the file even if it exists, but I need to create/save the file in the Downloads folder for easier access for the user. I thought of this (attempted) solution because of the issue described here prevented me from adding an attachment to certain e-mail clients.

The method CreateFileAsync (String) uses the FailIfExists option as the default. So when you create that file again, you get an error stating that file already exists.
For more info, see CreationCollisionOption.
The DownloadsFolder.CreateFileAsync("filename.txt", CreationCollisionOption.ReplaceExisting) method crashes with the error saying that the parameter does not fall within the expected range.
Because the app can only access files in the Downloads folder that it created, you can't specify OpenIfExists or ReplaceExisting for this parameter.
Please see DownloadsFolder.CreateFileAsync(String, CreationCollisionOption) document’s Parameters section(the last paragraph).
You can use DownloadsFolder.CreateFileAsync("filename.txt", CreationCollisionOption.GenerateUniqueName) to create the file.
It can automatically append a number to the base of the specified name if the file or folder already exists.
If you want to overwrite the exists file in DownloadsFolder. You can use FileSavePicker.PickSaveFileAsync to returns a storageFile object that was created to represent the saved file, and use FileIO.WriteTextAsync(IStorageFile, String) method to write text to the specified file. It can overwrite the old file.
For example:
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.Downloads;
savePicker.FileTypeChoices.Add("Simple Line Files", new List<string>() { ".txt" });
savePicker.SuggestedFileName = "filename";
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
await FileIO.WriteTextAsync(file, "hello");
}

Related

UWP C# Windows IoT Read Text Line to Process Word [duplicate]

I am trying to read a text file named thedata.txt that has a list of words that I want to use in a hangman game. I have tried different ways, but I can't figure out where the file gets placed, if at all when the app runs. I added the file to my project, and I have tried setting the build properties to content, and then embedded resource, but can't find the file. I have made a Windows 10 universal app project. The code I tried looks like this:
Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("thedata.txt");
using (StreamReader inputStream = new StreamReader(stream))
{
while (inputStream.Peek() >= 0)
{
Debug.WriteLine("the line is ", inputStream.ReadLine());
}
}
I get exceptions.
I also tried to list the files in another directory:
string path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
Debug.WriteLine("The path is " + path);
IReadOnlyCollection<StorageFile> files = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync();
foreach (StorageFile file2 in files)
{
Debug.WriteLine("Name 2 is " + file2.Name + ", " + file2.DateCreated);
}
I don't see the file there either...I want to avoid hard coding the list of names in my program. I'm not sure what the path that the file is placed.
the code is very simple, you just have to use a valid scheme URI (ms-appx in your case) and transform your WinRT InputStream as a classic .NET stream :
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///thedata.txt"));
using (var inputStream = await file.OpenReadAsync())
using (var classicStream = inputStream.AsStreamForRead())
using (var streamReader = new StreamReader(classicStream))
{
while (streamReader.Peek() >= 0)
{
Debug.WriteLine(string.Format("the line is {0}", streamReader.ReadLine()));
}
}
For the properties of the embedded file, "Build Action" must be set to "Content" and "Copy to Ouput Directory" should be set to "Do not Copy".
You can't use classic .NET IO methods in Windows Runtime apps, the proper way to read a text file in UWP is:
var file = await ApplicationData.Current.LocalFolder.GetFileAsync("data.txt");
var lines = await FileIO.ReadLinesAsync(file);
Also, you don't need a physical path of a folder - from msdn :
Don't rely on this property to access a folder, because a file system
path is not available for some folders. For example, in the following
cases, the folder may not have a file system path, or the file system
path may not be available. •The folder represents a container for a
group of files (for example, the return value from some overloads of
the GetFoldersAsync method) instead of an actual folder in the file
system. •The folder is backed by a URI. •The folder was picked by
using a file picker.
Please refer File access permissions for more details.
And Create, write, and read a file provides examples related with File IO for UWP apps on Windows 10.
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");

FileInfo.Path shows Value does not fall within the expected range exception when move file to another folder

How to cope with the path that exceeds the maximum character?
I want to move a file to another folder that is in accordance with the wishes of the user. But is plagued by error messages, as shown below:
code:
string path = (carousel.SelectedItem as Book).FileInfo.Path;
StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri((carousel.SelectedItem as Book).FileInfo.Path, UriKind.Absolute));
await file1.CopyAsync(ApplicationData.Current.LocalFolder, ((carousel.SelectedItem as FileInformation).Name), NameCollisionOption.ReplaceExisting);
Windows.UI.Popups.MessageDialog a = new Windows.UI.Popups.MessageDialog("Moved" + (carousel.SelectedItem as Book).Name, "To" + ApplicationData.Current.LocalFolder.Path);
await a.ShowAsync();
How to handle it?
This doesn't look like it's related to exceeding maximum characters. GetFileFromApplicationUriAsync expects an application URI (e.g. "ms-appx:///foo.txt") not an arbitrary path.
Since you have a full path use StorageFile.GetFileFromPathAsync
If the FileInfo already includes the StorageFile then use that directly rather than converting to a path and back.

ReadLinesAsync in UWP from absolute file path

I get error
The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)
My code is
public async void ReadFile()
{
var path = #"F:\VS\WriteLines.xls";
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync(path);
var readFile = await Windows.Storage.FileIO.ReadLinesAsync(file);
foreach (var line in readFile.OrderBy(line =>
{
int lineNo;
var success = int.TryParse(line.Split(';')[4], out lineNo);
if (success) return lineNo;
return int.MaxValue;
}))
{
itemsControl.Items.Add(line);
}
}
The error shows up at var file = await folder.GetFileAsync(path);
You cannot read a file from an arbitrary location on disk in a UWP App.
There are a couple of ways you can still accomplish your task:
You can add the WriteLines.xls file to your project and set it's build action to Content and Copy to output Directory to Copy if newer. You can then access the file with the code you have by simply replacing the "path" value to:
var path = #"WriteLines.xls"
More details here
If you need to be able to read any files from disk, you need to either use a FilePicker to select the file or copy the file in the Documents Folder and change the folder to:
var folder = KnownFolders.DocumentsLibrary;
More details here
You are asking for file with absolute path from application's local folder - hence it throws that error as you provide path that includes drive name.
In general UWP is very restrictive on where/how you can get files from - I don't think you can get it from absolute path in the sample (app needs more permissions to get to similar places). You can try StorageFile.GetFileFromPathAsync.
Detailed info on locations app can access - UWP apps on Windows 10: File access permissions.

C# OpenFileDialog open zip folder containing single file?

I have an application which currently provides the user with the ability to view a PDF file inside the application by using File->Open, browsing to the location of the PDF file using a Microsoft.Win32.OpenFileDialog, and then displaying that PDF file in a System.Windows.Controls.WebBrowser in the GUI.
I am creating the OpenFileDialog and setting the file extensions it can open using:
/*Create Open File dialog */
Microsoft.Win32.OpenFileDialog OFDlg = new Microsoft.Win32.OpenFileDialog();
/*Set filter for file extension and default file extension */
OFDlg.DefaultExt = ".pdf";
OFDlg.Filter = "PDF Documents (.pdf)|*.pdf";
I now want to extend this, so that the user can open a ZIP folder containing a single PDF document, and display that PDF document in the same way that I am above.
I tried changing the filter to allow .zip files, i.e.
OFDlg.DefaultExt = ".pdf|.zip";
OFDlg.Filter = "PDF Documents (.pdf)|*.pdf|ZIP|*.zip";
but when I browse to the location of the .zip file in the OpenFileDialog, the .zip folder is not displayed there- only normal folders and PDF documents (other documents in that directory, such as .doc & .xls are not displayed in the OpenFileDialog).
My reason for wanting to be able to open the contents of a .zip file directly from the .zip, rather than navigating to that file itself, is so that I can add public/private key encryption to the .zip, so that its contents can only be read securely.
I know that there could in theory be problems if the .zip contains more than one file, but I intend to send each encrypted file in its own zip folder, so it can be assumed that any zip file that the user is trying to open contains a single .pdf, and nothing else.
So my questions are:
How can I make .zip folders visible from the OpenFileDialog?
How can I make the selection of that .zip folder automatically open and display its contents (a single PDF file) in the System.Window.Controls.WebBrowser that I am currently using to display PDFs in my GUI?
Edit 1
I tried changing my OpenFile() method to the following code:
/*Set filter for file extension and default file extension */
OFDlg.DefaultExt = ".pdf";
OFDlg.DefaultExt = ".zip";
OFDlg.Filter = "PDF Documents (.pdf)|*.pdf";
OFDlg.Filter = "ZIP Folders (.ZIP)|*.zip";
but when I now run my application, and browse to the same location, although the .zip folder is now shown in the OpenFileDialog, the .pdf files no longer are... and if I double click the .zip folder, my application breaks, and I get a runtime error on the line
PdfPanel.OpenFile(docFP);
which says:
An unhandled exception of type 'System.AccessViolationException' occurred in MoonPdfLib.dll
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I guess that's to do with the MoonPDF library that I'm using to read the PDFs being unable to handle the .zip extension?
How would I resolve this to be able to open the PDF inside the ZIP?
Edit 2
Ok, so I've resolved the issue about only being able to see either the PDF files or the .ZIP folders (not both at the same time), by moving the functionality into two separate methods- one to display the PDF direct from the PDF's filepath, and another to display the PDF from the path of the .ZIP folder holding it.
The method for displaying the PDFs directly currently works (it is essentially the code in the first bit of code I've quoted). However the method for displaying the PDFs from the ZIP currently doesn't work...
I understand the reason for this- it's because I am passing a .zip folder to the OpenFile method... The code for this method currently looks like this:
private void openZipMenuItem_click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog OZipDlg = new Microsoft.Win32.OpenFileDialog();
OZipDlg.DefaultExt = ".zip";
OZipDlg.Filter = "ZIP Folder (.zip)|*.zip";
Nullable<bool> result = OZipDlg.ShowDialog();
if (result == true)
{
/*Open document */
string filename = OZipDlg.FileName;
//fnTextBox.Text = filename;
zipFP = OZipDlg.FileName;
/*browser.Navigate(docFP); ERF (27/05/2016 # 0935) Comment this line- I want to use PdfPanel to open docFP, not browser */
Console.WriteLine("Panel height: " + PdfPanel.ActualHeight);
PdfPanel.OpenFile(zipFP);
}
}
When I try to call this function to open a .zip, I get a runtime exception which says:
AccessViolationException was unhandled
An unhandled exception of type 'System.AccessViolationException' occurred in MoonPdfLib.dll
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I understand that I can't display a Zip folder in the PdfPanel (which is a MoonPdfPanel that I am using from the MoonPdfLibrary), so I know that I will get an exception here.
How can I pass the contents of zipFP to the call to PdfPanel.OpenFile(), rather than passing zipFP itself to it?
Edit 3
Ok, so my code is currently extracting the PDF file successfully from the ZIP folder when I open it- I can see that it is copied to the directory I have specified. I am now trying to get the PDF to be displayed automatically in the PDF Panel on my application- I've done this by adding the following code:
try{
string extractPath = #"C:\Documents";
using(ZipArchivev zip = ZipFile.Open(zipFP, ZipArchiveMode.Read))
foreach(ZipArchiveEntry entry in zip.Entries){
try{
ZipFile.ExtractToDirectory(zipFP, extractPath);
Console.WriteLine("zipFP: " + zipFP);
}catch(System.IOException){
Console.WriteLine("File already exists...");
}
}
string ExtractedPDF = string.Concat(extractPath, zipFP);
PdfPanel.OpenFile(ExtractedPDF);
}catch(AccessViolationException ex){
Console.WriteLine("Can't display a zip in the PDF panel..." + ex.InnerException);
}
But when my code tries to execute the line PdfPanel.OpenFile(ExtracedPDF);, I get an exception that says:
FileNotFoundException was unhandled | An unhandled exception of type 'System.IO.FileNotFoundException' occurred in MoonPdfLib.dll'
I understand that this is happening because the variable I am trying to display in the PDFPanel, ExtractedPDF actually holds the path of the folder containing the PDF, and not the PDF itself- How do I give it the name of the PDF file, when I don't actually know what the PDF file will be called?
Here is something works similar to your requests, the logic behind the code is:
Only display zip and pdf files in the OpenFileDialog
If user selected a pdf file, show it in the panel
If user selected a zip file, change the directory of the OpenFileDialog to the zip file(treat it like a folder)
Example code (working code....):
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "PDF files (.pdf)|*.pdf;*.zip";
ofd.ShowDialog();
//reopen OpenFileDialog if it is zip file. this part can be improved.
if (ofd.FileName.EndsWith(".zip"))
{
ofd.InitialDirectory = ofd.FileName;
ofd.ShowDialog();
}
//if it's a PDF, note that you don't really need this check,
//as the only file can reache here will be a PDF,
//and it can be the temporary file that inside a zip.
if(ofd.FileName.EndsWith(".pdf"))
{
//show it in your PdfPanel
}
Edit, based on your new comments and added code. you need to change your code to the following as your current code is mistaken directory with the file:
try{
string extractPath = #"C:\Documents";
string ExtractedPDF ="";
using(ZipArchivev zip = ZipFile.Open(zipFP, ZipArchiveMode.Read))
foreach(ZipArchiveEntry entry in zip.Entries){
try{
ExtractedPDF= Path.Combine(extractPath, entry.FullName);
entry.ExtractToFile(ExtractedPDF,true);
}catch(System.IOException){
Console.WriteLine("error during extraction...");
}
}
if( System.IO.File.Exists(ExtractedPDF))
{
PdfPanel.OpenFile(ExtractedPDF);
}
}catch(AccessViolationException ex){
Console.WriteLine("Can't display a zip in the PDF panel..." + ex.InnerException);
}
If you want to support multiple file formats in an open file dialog, you need to add a third (or better first) option, that aggregates all supported file extensions:
OFDlg.Filter = "Supported file formats|*.pdf;*.zip|PDF Documents|*.pdf|ZIP files|*.zip";
First, regarding showing the files in the open file dialog. Your initial method for doing this was correct. Your updated code now first sets the filter to show PDFs, then replaces that filter with one that shows zip files. The standard file open dialog isn't designed to show different file types at the same time. The right way to handle that is to give the user the option for which file types they want to show.
Typically, an "All files" option is added as well (with . as the search pattern). This way if the file type the user wants to open isn't available in the list, they can see it regardless.
As for opening the PDF file that is in the zip file, you need to take are of extracting the PDF file yourself. This question has some options for how to do that.

c# UWP Save StorageFile Without Dialog

I am in trouble with some issue about FileSavePicker. Is there any solution about saving a StorageFile without showing any popup or dialog to ask user. I want to give the current path of the storage file from code behind.
var byteArray = Convert.FromBase64String(Base64);
StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("file.jpg", CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBytesAsync(file, byteArray);
var savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add("JPEG-Image", new List<string>() { ".jpg" });
savePicker.SuggestedSaveFile = file;
savePicker.PickSaveFileAsync();
...We have to use a few paths which are defined under
'Windows.Storage.KnownFolders'...
it is not so. In fact your app can access any folder on device but it will need additional permissions. The most straightforward way to obtain permission you should do next:
1) ask user to pick folder from FolderPicker
2) store selected folder to StorageApplicationPermissions.FutureAccessList
After this your app can do anything with this folder.
Code that demonstrate how to obtain permissions:
var picker = new FolderPicker();
var pfolder = await picker.PickSingleFolderAsync();
StorageApplicationPermissions.FutureAccessList.Add(pfolder);
Code that demonstrate how to create file in desired folder:
var folder = await StorageFolder.GetFolderFromPathAsync("your path");
var file = await folder.CreateFileAsync("text.txt");
using (var writer = await file.OpenStreamForWriteAsync())
{
await writer.WriteAsync(new byte[100], 0, 0);
}
But keep in mind that "your path" is folder or any of subfolder that was stored to StorageApplicationPermissions.FutureAccessList. More details here FutureAccessList
StorageFile.GetFileFromPathAsync() is probably what you are looking for. I think the file must be created beforehand for this method to work. Be aware that creating the file in some locations might need additional permissions or is even impossible (I don't know how UWP and UAC interact).
Another possibility is to use Win32 APIs inside UWP apps. Maybe this can solve your problem if the desired API is available.
I want to thank you all for your interest. I figured out the solution. If you do not want to use any dialog or prompting you can not use FileSavePicker. Here are the simple codes you need to converting Base64 string to image and save.
var byteArray = Convert.FromBase64String(Base64);
StorageFile file = await Windows.Storage.KnownFolders.SavedPictures.CreateFileAsync(
"file.jpg", CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBytesAsync(file, byteArray);
I guess there is no way to give specific path. We have to use a few paths which are defined under Windows.Storage.KnownFolders. If it is not, please give some information about it.

Categories