Can You Use URI's from a Library (PicturesLibrary) In UWP? - c#

I'm trying to use this code to access a file's URI from the PicturesLibrary:
StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
StorageFile tpkFile = await picturesFolder.GetFileAsync("campus.tpk");
Uri tpkFileUri = new Uri(tpkFile.Path);
Debug.WriteLine("This is the tpkFile path: " + tpkFileUri);
ArcGISTiledLayer tpkLayer = new ArcGISTiledLayer(tpkFileUri);
try
{
MyMapView.Map.OperationalLayers.Add(tpkLayer);
}
catch(Exception e)
{
Debug.WriteLine("This is the Exception: " + e);
}
Which returns a URI of:
file:///C:/Users/username/Pictures/campus.tpk
But I know UWP apps only take URIs that start with ms-appx:///
My question is, how can I can I access the file at the first URI with the ms-appx:/// prefix?
Right now the code fails at this line:
ArcGISTiledLayer tpkLayer = new ArcGISTiledLayer(tpkFileUri);
Because it says the URI is not correct, so the reference to tpkLayer is null.

Copy it to your ApplicationData.Current and use ms-appdata:/// would that work for you? ms-appx:/// reference to the files in your appx which is readonly.
here you can read more about the ms-appdata schema: https://blogs.windows.com/buildingapps/2014/06/19/common-questions-and-answers-about-files-and-app-data-part-1-app-data/#j4m8ylp93K7ujk6Q.97

Related

System.IO.FileNotFound when passing file path value to OpenXML GetCellValue()

