Detect Folders or Files on Gridview on UWP - c#

I have files and folders in the "koleksibuku" folder. I want to show folders and files into the gridview. How do I detect whether in the "koleksibuku" folder there are folders or files?
Note:
When a folder using the image:"ms-appx:///images/folders_png8761.png"
When a file using data binding
Folder created by the user with the folder name in accordance with the wishes of the user

You can get a list of subfolders by calling GetFoldersAsync() on a StorageFolder.
Here's how to use it to get a list of folders from the music folder (requires "Music Library" capability).
var downloadsFolder = Windows.Storage.KnownFolders.MusicLibrary;
foreach (var folder in await downloadsFolder.GetFoldersAsync())
{
Debug.WriteLine($"Folder: {folder.DisplayName}");
}
Assuming you've created your "koleksibuku" folder in your apps local folder you would get it with something like this.
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var myFolder = await localFolder.GetFolderAsync("koleksibuku");
myFolder.GetFoldersAsync();

Related

Copy G Suite drive files and folders from one domain to another domain

I'm trying to query list of all files and folders from drive of G Suite user and copy all this files to another domain user.
Anyhow i am able to get list of all files but i am not able copy those files to another domain.
I have gone through several reference on sharing drive files but couldn't understand of granting authority so that destination domain can copy files.
I will be happy if anyone can help me to solve this problem.
public void Execute(){
string strFileId = "";
//get list of all files from source domain(eg. user#a.jp)
IList<Google.Apis.Drive.v3.Data.File> fileList = service.getDriveFiles();
Google.Apis.Drive.v3.Data.File title = new Google.Apis.Drive.v3.Data.File();
//loop files list
foreach (var fileItem in fileList)
{
strFileId = fileItem.Id;
title.Name = fileItem.Name;
//copy files to destination domain(eg. user#b.jp) with file ID
service.Files.Copy(title, strFileId).Execute();
}
}
private IList<Google.Apis.Drive.v3.Data.File> getDriveFiles()
{
// Define parameters of request.
Google.Apis.Drive.v3.FilesResource.ListRequest FileListRequest = source_drive_service.Files.List();
// get all files
FileListRequest.Fields = "nextPageToken, files(*)";
IList<Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files;
return files;
}
There are many ways to go about this, but one simple way is to:
With the Drive API get a list of all your files (which include folders in Drive) by using the Files.list().
For every file, you can transfer ownership to a new owner (the admin of Domain B for example).
Note: When a file is transferred, the previous owner's role is downgraded to writer.

UWP c# How to get a StorageFolder object of the app's download folder

I am creating files and folders using DownloadsFolder methods.
I'd like to get the parent folder as a StorageFolder instance so I can list and manipulate all the items in the app's downloads folder.
I've tried GetParentAsync() from a known StorageFile, but the return is null.
StorageFile sf = await DownloadsFolder.CreateFileAsync("testMarker");
StorageFolder dlFolder = await sf.GetParentAsync();
Is there any method to access this folder programmatically?
It seems your app doesn't has permission to read the Downloads directory. We can use GetParentAsync to get a parent the app has permissions for, but not to get folders the app doesn't have permissions in.
If we add the Music Library, Pictures Library and Videos Library to the Capabilities of the appxmanifest that we can use the GetParentAsync method to get the parent folder as a StorageFolder in these folder.
If you create a file or folder in the Downloads folder, we recommend that you add that item to your app's FutureAccessList so that your app can readily access that item in the future.
For more info, please refer the Locations Windows Store apps can access.
So if you want to get the other folders and files in DownloadsFolder, you should be able to Open files and folders with a picker.
It looks like this isn't possible - You can try to get the storage folder by using the Path with System.IO directly like so
var downloadPath = System.IO.Path.GetDirectoryName(storageFile.Path);
var downloadsFolder = await StorageFolder.GetFolderFromPathAsync(downloadPath);
But GetFolderFromPathAsync seems to throw an exception, I think windows just won't give you a reference to this folder.
A solution is to use a folder picker ONCE to pick the downloads folder (as part of the inital setup of the app). After that the app has full access to the downloads folder.
This code needs to be called once:
var folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Downloads;
folderPicker.FileTypeFilter.Add("*");
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("DownloadFolderToken", folder);
var messageDialog = new MessageDialog("Download folder: " + folder.Name);
await messageDialog.ShowAsync();
}
else
{
var messageDialog = new MessageDialog("Operation cancelled");
await messageDialog.ShowAsync();
}
This code can then be used to directly access the downloads folder:
var downloadsFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("DownloadFolderToken");
IReadOnlyList <StorageFile> fileList = await downloadsFolder.GetFilesAsync();
StringBuilder outputText = new StringBuilder();
outputText.AppendLine("Files:");
foreach (StorageFile file in fileList)
{
outputText.Append(file.Name + "\n");
await file.DeleteAsync();
}

Getting all files in UWP app folder

For UWP, it is easy to get all files in the app local folder as:
IReadOnlyList<StorageFile> files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
You can now iterate on the files list and even get further info on individual files.
I would like a similar all-file-getter for an app folder, for instance, consider the /Assets folder where app *.png files are stored.
Single file with a known name is no problem; I can refer to it quite easily as:
StorageFile.GetFileFromApplicationUriAsync(new Uri(#"ms-appx:///Assets/StoreLogo.png"))
My question is, therefore, is there a similar thing for getting all files in an app folder, such as /Assets folder? Logically, it should be something like StorageFile.GetFilesFromApplicationFolderUriAsync(new Uri(#"ms-appx:///Assets")) but unaware if an equivalent of the LocalFolder shown above exists.
You can access you installation folder by using Package.InstalledLocation. Therefore your code can look like this:
StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder assets = await appInstalledFolder.GetFolderAsync("Assets");
var files = await assets.GetFilesAsync();
var storageFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("Assets");
var files = await storageFolder.GetFilesAsync();
https://learn.microsoft.com/en-us/uwp/api/windows.storage.applicationdata

Get all files from Assets Windows Store Application

I am developing a Windows Store application and have about 20 images in the Assets/Backgrounds folder.
How can I get those files from C#?
I only need the file path, for example I need to create a List of strings object with file path. For example the list can be in the format of
List<string> files = new List<string> {"Assets/Backgrounds/file1.jpg",
"Assets/Backgrounds/file2.jpg", ...};
Thanks
First you need To know the Path of your Contents
string ImagesFile = #"Assets/Backgrounds/file1.jpg";
Then you Can Access The file like This:
StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await InstallationFolder.GetFileAsync(ImagesFile);
This contains all the files in your application package
ResourceManager.Current.MainResourceMap
Use Linq to filter those you need

How to know a new folder is created in a particular folder

I am developing a desktop application in C#.
I have programmatically created a folder(say ABC) inside the Main folder in C drive in Windows.
I want to know whether an user has created any new folder(by simply right clicking and creating new folder) inside ABC.
If the user has created a new folder then I need to get the details of that folder like folder name and privacy too.
Thanks in advance!
You can get the subdirectories of a folder (in your example, the folder "ABC") as an array of strings by calling the method GetDirectories:
string[] subdirs = Directory.GetDirectories(#"C:\ABC");
Then, if you'd like, you can iterate through all of them:
foreach (string dir in subdirs)
//dir is a path to a subdirectory
Don't forget the using statement!
using System.IO;
You can use DirectoryInfo to get the list of subfolder
DirectoryInfo dirInfo = new DirectoryInfo(#"c:\ABC");
DirectoryInfo[] subFolders = dirInfo.GetDirectories();
I'm not sure what you mean by privacy...

Categories