use subfolders in homegroup - c#

I'm trying to build a metro-app which shall load an image file from another computer in the same homegroup (all computers use windows 8 x64 with working homegroup). All samples I found do not use subfolders or use the filepicker.
Since all my images are in the same folder and I know their names I do not want to use the filepicker.
I activated "Pictures Library" in the appxmanifest and I can list the directories/computers in the homegroup but I'm stuck in opening files or subfolders.
Here's what I did:
var folder = await Windows.Storage.KnownFolders.HomeGroup.GetFolderAsync("homegroupname");
foreach (var a in await folder.GetFoldersAsync())
{
Debug.WriteLine(a.Name.ToString());
}
This gave me a list of the computers of the homegroup (as expected).
Here's what I tried without success:
folder = await Windows.Storage.KnownFolders.HomeGroup.GetFolderAsync(#"homegroupname\computername");
folder = await folder.GetFolderAsync(#"computername");
These attempts didn't work and I ran out of ideas. Do I have to allow the folder somewhere? Is my way of opening the (sub-)folders the right one?

I did it with the following (nearly intuitive) approach:
I create a CommonFileQuery for files of the right type and choose the one with fitting name.
List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".jpg");
fileTypeFilter.Add(".png");
var queryOptions = new QueryOptions(CommonFileQuery.OrderByDate, fileTypeFilter);
var query = KnownFolders.HomeGroup.CreateFileQueryWithOptions(queryOptions);
IReadOnlyList<StorageFile> fileList = await query.GetFilesAsync();
StorageFile file = fileList.FirstOrDefault(x => x.Name == "123_123.jpg");

Related

Is there any way to open file explorer and select a file from a UWP app?

I can open file explorer from UWP apps using Launcher.LaunchFolderAsync() (+), but is there any way to make a file selected in that file explorer window?
There are some ways to achieve this in Win32 apps which involve calling explorer.exe directly and passing parameters to it, which obviously won't work for UWP.
You can also use the Launcher.LaunchFolderAsync and use the second parameter Folder​Launcher​Options too.
Folder​Launcher​Options can make the file or folder that you want to select that use the ItemsToSelect.
ItemsToSelect is a read-only property, but you can add items to the existing list.
Here's an example, getting a folder using FolderPicker and then selecting all files:
The first is get the folder:
FolderPicker p = new FolderPicker();
p.FileTypeFilter.Add(".txt");
StorageFolder folder = await p.PickSingleFolderAsync();
And then get all files in the folder
foreach (var temp in await folder.GetFilesAsync())
I can use FolderLauncherOptions to add the item that I want to select.
var t = new FolderLauncherOptions();
foreach (var temp in await folder.GetFilesAsync())
{
t.ItemsToSelect.Add(temp);
}
Then open the file explorer
await Launcher.LaunchFolderAsync(folder, t);
You can see that the explorer will be opened while selecting all files.
You can also add folders to the ItemsToSelect and it will be selected.
See here for more details: https://learn.microsoft.com/en-us/uwp/api/Windows.System.Launcher#Windows_System_Launcher_LaunchFolderAsync_Windows_Storage_IStorageFolder_Windows_System_FolderLauncherOptions_

Using multiple DirectoryInfo calls with GoDaddy causes an error?

I'm wondering if anyone has come across this issue or knows of a better way to do this with GoDaddy as the web host?
First off I'll say this code works just fine when I run it locally. It's only when I put it out on a GoDaddy site that is causes an error. The idea behind the code is that I want to look at a "Galleries" folder and inside of that main folder I have folders like "Completed" and "Current". This is where the end user will put different folders underneath those. I then get all of those subfolders into a List which I use to create a as hyperlinks. Those hyperlinks will get all the files in that folder and display them in an image gallery.
Again, it works just fine locally but when I put it on GoDaddy I get an error like "Could not find a part of the path 'G:\PleskVhosts\websitename\httpdocs\aaw\Galleries\Current".
Doing some troubleshooting, if I just do the first directory it works fine on GoDaddy. It's not until I add a second or more that I get the above error. I'm stumped as to why I would get this since they are all in their own variables and Lists.
Here is a sample of the code. If I only use the first DirectoryInfo then all is good. It's not until I add the second one that the issue arises.
DirectoryInfo dirComplete = new DirectoryInfo(HostingEnvironment.MapPath("/Galleries/Completed/"));
DirectoryInfo[] completedFolderList = dirComplete.GetDirectories();
var completedFolders = new List<Gallery>();
foreach (var folder in completedFolderList)
{
var gallery = new Gallery();
gallery.GalleryName = folder.Name;
completedFolders.Add(gallery);
}
DirectoryInfo dirCurrent = new DirectoryInfo(HostingEnvironment.MapPath("/Galleries/Current/"));
DirectoryInfo[] currentFolderList = dirCurrent.GetDirectories();
var currentFolders = new List<Gallery>();
foreach (var folder in currentFolderList)
{
var gallery = new Gallery();
gallery.GalleryName = folder.Name;
currentFolders.Add(gallery);
}
var vm = new GalleriesViewModel();
vm.CompletedGalleries = completedFolders;
vm.CurrentGalleries = currentFolders;

How to List a my Folder in a UWP App

I have a folder, let's say "C:\Downloads\Comic" and I would list all the files in this Directory, but I didn't find a way how.
The only way I found is with the help of a FolderPicker.
private async Task PickDirAndListfiles()
{
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)
{
var files = await folder.GetFilesAsync();
foreach (var storageFile in files)
{
Debug.WriteLine(storageFile.Name);
}
}
}
But I know where the folder is and I don't want to pick it first. I want just this one folder!
How can I simply list my folder?
If you create this folder from your app you can use :
Windows.Storage.DownloadsFolder
and then you'll be able to access it.
If it's an already existing folder you'll need to ask the user to pick the folder/file and use :
StorageApplicationPermissions.FutureAccessList
to get a token that will be used for retrieving the corresponding folder/file without asking the user again and again to pick an item.
Alternatively you can declare capabilities to allow your app picking files without user interaction : Declaring capabilities

