How can I access the current application folder in UWP? - c#

I need to access a subfolder in the application folder whose path is:
C:\Users\myname\Documents\Visual Studio 2015\Projects\App2\App2\Templates
and retrieves all the data inside. I need to read the xml files in a subfolder of it, parse them, and store them in a dictionary. I have the parsers ready but just don't know how I can access the folder?
I tried
StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder templateFolder = await localFolder.GetFolderAsync("Templates");
But it didn't work. An error occurred indicating that folder "Templates" could not be found. However, when I tried "Assets" instead, which had been in the folder ever since the project was created, it worked. How can I access it?

If Template folder is in your project, then you can get it via:
var TempFile = "ms-appx:///Templates/Yourfile.jpg";

Related

Can we create a folder in project and add images in it by user instead of bin?

I am creating a desktop application in which user can add persons and their images. Currently, the images folder is in bin folder but when I publish the application and run on client pc then images folder gone missing.
OR
Can I add images in Project->Properties->Resources programmatically?
Currently when I tried to user this Images Folder in PMS Project then images added in the folder present in the bin folder. How I can add in this folder?
Currently, I tried to access this is
string path = System.IO.Directory.GetCurrentDirectory();
Bitmap imgImage = new Bitmap(pictureBox1.Image);
System.IO.File.Copy(ImageName, Path.Combine(appPath, CNIC + Path.GetFileName(ImageName)), true);
I also want to keep images folder in the installation folder.
But it goes in the bin folder. How I can achieve this?
If you plan to have a private folder where your app store some data then you should really use the standard specs. You should create a folder inside the Environment.ApplicationData defined in the SpecialFolder enum
You could have something like this in the startup code of your application
string commonFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string appFolder = Path.Combine(commonFolder, "PMS");
string imgFolder = Path.Combine(appFolder, "PoliceImages");
Directory.CreateDirectory(imgFolder);
Now those strings variables should be stored inside some kind of global configuration static class where you can retrieve them in any part of your application
If you want the folder be created automatically, you can add the basic images that would go into that folder and mark their Build Action and Copy to Output Directory properties appropriately in their properties window. This will make sure the folder gets created and also images will be copied in published copy.
Alternatively, If there are no images from development time, you can create the folder in client's installed location, using System.IO classes. Like -
System.IO.Directory.CreateDirectory("ss")
However, I would like to mention that its not a good practice to allow users to add images into a sub folder in bin unless when it is going to be a part of application itself after adding. If it is a content or data, try to keep it out of the folder where application binaries are installed. Probably,
Use an appropriate database storage.
C:\Users\[USERNAME]\AppData\Roaming
var directory = Environment.GetFolderPath(Environment.SpecialFolder.AppData);
OR
var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Or simply D:\Temp\[ApplicationName]

How to download file where application is stored?

So the following script is supposed to download a file to the location where the application is located. Now I Only can get GetTempPath to work. But that downloads file to the temporary folder. But I need it to download to the folder where the application is located.
var output = System.IO.Path.Combine(System.IO.TempPath(), fileName);
And when I try different things instead of GetTempPath it states method not found.
var folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
This will get the folder of the code of the currently executing assembly, basically the assembly which is executing your code.

Temporary folder access denied

I started with UWP platform apps and I want create new folder in Temp folder:
StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;
temporaryFolder = await temporaryFolder.CreateFolderAsync
(Path.GetFileNameWithoutExtension(Path.Combine(temporaryFolder.Path, fileName)),
CreationCollisionOption.ReplaceExisting);
Everything looks OK, but when I want to decompress a ZIP file, which is in Temporary folder into folder which one I created then I give exception:
System.UnauthorizedAccessException: Access to the path 'C:\Users\Admin\AppData\Local\Packages\cebff192-8162-4800-9f9c-b3ce1ca8849f_5gyrq6psz227t\TempState\1' is denied.
My question is simple: How I can create a new folder in Temp to which I can write?
You should be able to access the file in the temporary Folder, but it depends on how you access it. When you access the file, please avoid using the file path. See this blog: https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/
To open and read a file in the temporary app data store, use the file APIs, such as Windows.Storage.StorageFolder.GetFileAsync. You can get more details about the temporary folder from Temporary app data.

Cannot create subfolder under RoamingFolder in Windows Store App

I'm writing a Windows 8.1 store application and am trying to create a subfolder in the ApplicationData.Current.RoamingFolder directory. There is no data currently stored there. However, the following line throws a System.UnauthorizedAccessException saying "Access is Denied".
var folder = await ApplicationData.Current.RoamingFolder.CreateFolderAsync("subfolder", CreationCollisionOption.OpenIfExists);
Strangely enough, if I use LocalFolder instead of RoamingFolder, the operation succeeds.
var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("subfolder", CreationCollisionOption.OpenIfExists);
Why can't I create a subfolder in the roaming folder? I can manually create the folder via Windows Explorer.
The documentation (link) says that there are limitations on what can be placed in the folder, specifically:
"The sync engine has restrictions on the file name conventions that you must follow to ensure the items in the roaming folder can roam. Be sure that your file and folder names do not contain leading whitespace. The sync engine may limit the total size of settings and files that can roam."
However, I don't see anything that would explain the above exception. What am I missing and what other limitations am I missing about the roaming folder?
Interestingly, if I create a brand-new application both lines of code function perfectly. I don't know why that is.
Edit: As mentioned by Nate, the CreateFolderAsync documentation says "If you try to create a subfolder in a virtual folder like a library or a file group, this method may fail." Does anyone know if that applies here? If it does, what limitations does it introduce? The only information I've found on this issue is this question, but it doesn't seem to firmly resolve the issue.

How to access home directory and auto create directory on install

I want to create a directory with some config file on the installation process and user home directory. My questions are:
-> how to create this directory while installing
-> how to access home directory from c# code
You have access to some special folders for your application. You can save a file to the LocalFolder like this:
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("config.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, "Text to write to file");
and then retrieve it:
file = await ApplicationData.Current.LocalFolder.GetFileAsync("config.txt");
There's also a RemoteFolder for files you want available to the user if they use your application on another device.
This folder can be found by going to something like "C:\Users\USERNAME\AppData\Local\Packages" and then finding the folder for your app.

Categories

Resources