Get all files inside a specific folder in a library with UWP - c#

I'm trying to get all the videos in a specific folder inside the Videos library using UWP, right now I can get all videos inside the Videos library, but I'd like to reduce my results to only those inside the specified folder. My code is this:
Windows.Storage.Search.QueryOptions queryOption = new QueryOptions(CommonFileQuery.OrderByTitle, new string[] {".mp4"});
queryOption.FolderDepth = FolderDepth.Deep;
var files = await KnownFolders.VideosLibrary.CreateFileQueryWithOptions(queryOption).GetFilesAsync();
StorageFile videoToPlay = (files[new Random().Next(0, files.Count)] as StorageFile);
var stream = await videoToPlay.OpenAsync(Windows.Storage.FileAccessMode.Read);
Player.SetSource(stream, videoToPlay.ContentType);
Debug.WriteLine(Player.Source);
How could I access a subfolder named "Videos to Play" and then get all the videos inside that folder? I tried accesing it by using a path like:
string localfolder = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
var array = localfolder.Split('\\');
var username = array[2];
string[] allVideos = System.IO.Directory.GetFiles("C:/Users/" + username + "/Videos/Videos to Play");
But I get access denied even though I already requested access to the Videos library (and the fact that the first example works shows that I actually have access to it).

try
{
var folder = await KnownFolders.VideosLibrary.GetFolderAsync("Videos to Play");
}
catch (FileNotFoundException exc)
{
// TODO: Handle the case when the folder wasn't found on the user's machine.
}
In the folder variable you'll have the reference to the desired folder. Then it's the very same stuff that you already do, but instead of KnownFolders.VideosLibrary folder use this one!

Related

Unable to move directory in azure using ShareDirectoryClient

Normally we can move the directory using
// source is: "C:\Songs\Elvis my Man"
// newLocation is: "C:\Songs\Elvis"
try
{
// Previous command was: Directory.Move(source, newLocation);
DirectoryInfo dir = new DirectoryInfo(source);
dir.MoveTo(newLocation);
}
catch (Exception e)
{
Console.WriteLine("Error: "+ e.Message);
}
Now, when using the azure:
string myconnectionString = ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString.ToString();
string myshareName = "Mysampleshare";
string mydirName = "Mysampledir";
// Get a reference to a share and then create it
ShareClient myshare = new ShareClient(myconnectionString, myshareName);
ShareDirectoryClient directory1 = myshare.GetDirectoryClient(mydirName);
string myshareName2 = "Mysampleshare2";
string mydirName2 = "Mysampledir2";
// Get a reference to a share and then create it
ShareClient myshare2 = new ShareClient(myconnectionString, myshareName2);
ShareDirectoryClient directory2 = myshare2.GetDirectoryClient(mydirName2);
Directory.Move(directory1.Path, directory2.Path);
I am unable to move the directory from one location to another using azure. I am getting exception.
DirectoryNotFoundException: The path specified by sourceDirName is invalid
Please suggest your advice.
ShareDirectoryClient essentially implements Azure File Service REST API thus you cannot use System.IO operations like Directory.Move with it.
There are two possible solutions:
If you want to use the SDK, what you will need to do is list files and directories recursively in the source directory and then copy individual files from source directory to target directory. You will also need to create directories in the target directory as well. Once the copy operation is complete, then you will need to delete all files and directories from the source directory. Once the source directory is empty, only then you will be able to delete the source directory.
You will need to do all this because the REST API does not natively support move operation. To accomplish move, you will need to perform copy operation followed by delete operation.
If you want to use System.IO, then you will need to mount the file share as a shared network drive so that you can get a drive letter assigned to that file share. Once you have that, then you will be able to use operations like Directory.Move available in System.IO namespace.
UPDATE
Please try the code below:
using System;
using System.Threading.Tasks;
using Azure.Storage.Files.Shares;
namespace SO69798149
{
class Program
{
const string MyconnectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key";
const string MyshareName = "share-name";
const string SourceDirectoryName = "source-directory-name";
private const string RenamedDirectoryName = "new-directory-name";
static async Task Main(string[] args)
{
ShareClient myshare = new ShareClient(MyconnectionString, MyshareName);
ShareDirectoryClient sourceDirectoryClient = myshare.GetDirectoryClient(SourceDirectoryName);
ShareDirectoryClient targetDirectoryClient = myshare.GetDirectoryClient(RenamedDirectoryName);
await RenameDirectory(sourceDirectoryClient, targetDirectoryClient);
Console.WriteLine("Directory renamed.");
}
static async Task RenameDirectory(ShareDirectoryClient sourceDirectoryClient,
ShareDirectoryClient targetDirectoryClient)
{
//Create target directory
await targetDirectoryClient.CreateIfNotExistsAsync();
//List files and folders from the source directory
var result = sourceDirectoryClient.GetFilesAndDirectoriesAsync();
await foreach (var items in result.AsPages())
{
foreach (var item in items.Values)
{
if (item.IsDirectory)
{
//If item is directory, then get the child items in that directory recursively.
await RenameDirectory(sourceDirectoryClient.GetSubdirectoryClient(item.Name),
targetDirectoryClient.GetSubdirectoryClient(item.Name));
}
else
{
//If item is file, then copy the file and then delete it.
var sourceFileClient = sourceDirectoryClient.GetFileClient(item.Name);
var targetFileClient = targetDirectoryClient.GetFileClient(item.Name);
await targetFileClient.StartCopyAsync(sourceFileClient.Uri);
await sourceFileClient.DeleteIfExistsAsync();
}
}
}
//Delete source directory.
await sourceDirectoryClient.DeleteIfExistsAsync();
}
}
}