Saving file on documents library Windows 8 C#

I'm creating a simple app for windows 8 that writes me a xml file to documents library.
The problem is when i'm save the file, it saves it on skydrive and i want to save it on c:\Users\pc-name\Documents. I'm using KnownFolders.DocumentsLibrary and updated the manifest to save xml files too, otherwise i couldn't save any file in it.
public static async void XmlSaveFreeChallenge(Challenge currentChallenge)
{
var challenge = new XElement("Challenge");
var docSave = new XDocument(challenge);
challenge.Add(new XAttribute("Name", currentChallenge.Template));
var pontos = new XElement("Type", currentChallenge.Type);
docSave.Descendants("Challenge").FirstOrDefault().Add(pontos);
var folder = KnownFolders.DocumentsLibrary;
var outputStream = await folder.OpenStreamForWriteAsync("CaiMUfiles\\output\\Desafios\\" + currentChallenge.Template + ".xml", CreationCollisionOption.ReplaceExisting);
var ms = new MemoryStream();
docSave.Save(outputStream, SaveOptions.None);
await ms.CopyToAsync(outputStream);
}
The user gets to choose where the Documents library points. See the following option: settings charm -> Change PC Settings -> SkyDrive -> Save documents to SkyDrive by default
I'm not sure if it's possible for your app to override the user's choice there. Even if it is possible, it's probably better to respect the user's choice.
What Isaac McGarvey said is correct nevertheless there might by something that you can do. i did not found a way how to programmatically switch default saving folder for libraries but you still have access to all folders that is included in libraries. The only think is that you need to know absolute path so if you can save the path before then you can use this to get desired folder :
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync("AbsolutePath");
and then you can start enumerating creating or whatever you need. The problem is that if you use for example
List<StorageFolder> folder = await KnownFolders.DocumentsLibrary.GetFoldersAsync();
you wont get folder for Documents and folder for SkyDrive and folder for other linked folders to libraries you will just get all folders that is inside all of these folders in one list which mean you cannot choose where to save file.
I hope this helps a bit.

Search for files in machine in metro app using C#

I'm developing metro app using C# and XAML,As we all know we can search music files in music library using below code.
IReadOnlyList<IStorageItem> itemsList = await KnownFolders.MusicLibrary.GetItemsAsync();
What about music files in other drives(partitions) like D:,E: etc..Is there any way to search other drives for music files?
Brief: I want to search .mp3 files from my hard-drive and display them in Gridview, is there any efficient way to achieve this? Please help me.
It´s possible to search within other folders or drives, but it´s required to use the FolderPicker to select the StorageFolder...
async Task<IEnumerable<StorageFile>> FindMusicFiles()
{
var folderPicker = new FolderPicker()
{
CommitButtonText = "Yippie!",
SuggestedStartLocation = PickerLocationId.ComputerFolder,
ViewMode = PickerViewMode.List
};
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageFileQueryResult queryResult = folder.CreateFileQuery();
queryResult.ApplyNewQueryOptions(new QueryOptions(CommonFileQuery.OrderByName, new[] { ".mp3" }));
IReadOnlyList<StorageFile> files = await queryResult.GetFilesAsync();
return files;
}
return new StorageFile[] { };
}
Windows Store apps are restricted to search within KnownFolders or LocalFolder of the application, so you cannot search within other drives
It's also possible for the app to remember the folder(s) you've selected with the FolderPicker, see my post on Windows 8 App and access to file system where I've explained how to use FutureAccessList...

Categories

Resources