open specific file in appropriate app from inside Windows 8 App - c#

I'm creating Windows 8 app in which i need to list specific files in a page and then let user open that file. I'm using C# as backend.
I want to ask this (as in the image below), to my user when they try to open any app listed in my app page.
when user select specific app, then the file that is clicked should open in that app.

Unlike previous versions of Windows, the User is now in control of file associations.
To show the launch using the Window above, you can refer to the complete example of launching options here.
The core of it is deciding on a Point on the screen (which is openWithPosition in the example below) and then calling LaunchUriAsync with the options set to display the window.
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
options.UI.InvocationPoint = openWithPosition;
options.UI.PreferredPlacement = Windows.UI.Popups.Placement.Below;
// Launch the URI.
bool success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
The linked example code contains a function that can compute a reasonable Point given a XAML element.

Related

How to open new browser window in C#, WinUI 3?

I am trying to open a URL in a new browser window. Specifically, a new browser window. The following code launches the URL in the default browser, but always opens the URL as a new tab in an existing browser window (if one exists). I want to launch the new tab in a new browser window, regardless of if one (or more) browser windows are already open.
This code launches the URL in WinUI 3 (in a new tab in an existing browser window), and appears to be the simplest:
Windows.System.Launcher.LaunchUriAsync(new Uri("http://google.com"));
This code does the same thing, and also works in WinUI 3 (credit to this answer here):
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = "http://google.com";
myProcess.Start();
Please note that the following code does NOT work at all in WinUI 3:
System.Diagnostics.Process.Start("http://google.com");
EDIT: Additional Ruled-out Method
The following modification of Process.Start() works in WinUI 3, but like the above methods only opens new tabs in existing browser windows, and thus is not a solution to the question above (also note that it forces using a specific browser, rather than the default browser):
Process.Start("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe", "http://google.com");
You can do it by calling a JavaScript command into webview2:
await webView.ExecuteScriptAsync("window.open('http://www.bing.com');");
Make sure you have either set the source of webView, or that you have already called EnsureCoreWebView2Async().

windows explorer cant see files written by c# app File.WriteAllLines

I am having a strange problem that I am unable to access files written by my c# application. My app basically does :
var file = "C:\\Users\\Public\\Documents\\something.txt";
List<string> content = new List<string> { "one thing", "two things" };
Console.WriteLine(System.IO.File.Exists(file));
System.IO.File.WriteAllLines(file, content);
Console.WriteLine(System.IO.File.Exists(file));
The first time I run the app, the output is
False
True
Yet I cannot see the written file in Windows Explorer (Windows 10). I get no exceptions attempting to write the file. The second time I run the app, the output is :
True
True
According to my application the file is being written however Windows thinks differently. As a sanity check I spun up a second app that opens a dialog using OpenFileDialog. When I run that, I am able to see my written files! Windows explorer still cannot. Attached is a screenshot of windows explorer and my openfiledialog side by side.
If I go to notepad and browse for the file I cannot see it or manually type in the name.
Its been a long week of work, there must be some dumb explanation...? Help! :-)
Screenshot - windows explorer on left, c# app open dialog on right :
https://imgur.com/a/8ZTDIe6
per #BACON 's suggestion in the comments above I discovered that after disabling the Comodo anti-virus I am able to write and see my files.
I believe the software is running my app or either only allowing IO from my app in some kind of container. I need to figure out how to grant my apps proper permissions through the anti-virus software, but that was the culprit.

How to create a UWP application which only opens a specific url

I want to create a UWP application which only opens a specific url in the browser. So I mean that this app will have an icon in the start menu but when a user clicks this icon nothing happens but only my url is opened in the browser. Is it possible? How to implement such UWP application?
I created an App class and in OnLaunched method I wrote this code:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
var task = Windows.System.Launcher.LaunchUriAsync(new Uri("https://stackoverflow.com")).AsTask();
task.Wait();
CoreApplication.Exit();
}
This works fine. The only problem is that white window of my app is opened and closed after about 1 second.
Update 1:
The main purpose of my app is to work as an appservice. Another component of my system successfully communicates with it. If it is possible not to show the icon in the start menu at all it would be great solution. However, I don't know how to hide my app from the start menu. So another appropriate solution for me is just to open the url of my specific web site without showing a splash window once a user clicks the icon in the start menu.
I don't care about certification in the store.
Even if you can successfully implement such behavior, the app window and splash screen will still display (even if just for a while) and the app will definitely not pass Microsoft Store submission (because it exits during launch and it brings no additional value over the website itself)

Cortana App Open a Webpage

I need to write an Cortana extension (app, experience, plugin, etc..) that will receive a query from the user and based on this query construct an webpage URL and open that URL in the default client browser. My question is, can an App do that? Could you share any code example? Can this app run both on cell phones and windows laptops/desktops?
I believe so. You can create an app that implements Windows.ApplicationModel.AppService IBackgroundTask cortana will activate your background service if you register a VCD file with the right 'ListenFor' commands. You wire your app via "VoiceCommandService Target=".
Your background app would then launch the default browser.
Here is a step by step guide.
Here is the code:
string uriToLaunch = #"http://www.bing.com/";
var uri = new Uri(uriToLaunch);
await Windows.System.Launcher.LaunchUriAsync(uri);

How can I open an image in the default photos app?

I have an app that captures images from the device's camera and saves them as StorageFiles to a folder in my app's roaming data. I've been trying to make a page that will open the image and show a preview, but I've been having many problems doing that, so I want to just open the image in the default photos app. However, I can't find any way to do that. Using await Launcher.LaunchUriAsync(new Uri(#"ms-appdata:///roaming/folder/img.jpg")); asks if I want to search for an app in the Store (searches for "ms-appdata"). Does the native Photos app for Windows (Phone) have a dedicated URI scheme? Also, I am targeting Windows 10 with this app, so the URI schemes (if any) may have changed.
Launcher.LaunchUriAsync is for launching an application using the URI scheme, this is not what you want.
Instead you'll want to launch an app based on a file:
Get the image as a StorageFile
var imageFile = await StorageFile.GetFileFromPathAsync(#"ms-appdata:///roaming/folder/img.jpg");
Then you tell the OS to launch an app to handle that file. It's then up to the user to choose which app to handle the image file:
var success = await Windows.System.Launcher.LaunchFileAsync(imageFile);
You can read more about "launching" files here.

Categories

Resources