How to get file shared assets in Win 8.1 universal app? - c#

I have universal app and shared assets in it. These are pictures I use both in the mobile and in the desktop app. But, obviously installation folder is not accessible in code-behind. Is there way how to achieve that or I have to add pictures to both platform-wise projects?

But, obviously installation folder is not accessible in code-behind.
No, it is accessible.
Is there way how to achieve that or I have to add pictures to both platform-wise projects?
We can use Package.InstalledLocation to access the folders or files in the installed location for example like this:
var assetsfolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
And we don't need to add pictures to both platform projects. You can create a folder for example named "ShareAssets" in the [ProjectName].Shared like this:
And I put a image under this folder, so will both the windows 8.1 project and windows phone 8.1 project be able to access this folder:
<Image Source="ShareAssets/miao4.jpg" />
As the method I provided, we can also access this folder in the code behind from both platform:
var shareassetsfolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("ShareAssets");

Related

Access to an embeded resource file doesn't work on Xamarin but on Windows App

I added an excel File (template.xlsx) to the project resources as an embedded resource. In my code I want to save that file to disk.
On Windows it works well with:
File.WriteAllBytes("test.xlsx", Properties.Resources.template);
But in my Xamarin.Forms project exactly the same line does not work because "Properties" is unknown in the current context.
I searched half a day in the net but all hints I got don't work either or confuse me even more. It seems I am missing an essential piece of base knowledge here. Could somebody tell me if I can easily access an embeded File in Xamarin similar like I do on Windows?
The difference I can see in the two test projects is:
In the project explorer of the windows project I can see Properties->Resources.resx. However, the template file is not located there but in Resources->template.xlsx
In the project explorer of the Xamarin project there is no Resources.resx in the Properties folder.
My file is again located under Resources->template.xlsx
You have to read that file in your code and then use it to write that file to file storage (application sandbox) of the platform. You can save files to that location, although you may not easily be able to access that file outside the scope of the app (say from another application).

Create app with data already present on install

I'm currently programming an app that need to access to some Excel files.
So what I need is to create a folder in the app files with these Excel files in it but I want that folder to be created at the app install, so they would be accessible for every device that install the app.
The files also need to be modifiable in the future by the user.
The problem is that I don't know how to do it right. Should I just create a new folder in the Solution Explorer and put the Excel files in it ? Should I create the folder programmatically and force the user to put them manually in that folder ?
I don't really know how to do it so that the application will not be too complicated to be modified by the user.
EDIT : Also, if I put the files in the Assets, will the user be able to change them later ?
So what I need is to create a folder in the app files with these Excel files in it but I want that folder to be created at the app install, so they would be accessible for every device that install the app. The files also need to be modifiable in the future by the user.
For your requirement, you could use ApplicationData.LocalFolder to store Excel files, LocalFolder has full access permission. LocalFolder exists in the app's sandbox path and will be created after the app is installed.
Also, if I put the files in the Assets, will the user be able to change them later ?
Assets folder exists in Windows.ApplicationModel.Package.Current.InstalledLocation and it is read only that often use to store some static resource. You can't modify the file at run time.
For more details about file access permissions please refer this document.
Forget the installer. It is deprecated and the new apps on Windows are installed over the store.
If you want to copy some files (Excel templates), you have to put them in your resources. At startup you can check if there is a folder in your App-Data folder with your files, if you don't find them, you can copy it from your resources. So even if the file is deleted, your app can copy them in the next start.
If you use Windows Forms with .NET, you can check this page:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.localuserappdatapath?view=netframework-4.8
If you are writing an UWP-app, check this page:
https://learn.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data

How to use UWP FolderPicker in a WPF c# desktop app

I have a WPF desktop app and I want to use the UWP FolderPicker API to pick a directory. My app uses the UWP packaging project so it is built and ran as an appx. I've added in the Windows and WindowsBase references and my project builds and runs. However I get a runtime error when trying to use the folder picker. My code is as follows:
private async void OnGetDirectory(object parameter)
{
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
// Application now has read/write access to all contents in the picked folder
// (including other sub-folder contents)
Windows.Storage.AccessCache.StorageApplicationPermissions.
FutureAccessList.AddOrReplace("PickedFolderToken", folder);
}
else
{
}
}
The error I get is on the line System.Exception: await folderPicker.PickSingleFolderAsync(); and the error 'Invalid window handle. (Exception from HRESULT: 0x80070578)'
What am I missing or is it even possible to use the FolderPicker from a WPF app?
Instead of UWP FolderPicker you can just use a normal Windows Forms folder dialog - FolderBrowserDialog in System.Windows.Forms namespace (see this question for an example).
If you want to work with StorageFolder, you can call StorageFolder.GetFolderFromPathAsync method or you can use the classic folder APIs like System.IO.Directory or System.IO.DirectoryInfo.
According to UWP APIs available to a packaged desktop app (Desktop Bridge) article, some feature areas are not yet fully tested or currently functioning as intended. The "File and folder pickers" feature that you used just in the list. The detail description for this feature is as follows:
Packaged apps have full file system access and do not need UWP pickers.
So that FolderPicker may not completed supported in your UWP packing project. Please try to use the file relative APIs that supported in WPF itself.
More details please reference this document.

Get file paths for files in Centennial app

When converting an app to a Windows Store app, we add the files as mentioned here. But now - how do we access them? What's their path?
They're not in the special folder created for the package.
You can create a folder structure in your project where you can include the files and then since you are already crossing the Centennial Bridge, you can start using the Windows.Storage.StorageFile WinRT API to interact with files.
For example, you can use: StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///folderUnderYourAppRoot/image.bmp")); to get the file which is under the "folderUnderYourAppRoot". This is the generic guidance: https://msdn.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files, so you can review it further...

How to access a folder in c# in application files which is created after publishing application using relative path?

I am creating a application where I need to access images from a image folder in application files which I will add later after publishing the application. But the thing is when I access my application data path it is .../AppData/Temp...
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
So what is the way to access the data in Application Files in .exe directory.
My base directory is in Temp. But I want to access the one from where I executed the .exe
See more How can I get the application's path in a .NET console application?
Sulotion:
Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location,#"/Images/logo.png");
I hope this helps you.

Categories

Resources