When I pass the value from the OpenFilePicker() method back to the button click method, I can utilize a debug string and ensure that the value is not null.
However, when I pass it to the GetCellValue() method, a 'FileNotFound' exception is thrown. Utilizing a debug statement here also shows that the value is not null and returns a valid file path of "C:\Test.xlsx".
Tried changing file permissions to RWX for all, attempted different folder locations. All permissions and folders seem to have the same issue.
public async void FileSelectButton_ClickAsync(object sender, RoutedEventArgs e)
{
string filePath = await openFilePicker();
//Debug.WriteLine("result:: " + filePath);
GetCellValue(filePath, "Sheet1", "A1");
}
public async Task<string> openFilePicker()
{
var archerReportPicker = new
Windows.Storage.Pickers.FileOpenPicker();
archerReportPicker.ViewMode =
Windows.Storage.Pickers.PickerViewMode.Thumbnail;
archerReportPicker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.Downloads;
archerReportPicker.FileTypeFilter.Add(".xlsx");
archerReportPicker.FileTypeFilter.Add(".xls"); // Default extensions
Windows.Storage.StorageFile archerReport = await archerReportPicker.PickSingleFileAsync(); //Get file
if (archerReport != null)
{
// Application now has read/write access to the picked file
this.fileTextBox.Text = archerReport.Name; // Load it up and throw the data in the textbox.
var filePath = archerReport.Path;
return filePath;
}
else
{
this.fileTextBox.Text = "";
return null;
}
}
public static string GetCellValue(string fileName, string sheetName, string addressName)
{
string value = null;
// Open the spreadsheet document for read-only access.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false)) //Line where exception is thrown
{...}
Throws System.IO.FileNotFound Exception as opposed to opening valid file path.
The issue also occurs when filePath or fileName is defined using const string '#c:\test.xlsx'
The short answer to this question is here:
https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/
The gist of it is that in UWP, Storage Pickers return a non-filesystem bound Windows.Storage object. You can glean the filesystem path from the object, but because you are performing an operation on a secondary object, the fact that the user gave permissions for the first object does not apply to the second, resulting in an Access Denied condition when attempting to open the file - even if NTFS permissions allow 'Everyone' access.
This can be confirmed by monitoring the application using Process Monitor from SystemInternals.
If I discover a work-around to this issue, I will update this answer, but I will likely move away from UWP back towards a Windows Forms Application to avoid this issue entirely.

UWP ZipFile.CreateFromDirectory

I have the following code
private static async Task CreateZipFile(string folderPath)
{
await Task.Run(() =>
{
try
{
ZipFile.CreateFromDirectory(folderPath, ApplicationData.Current.LocalFolder.Path + "backup.zip");
}
catch (Exception e)
{
;
}
});
}
In my UWP app. However I am getting access denied errors on the LocalFolder.
The directory I am trying to zip is in my LocalState folder so
folderPath = C:\Users\username\AppData\Local\Packages\MyApp_3y0bchp7kwvet\LocalState\BACKUP
Any ideas how to resolve? Other code has no problem accessing these folders.
I'd think the problem here is that you've used wrong path as destinationArchiveFileName in ZipFile.CreateFromDirectory method.
For a valid path, it should be ApplicationData.Current.LocalFolder.Path + "\\backup.zip".
Once you changed your code like the following, it should be able to work.
ZipFile.CreateFromDirectory(folderPath, ApplicationData.Current.LocalFolder.Path + "\\backup.zip");

Directory.GetDirectories return empty string inside an async Task operation

I have a UWP application which perform to capture and process images from a camera. This project leverage Microsoft Cognitive Services Face Recognition API and I'm exploring the application's existing functionality for awhile now. My goal is that when the image of a person is identified by the camera (through Face Recognition API service), I want to show the associated image of that person.
With that, the images are captured and stored in a local directory of my machine. I want to retrieve the image file and render it on the screen once the person is identified.
The code below shows the async Task method ProcessCameraCapture
private async Task ProcessCameraCapture(ImageAnalyzer e)
{
if (e == null)
{
this.UpdateUIForNoFacesDetected();
this.isProcessingPhoto = false;
return;
}
DateTime start = DateTime.Now;
await e.DetectFacesAsync();
if (e.DetectedFaces.Any())
{
string names;
await e.IdentifyFacesAsync();
this.greetingTextBlock.Text = this.GetGreettingFromFaces(e, out names);
if (e.IdentifiedPersons.Any())
{
this.greetingTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.GreenYellow);
this.greetingSymbol.Foreground = new SolidColorBrush(Windows.UI.Colors.GreenYellow);
this.greetingSymbol.Symbol = Symbol.Comment;
GetSavedFilePhoto(names);
}
else
{
this.greetingTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Yellow);
this.greetingSymbol.Foreground = new SolidColorBrush(Windows.UI.Colors.Yellow);
this.greetingSymbol.Symbol = Symbol.View;
}
}
else
{
this.UpdateUIForNoFacesDetected();
}
TimeSpan latency = DateTime.Now - start;
this.faceLantencyDebugText.Text = string.Format("Face API latency: {0}ms", (int)latency.TotalMilliseconds);
this.isProcessingPhoto = false;
}
In GetSavedFilePhoto, I passed the string names argument once the person is identified.
Code below for the GetSavedFilePhoto method
private void GetSavedFilePhoto(string personName)
{
if (string.IsNullOrWhiteSpace(personName)) return;
var directoryPath = #"D:\PersonImages";
var directories = Directory.GetDirectories(directoryPath);
var filePaths = Directory.GetFiles(directoryPath, "*.jpg", SearchOption.AllDirectories);
}
However, in GetSavedFilePhoto method the variable directories returned an empty string of array when using directoryPath string variable. Directory "D:\PersonImages" is a valid and existing folder in my machine and, it contains subfolders with images inside. I also tried Directory.GetFiles to retrieve the jpg images but still returned an empty string.
I think it should work because I have used Directory class several times but not inside an asyncTask method. Does using async caused the files not returned when using I/O operation?
Sorry for this stupid question, but I really don't understand.
Any help is greatly appreciated.
Using Directory.GetFiles or Directory.GetDirectories method can get the folder/file in the local folder of the Application by the following code. But it could not open D:\.
var directories = Directory.GetDirectories(ApplicationData.Current.LocalFolder.Path);
In UWP app you can only access two locations at default (local folder and install folder), others need capabilities setting or file open picker.Details please reference file access permission.
If you need access to all files in D:\, the user must manually pick the D:\ drive using the FolderPicker, then you have permissions to access to files in this drive.
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
}
else
{
//do some stuff
}

How can I fix this DirectoryNotFoundException?

