What's the default file path in WinRT/WP 8.1? - c#

Hello fellow programmers.
I recently made a mistake while using the SQLite-NET package for Windows Phone 8.1. When opening a new SQLiteConnection, I would give as parameter to its constructor the string "data.db", without being actually aware of what was going on under the hood. Later on, I noticed that the file wouldn't be deleted, even if I uninstalled the application (since I would have in my app the same entries that I had before uninstalling it); I assume that I should be using the local folder for application data instead.
However, here is the real question: what the hell is the folder where the data.db file was created? I tried to figure it out with the following piece of code:
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///data.db"));
System.Diagnostics.Debug.WriteLine(file.Path);
and what I got as output of the WriteLine method was the path
C:\Data\SharedData\PhoneTools\AppxLayouts\f7529f24-ba24-4fdb-8353-cff9214180a0VS.Debug_ARM.Valbrand\data.db,
which only got me even more confused. I couldn't find any satisfactory info on this, and I would REALLY like to further my understanding of what happened here.
Thanks in advance!

The path you are looking at is the install directory of your app. You have a Silverlight 8.1 app, so the path is slightly different than a Silverlight 8.0 app, but the articles here and here will help you get a rough understanding.
The recommended usage of this directory is to take the resources that you want backed up out of it on first run and move them to the app data container, so they can be backed up by the system.

C:\Data\SharedData\PhoneTools\AppxLayouts\f7529f24-ba24-4fdb-8353-cff9214180a0VS.Debug_ARM.Valbrand\data.db
This is the path on your phone/emulator.
And This is not the location where you can find in your PC.

Related

WinUI 3 calling ApplicationData.Current() throws System.TypeInitializationException