Open multiple files by selecting single file in a sequence - Generate file name from multiple files

I have a UWP app that has an option to open an Image Sequence. Typically image sequences are named as follows:
MyImageSequence_00001.png,
MyImageSequence_00002.png,
...
MyImageSequence_01024.png,
MyImageSequence_01025.png,
Generally, these image sequences will be named something like "UniqueFileName_v06" with a frame number like "_0004" following. In a lot of cases you could have multiple image sequences in the same folder but they will have different leading names such as "UniqueFileName_v04_Reflections" or "UniqueFileName_v04_Depth". The starting frame number could be any number and the frame number will increase sequentially to the last frame of the sequence.
I'm currently having the user select all of the files manually as follows:
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.ViewMode = PickerViewMode.List:
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.FileTypeFilter.Add(".tif");
files = await filePicker.PickMultipleFilesAsync();
What I am hoping to do is allow a user to open their image sequence by selecting a single image from the sequence in a file picker. So if they were to select "MyImageSequence_00003.png" the app will find and select the rest of the files in the sequence (MyImageSequence 00001-01025) and add them to the filePickerSelectedFilesArray variable files.
Secondly, I'd like to generate a composite string name from the list of files. So, generate a name like "MyImageSequence_00001-01025" to identify the image sequence.
Any thoughts on the best way to go about this? Seems like I should use something like the StorageItemsQuery class but unsure if that is the best method nor how to go about it.
Thanks for any help you can provide!
I'm using the following to get the Image sequence name without frame numbers:
File path to input C:\Users\Me\Desktop\MyImageSequenceFolder\MyImageSequence_00001.png
string input = Path.GetFileNameWithoutExtension(fileFromImageSequence.Path);
string pattern = #"\d+$";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
sequenceName = result.TrimEnd('_');
sequenceName output is "MyImageSequence"
At present, native UWP applications cannot access files through paths (permission restrictions).
According to your description, the picture sequences are all placed in the one folder. Then before selecting a file through FileOpenPicker, we can first obtain access to the folder and add it to the future access list.
public async Task<StorageFolder> GetImageFolderAsync(string token = "")
{
StorageFolder folder;
if (string.IsNullOrEmpty(token))
{
var folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
string folderToken = StorageApplicationPermissions.FutureAccessList.Add(folder);
// save token to LocalSettings or other location.
// ...
}
}
else
{
folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(token);
}
return folder;
}
After adding the folder to FutureAccessList, we will get a Token. In the future, we can get the folder directly through Token without opening FolderPicker. This is equivalent to the user authorizing the application to access the folder.
Then, when the user selects the file through FileOpenPicker, you can obtain the folder by the above method, and find the same type of image sequence in the folder according to the rules you set (like StorageItemsQuery).
public async Task<IReadOnlyList<StorageFile>> SearchSameImages(StorageFile fileFromImageSequence)
{
string input = Path.GetFileNameWithoutExtension(fileFromImageSequence.Path);
string pattern = #"\d+$";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
sequenceName = result.TrimEnd('_');
var queryOptions = new QueryOptions();
queryOptions.ApplicationSearchFilter = $"\"{sequenceName}\"";
string token = GetToken(); // From LocalSettings or other place.
var folder = await GetImageFolderAsync(token);
var queryResult = folder.CreateFileQueryWithOptions(queryOptions);
var files = await queryResult.GetFilesAsync();
return files;
}
In this way, you can ask the user to specify a folder to store the sequence of pictures when the application first starts, and retain access to the folder after that. Use the method provided above to query the sequence of pictures in a folder based on a single file.
Thanks.

Downloading images from publicly shared folders and sub-folders on Dropbox