I have a DirectoryNotFoundException on a .txt file if I use the full path it's working but I don't want to use the full path because I want the program work no matter where it is placed (compatibilty with the maximum of computer)
Here's my code
private void SaveClose_Click(object sender, RoutedEventArgs e)
{
if (Windowed.IsChecked == true)
windowed = true;
else
windowed = false;
string textWriteWindowed;
if (windowed == true)
{
textWriteWindowed = "-screen-fullscreen 0" + Environment.NewLine;
}
else
{
textWriteWindowed = "-screen-fullscreen 1" + Environment.NewLine;
}
var selectedResolution = ResolutionBox.SelectedItem.ToString();
var split = selectedResolution.Split('x');
widthChoose = Int32.Parse(split[0]);
heightChoose = Int32.Parse(split[1]);
string textWriteWidth;
textWriteWidth = "-screen-width " + widthChoose + Environment.NewLine;
string textWriteHeight;
textWriteHeight = "-screen-height " + heightChoose + Environment.NewLine;
File.WriteAllText(#"\Resources\arguments.txt", textWriteWindowed);
File.AppendAllText(#"\Resources\arguments.txt", textWriteWidth);
File.AppendAllText(#"\Resources\arguments.txt", textWriteHeight);
this.Close();
}
The first argument of File.WriteAllText takes a path as input. Whatever you have mentioned is not the absolute path but it is just the relative path of the file. WriteAllText creates the file but doesn't create the directory by itself. So something like:
File.WriteAllText(#"\arguments.txt", textWriteWindowed);
shall work (and create the file in the respective drive), but
File.WriteAllText(#"\Resources\arguments.txt", textWriteWindowed);
shall not work. Hence, if you want to create a file in the path where the application resides, you can do something like:
string folder=Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
File.WriteAllText(#"\arguments2.txt", "ABC");
If you want to create a directory, then you could do something like:
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
file.Directory.Create();// If the directory already exists, this method does nothing.
System.IO.File.WriteAllText(file.FullName, textWriteWindowed);
Hope this answers your query.
you have to check whether the folder is exist before save the file,
if folder not exist create it using
Directory.CreateDirectory(...)
Directory.Exists(..)
you can use to check folder existence
IF you wanted to get the local path of the file you are executing use this:
var fInfo = new FileInfo(System.Reflection.Assembly.GetCallingAssembly().Location);
From there, you would do the following:
var parentDir = new DirectoryInfo(fInfo.DirectoryName);
var subDir = new DirectoryInfo(parentDir.FullName + "Resource");
if(!subDir.Exists)
subDir.Create();
This would ensure that you always have a folder in the directory of your executable. But just so you know, this is absolutely horrible code and should never ever be implemented in a production like environment. What if some knucklehead sysAdmin decides to place your program/folder in an area that the current user does not have access/writes too? The best place to write to is %APPDATA%, this will ensure the user always has read/write permissions to what you are trying to accomplish.
I don't know how but doing that worked for me :
File.WriteAllText(#"./arguments.txt", textWriteWindowed);
File.AppendAllText(#"./arguments.txt", textWriteWidth);
File.AppendAllText(#"./arguments.txt", textWriteHeight);

How to open a packaged file with WinRT

I am trying to figure out how to port some .Net code that parsed an xml file to WinRT. So far, with the help of The given System.Uri cannot be converted into a Windows.Foundation.Uri, I have the below code. Still, I get stuck immediately after I create the Uri:
static readonly Uri ResourcesBase = new Uri(#"ms-resource://MyAssembly/");
public override async void Load()
{
Uri uri = new Uri(ResourcesBase, filePath); // filePath = "Data//world.xml";
XmlLoadSettings settings = new XmlLoadSettings() { ValidateOnParse = false };
XmlDocument xmlDoc = await XmlDocument.LoadFromUriAsync(uri, settings);
foreach (IXmlNode xmlNode in xmlDoc.ChildNodes)
{
ProcessNode(xmlNode);
}
}
I get an unhandled exception when I try to call XmlDocument.LoadFromUriAsyn(uri):
ArgumentException was unhandled by the user code - Value does not fall within the expected range.
Anyone else feel like everything is 10 times harder with WinRT?
EDIT:
I have tried all the following strings, and get the exact same error:
Uri uri = new Uri("ms-resource://MyAssembly//" + filePath);
Uri uri = new Uri("ms-resource://MyAssembly/" + filePath);
Uri uri = new Uri("d:\\projects\\crystal\\" + filePath); // A valid absolute path
Project Set Up:
Project
Properties
References
Assets
Data
world.xml
Source code...
In Code:
filePath = "Data\\world.xml";
I have also tried putting the xml file under assset\data, and just assets. Nothing seems to make a difference.
Another thing, I have the Build Action of the xml set to "Content". Is that correct? The only other thing I could imagine that it would be is "Embedded Resource" but I doubt it.
Full Exception details:
System.ArgumentException was unhandled by user code
HResult=-2147024809
Message=Value does not fall within the expected range.
Source=Windows.Data.Xml.Dom
StackTrace:
at Windows.Data.Xml.Dom.XmlDocument.LoadFromUriAsync(Uri uri, XmlLoadSettings loadSettings)
at Crystal.IO.File.XmlFileSerializer.d__1.MoveNext() in d:\Projects\Crystal\library\IO\File\XmlFileSerializer.cs:line 32
InnerException:
Download the smallest example possible to repro the issue: test_xml.zip
I finally figured it out after I looked at Windows Runtime Xml data API sample.
public override async Load()
{
var file = await GetPackagedFile("assets", "world.xml");
LoadXml(file);
}
private async void LoadXml(StorageFile file)
{
XmlLoadSettings settings = new XmlLoadSettings() { ValidateOnParse = false };
XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(file, settings);
foreach (IXmlNode xmlNode in xmlDoc.ChildNodes)
{
//ProcessNode(xmlNode);
}
}
private async Task<StorageFile> GetPackagedFile(string folderName, string fileName)
{
StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
if (folderName != null)
{
StorageFolder subFolder = await installFolder.GetFolderAsync(folderName);
return await subFolder.GetFileAsync(fileName);
}
else
{
return await installFolder.GetFileAsync(fileName);
}
}
}

Categories

Resources