I am trying to convert a windows desktop application from WinUI 2 to WinUI 3. However, I am having a problem with the Windows.Storage.ApplicationData class. When I call ApplicationData.Current (so I can access a file in the app's local data store or to get a local setting) a System.TypeInitializationException exception is thrown.
The following screenshot shows the problem:
System.TypeInitializationException
(This is a default application that I created in Visual Studio 2022 using the project template "Blank App, Packaged (WinUI 3 in Desktop)", the only change I made was to add the line to get the current ApplicationData object in the App class's OnLaunched() method:
var appData = Windows.Storage.ApplicationData.Current;)
Any idea what I am doing wrong here? I think maybe there is a trust or cababilities issue, or I should use other methods to get the app's local data folder or settings for a WinUI 3 desktop application.
I encountered the same problem recently. No matter where I made the call in my WinUI 3 application, whenever I invoked a call to the ApplicationData API, I would always encounter the same exact exception (System.TypeInitializationException with an inner COMException). I failed to find an answer in the API documentation, particularly where one would hope to find such an answer. (If it's there, it's buried or perhaps easily overlooked by someone who is not as familiar with .NET programming, such as myself.) The following line of code always threw an exception for me:
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
The solution that worked for me was something I learned from a response to an issue on the WindowsAppSDK GitHub repository. To quote the information I found most relevant to the issue I was having:
A client that is not running in an App Container must open ApplicationData using this API: https://learn.microsoft.com/en-us/uwp/api/Windows.Management.Core.ApplicationDataManager?view=winrt-22000, while a client that is running in an App Container must open ApplicationData using this API instead: https://learn.microsoft.com/en-us/uwp/api/windows.storage.applicationdata?view=winrt-22000
Basically, I learned that I needed to open ApplicationData using the ApplicationDataManager class. As noted from the GitHub link, the solution I found was to replace the offending line of code with the following:
var localSettings = ApplicationDataManager.CreateForPackageFamily(Package.Current.Id.FamilyName).LocalSettings;
From there I can save and load app settings as usual, which is suitably explained here:
localSettings.Values["test setting"] = "a device specific setting";
String localValue = localSettings.Values["test setting"] as string;
Hopefully this helps anyone who finds themselves facing the same issue.
Scratch that, the problem only happens if in the debugger I put a breakpoint on the "ApplicationData.Current" line. Very weird. I think I am having the same problem as here:
Getting values of ApplicationData.Current.LocalSettings in static context

Xamarin Forms PCLStorage files are lost after app stops debugging

Using PCLStorage in Xamarin Forms I can create a local folder and store images downloaded from Azure.
I can then successfully test to see of the images exists using
ExistenceCheckResult fileExist = await MyFolder.CheckExistsAsync(fileName);
However, once the app has been stopped (by stopping debugging in Visual Studio) and restarted, if I run the test code above, it fails and tells me the images do not exist.
Do files then only exist for the time the app is running?
This thread looks like it could possibly cover what you are experiencing:
https://forums.xamarin.com/discussion/87498/i-am-using-pclstorage-where-are-the-files-going-to
Reading up on this, the file persistence side of things looks like debugging should not affect it, as once it writes the file - then it's in the specified location.
Also - looking at PCLStorage and the last time it was modified, if this is a new addition to your project, I can't say I would recommend it, as it looks like it is no longer being worked on, and hasn't been for some time.
I would go with an implementation from System.IO as it is now supported within .net standard 2.0 - and there appear to be lots of great examples for you to follow.
If this doesn't help, then post some more of your source code so that it can be looked into in further detail.

Universal Windows Platform (UWP) C# app - How to run stand alone python inside your application

I am trying to find a way to call python from my UWP app. So far I have a .exe file that I have compiled from python using pyinstaller (www.pyinstaller.org/). This basically allows me to package up my python script as a standalone binary (ie: you don't need python to run it). This all works well and I can call my wrapped up python .exe via cmd.exe no problem:
$ process.exe -p "path\to\file"
$ Processing file: "path\to\file"...
$ Done.
So now I just need to call it from my UWP app - so I have added it to my application like so:
C# Project
Assets/process.exe
Frustratingly, I've not had much luck googling for answers to my problem - my attempted solutions so far have included:
Calling the "Assets/process.exe" directly from my app
Looked at "Launch an app and get results". I think this seams to be for external applications however... I certainly didn't get it going anyway.
Opening the cmd.exe (somehow) and calling my process.exe from there.
I'm not even sure if I'm trying to do this the correct way or not. Or if I have just not understood some of my findings. Or (fingers crossed) there is a simple solution to this I just don't know about and have somehow missed as I'm very new to UWP development and C#.
So any solutions/pointers here would be greatly appreciated thanks!!
UWP apps are 'sandboxed'; i.e. they have many security restrictions placed upon them to isolate them from the rest of Windows (like not being able to read/write to the Registry and not being able to directly access random files from the file system).
So there is no way to run an .exe (or any other executable) from your UWP app. If you have access to a StorageFile (say music, video or any other file format) then you can launch the file in the default program associated with that file type.

Windows Form Application : Publish issue

I'm very new to Visual Studio 2010. I decided to create a simple WFA with C#.
Everything work fine with Images and Audio playback. My intention is to create a standalone application and send it to my friend as a gift. Problem I facing now is that when I tried to publish the application, the Images / Audio is still using the same location in my PC. So it won't play nor display any images.
Any comment on this or guide for me? I did try search for solution but doesn't seems to have any luck.
This is the code that I used to play Audio :
SoundPlayer ply = new SoundPlayer(#"C:\Users\Liam619\Documents\Visual Studio 2010\Projects\BirthdayApp\BirthdayApp\Resources\BirthdaySong.wav");
If I remove or change the path, the Application will hit error for locating the Audio file.
There may be several solutions to your problem.
a) embed the sound into a resource. As a beginner, resources may be a bit tricky to get it right the first time. But I want to encourage you reading something about it. You'll need resources when you want to translate your first program.
b) create an installer which copies the sound file to the installation directory. Try InnoSetup. If you're a programmer, sooner or later, you'll need to create a Setup anyway. Always worth knowing how to do that.
In that case, you still need the path to the sound file, but if you install your EXE into the same path as the sound file, see getting the application's executable directory.
everything in the database whether images or audio refers to your own server database.you have to send the database too with the app and the correct version .NET framework needs to be installed on the target PC.

Windows Phone 8 UnauthorizedAccessException when opening file

I am having a very strange error when trying to access a ".stl" file in the "Stl" folder of my application when the app is downloaded from the Windows Phone Marketplace. It does not exhibit this behavior when it is launched from Visual Studio 2012 Express in either debug or release mode. Once I try loading a file it gives me an "System.UnauthorizedAccessException Access to path 'C:\Data\Programs\APPUID\Install\Stl\test.stl' is denied" exception while accessing a ".gcode" file in an almost exactly the same fashion from the "GCode" causes no error what so ever.
I have no idea what could be causing this or how to debug this because I don't know if I can somehow attach the debugger to an instance of the store downloaded app.
I also have no idea what could possibly be different between deploying the exact same app from the store and from Visual Studio. The Visual Studio installed app also does not give any troubles if the app is launched from the phone without the debugger being attached.
Any ideas?
PS. The file is being opened by a filestream which is then used by a binarreader. I am not sure if the source code is really needed and for which part but the important line is just:
FileStream fileStream = new FileStream(filePath, FileMode.Open);
Where "filePath" in this case is "Stl\test.stl".
Without seeing your actual code it is hard to say what the problem is. So instead of attempting to read your mind I will offer an alternative solution.
I have worked extensively with Isolated Storage on the Windows Phone and I have learned this.
There is no library, no API, no SDK, no Web service, in the world, that has worse error reporting than the Isolated Storage on windows phone.
EVERYTHING is Invalid Access or Unauthorized Access with ZERO further information.
To remedy this I created a DLL that serializes objects to the Isolated Storage for you.
All you have to do is put [DataContractAttribute] above your class name and [DataMemeber] above any variable you want saved. Then you just pass your object and unique name into the savefile method. That's it! Instant save
You can find my free DLL EZ_Iso.dll for download here. With example code and instructions
The code is open source so if you wish you may also decompile the dll and see how it all works.
Feel free to reach out to me here or on twitter if you have any questions or enhancements.
Ok I figured it out. The "install" directory is actually restricted access but for some reason the Visual Studio signing process leaves the app with enough permissions to access this folder. The correct procedure of determining a relative directory is not to use "Directory.GetCurrentDirectory()" but rather to use "ApplicationData.Current.LocalFolder". Hope this helps!

Categories