I am creating an Windows Explorer kind of app using metro UI. I want to show content of a certain folder in my User folder.
I wanted to know if it is possible to access the folders and files inside users folder and how do I do it?
Edit :
I tried using StorageFolder. For documents folder, it gives me an exception.
The KnownFolders.DocumentsLibrary folder has restrictions on its use. You have to manually declare the capability in your manifest, declare specific file types you want to access, and then to publish the app in the Store you have to be using a company account (not an individual account), and have to submit written justification for your programmatic use of the folder.
See http://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx as well as the note for section 6.6 of http://msdn.microsoft.com/en-us/library/windows/apps/hh921583.aspx.
Note that this is for programmatic access without user consent. If you use the file picker, the user can of course point to the documents folder thereby giving you permission to use it. But without that, you need to use your app data folders where you do have programmatic access.
The underlying reasoning here is that files that your app it generating for its own use, that don't have direct meaning to the user, should go in app data to avoid polluting a folder like Documents with stuff that the user doesn't know what to do with. For "user data" files--which the user does understand, you should give them the option to choose where those files go, hence the use of the file picker.
A few file types like music, pictures, and video have direct library access via manifest capabilities, but in that case the user generally understands that they're working with that kind of data.
You can access the contents of users folder like this:
string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).ToString();
Use SpecialFolder to get the user folder and Directory.Enumerate() for getting files in the folder.
Related
Declaring an App Capability in manifest file, we can have our UWP app access certain folders such as Pictures, Videos, Music etc.
Moreover, the broadFileSystemAccess capability allows apps to get the same access to the file system as the user who is currently running the app without any additional file-picker style prompts during runtime.
But how about even if the user has access to his/her entire system but wants a UWP app to access only a specific folder needed for the app to have read access. Can we implement such a functionality in a UWP app without using file-picker?
Why do I need that?: Our app reads files from a folder that is too big to be included inside the app and has tons of files of various types inside it. Only read-access is needed.
But how about even if the user has access to his/her entire system but wants a UWP app to access only a specific folder needed for the app to have read access.
You could not create that folder only has read access capability for current user. And the folder's attribute is control on the system level. You could only create the folder and set attribute as read only for other user. And that could not be achieved(System.IO.File.SetAttributes) in uwp platform.
Yes, the folder is provided for download. But, I'm not sure if a UWP app can have an entire folder (specially a large data folder) downloaded inside it have it's code read it.
For the requirement, you could store the download in your app's LocalFolder and there are no general size restriction on local data stored. And it only could be accessed for current app.
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Certainly, you could also store the large data in the Download folder. For more info please refer this document.
I'll try to provide you the logic of why your request is conflicting and thus not implemented at the system level.
Sandboxing means that the app has the access only to its own folders. From the system aspect the only exception to that rule could be that some folders are not considered security critical (like Pictures, Music, Downloads) and you may declare access to those folders. The second exception is if the user gives you access to the folder. In both of those cases sandboxing is not broken because the expansion of the sandbox is not arbitrary. As soon as you arbitrarily want to expand the sandbox, there is no difference in safety between having access to whole the system and few folders that you randomly pick. I am not sure how do you see the difference in that - in both case you arbitrarily pick to access some folders just if you have the access to whole the disk you do that from the code while if there was some capability to do that through some declaration then you would do it through declaration. But effects are the same - your app has arbitrary access to the unsafe portions of the disk that depends only on your preferences.
Before I get into further details I wanted to point out that uwp can now know have system wide file access so this issue is not exactly related to that.
With that out of the way, the issue is rather simple to explain, uwp cannot query or access link (.lnk) or weblink files as well as possible other files that I dont know yet.
To be more specific the tools found both in the System.io and the Windows.Storage namespaces are incapable of listing .lnk and url files.
They simply never appear on Queries like the System.IO.Directory.GetFiles
StorageItemQueryResult.GetFiles()
While also direct access from path as allowed by functions like
StorageFile.GetFileFromPathAsync() or System.IO.File.Open()
will cause System.UnauthorizedAccessException: Access is denied.
I am posting knowing full well that this will probably never be answered, the only glimmer of hope is the fact the hidden build in uwp explorer can indeed list the affirmational files.
The .lnk file type has limitation to be accessed from UWP app since UWP app's sandboxed and user permission. You can not access this file type from path even using the broadFileSystemAccess Capability. But you can get the file by FileOpenPicker which lets the user choose and open files.
Is there a way to get a list of folders that my Application has permission to access, and how would I add a new folder to the list? For Example, Documents Folder, Picture Folder, etc.. etc.. And how to make this list of Folders persistent between application starts?
You can use KnownFolders static class to get common folders such as Documents, Pictures, etc. However, to be able to freely access them, you need to specify appropriate capabilities in your manifest (like 'Pictures Library', for example).
For any other arbitrary folder, you will need to explicitly ask user to pick it using FolderPicker. You can then save this folder for future access using FutureAccessList or MostRecentlyUsedList.
There is a good sample covering all those options in UWP samples repo.
Pretty much I must find a way to populate image boxes with the corresponding information to the images, if it's possible in a Windows 8/Metro/Store App. I have a database with all the physical paths of the images which are stored on the local computer e.g: "c:\BookImages\spud.jpg". Now when i pull the information from the database with information about the book, I also take the file path of image("c:\BookImages\spud.jpg"). All i need to now do is to fetch the image and display it in a image box. But i do not how to do this, i have been researching for a while now, and I'm coming to the understanding that i cannot select file's from other than the actual install directory of the windows app. Unless i use a FileOpenPicker, which will be a useless for the user to select the folder which contains the images every time they run the app. If anyone has a solution to my problem please let me know it will be much appreciated.
See If all the directories were given access for a win rt app then it would be a security risk
So some specific folder are available try storing user data in there . After that only File Picker can Help.
Heres the link
Folders for winrt
Its a Microsoft policy so Please design app accordingly. You might needd to set capabilities in the app manifest file for some folders. Please go through it :)
The short answer is to have the user pick the folder once and save the StorageFolder in the Windows.Storage.AccessCache.
The long answer is that all this boils down to having your data store in a place that is programmatically accessible.
One option, as Anobik pointed out, is to use either your appdata folders (where you have open access), or something like the Pictures Library, which you can access given a capability in the manifest. This way you don't need to ask the user.
If you use the file picker to let the user select a folder for the data store, he or she has granted permission to access that folder programmatically, including its contained files.
The important thing is that you have to preserve that permission across sessions, which is the purpose of the AccessCache.
Think of a StorageFolder object (and a StorageFile) as an abstraction for a pathname--you never really want to save pathname strings unless you have inherent programmatic access to that location. This isn't true for stuff from the file picker, so you have to use the AccessCache to essentially save the folder reference and its permissions.
Kraig Brockschmidt
Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press (also see second edition preview)
I'm developing an application that will be published via ClickOnce. I've been using the Application.ExecutablePath folder to store some licensing information. Now, I want the application to check for updates, but whenever it does the license file get lost and the user needs to enter the information again. Seems like ClickOnce is basically uninstalling and reinstalling the new version of the application. I could save the license file somewhere in the System folder, but that's an ugly solution. Another file with the same name could already exist in there, or I might get access denied exceptions or stuff like that. I'm not trying to hide the file. And I need the folder to be accessible without administrator privileges.
So what's the best and safest way I can store a file that will stay after I uninstall (or update) the application?
I think you should store it in the common application data place. You can get the folder path by the following code
Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData);
This folder is a built-in special folder. It's main purpose is to store shared application data for all the users using the same application. Usually, you would like to create a sub folder with your compaany name and product name under that folder so that it won't conflict with other application. If your don't want to share your license key to all users on the same machine, you can use System.Environment.SpecialFolder.ApplicationData instead.
I have seen many applications hide their license settings in the Registry - sometimes hiding under clever key names. I hate it as a user, but if you like to keep the data I think registry is the best.