This is similar to my previous question:
Downloading images from publicly shared folder on Dropbox
I have this piece of code (simplified version) that needs to download all images from publicly shared folder and all sub-folders.
using Dropbox.Api;
using Dropbox.Api.Files;
...
// AccessToken - get it from app console
// FolderToDownload - https://www.dropbox.com/sh/{unicorn_string}?dl=0
using (var dbx = new DropboxClient(_dropboxSettings.AccessToken))
{
var sharedLink = new SharedLink(_dropboxSettings.FolderToDownload);
var sharedFiles = await dbx.Files.ListFolderAsync(path: "", sharedLink: sharedLink);
// var sharedFiles = await dbx.Files.ListFolderAsync(path: "", sharedLink: sharedLink, recursive: true);
// "recursive: true" throws: Error in call to API function "files/list_folder": Recursive list folder is not supported for shared link.
foreach (var entry in sharedFiles.Entries)
{
if (entry.IsFile)
{
var link = await dbx.Sharing.GetSharedLinkFileAsync(url: _dropboxSettings.FolderToDownload, path: "/" + entry.Name);
var byteArray = await link.GetContentAsByteArrayAsync();
}
if (entry.IsFolder)
{
var subFolder = entry.AsFolder;
// var folderContent = await dbx.Files.ListFolderAsync(path: subFolder.Id);
// var subFolderSharedLink = new SharedLink(???);
}
}
}
How do I list entries of all sub-folders?
For any given subfolder, to list its contents, you'll need to call back to ListFolderAsync again, using the same sharedLink value, but supplying a path value for the subfolder, relative to the root folder for the shared link.
For example, if you list the contents of the folder shared link, and one of the entries is a folder with the name "SomeFolder", to then list the contents of "SomeFolder", you would need to make a call like:
await dbx.Files.ListFolderAsync(path: "/SomeFolder", sharedLink: sharedLink);

Windows Store App c# replace SQL Database in local storage

I am trying to overwrite the database in my local folder with one that is in my pictures directory I use the following
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
String picName = "SqlLiteWin8-1.db";
var file2 = await storageFolder.TryGetItemAsync(picName) as IStorageFile;
StorageFile fileCopy = await file2.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder, "SqlLiteWin8-1.db", Windows.Storage.NameCollisionOption.ReplaceExisting);
It seems to work and the file is copied and overwrite the old one
Problem is when I run the app it still shows the old data
I checked by manually deleting the db in the local state folder of the app then running the code and it copies it to the folder.
I think its still using the database in the app package and not the one in the local folder
The database in the pictures directory is identical to the one stored in the app except 1 record is modified
I want to overwrite it so that I can just supply a new db file to users and the app will use the new data or is there a way of bypassing this and read the DB straight from the pictures directory instead of the local folder
one though was it uses the following code to check if the db exists and copies if it doesnt could this be what is causing it not to work
public static async Task<bool> checkDataBaseConnection()
{
bool isDatabaseExisting = false;
try
{
var uri = new Uri("ms-appx:///SqlLiteWin8-1.db"); //in application folder
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
var destinationFolder = ApplicationData.Current.LocalFolder;//local appdata dir
//await file.DeleteAsync();
// var f = await destinationFolder.GetFileAsync("data.db3");
await file.CopyAsync(destinationFolder);
}
catch (Exception ex)
{
throw ex;
isDatabaseExisting = false;
}
if (!isDatabaseExisting)
{
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("AFSMOJO.db");
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
}
return isDatabaseExisting;
}
As always your help is greatly appreciated
Mark
Ignore my stupidity I found what was wrong I was connecting to the internal database instead of the local one
I changed
var db = new SQLiteConnection("SqlLiteWin8-1.db");
to
var db = new SQLiteConnection(Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "SqlLiteWin8-1.db"));
and now i can change my db in the picture folder and it copies it to the local one and uses the new data
Thanks
Mark

How to check a folder exists in DropBox using DropNet

I'm programming an app that interact with dropbox by use DropNet API. I want to check if the folder is exist or not on dropbox in order to I will create one and upload file on it after that. Everything seen fine but if my folder is exist it throw exception. Like this:
if (isAccessToken)
{
byte[] bytes = File.ReadAllBytes(fileName);
try
{
string dropboxFolder = "/Public/DropboxManagement/Logs" + folder;
// I want to check if the dropboxFolder is exist here
_client.CreateFolder(dropboxFolder);
var upload = _client.UploadFile(dropboxFolder, fileName, bytes);
}
catch (DropNet.Exceptions.DropboxException ex) {
MessageBox.Show(ex.Response.Content);
}
}
I'm not familiar with dropnet, but looking at the source code, it appears you should be able to do this by using the GetMetaData() method off of your _client object. This method returns a MetaData object.
Example:
//gets contents at requested path
var metaData = _client.GetMetaData("/Public/DropboxManagement/Logs");
//without knowing how this API works, Path may be a full path and therefore need to check for "/Public/DropboxManagement/Logs" + folder
if (metaData.Contents.Any(c => c.Is_Dir && c.Path == folder)
{
//folder exists
}

Categories