I am trying to develop an app which launches a regular .exe application from metro app using launcher class. MSDN provided a sample here and a stackoverflow sample is here
The problem is that my metro gives error of "file not found" even the file is there. i have tried to place file on other drives as well but the problem persists
here is my code sample
// Path to the file in the app package to launch
string imageFile = #"E:\App.exe";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
/* error in the above line .it says file not found The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)*/
if (file != null)
{
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
LaunchFileAsync is for launching a file in its default program.
http://msdn.microsoft.com/library/windows/apps/Hh701461
I am not convinced it will work with an .exe
The correct usage is something like:
LaunchFileAsync("images\\picturesofcats.png");
This then opens a picture of cats in your default image viewer.
This will not work for an .exe due to sandboxing, and because .exe has no default opener.
There are a few tricks to get around this, see: Launching a Desktop Application with a Metro-style app
Generally, you are working against the design of Windows 8 to do this, so you might want to reconsider your approach.
Related
I want users to be able to open a file using my MAUI app. For example in Windows they could right-click on the file in File Explorer, choose my app in "open with" and then my app would open the file.
I have put file type associations in the Windows app manifest, and it does successfully launch my app when the file is opened by the user in File Explorer.
My problem is, how do I get notified that the reason my app was launched was because they want to open a file, and how do I get the file path?
If this was a regular UWP app and not a MAUI app, I could overload Application.OnFileActivated and handle it there. However I have not been able to find that in MAUI. link to MAUI source
Should I try to handle this by overloadng MauiWinUIApplication.OnLaunched for the Windows version of my app? Or do I have to add handlers via ConfigureLifecycleEvents? Or somewhere else?
I see that OnLaunched is being triggered when I try to open my app with the file, but a) args.UWPLaunchActivatedEventArgs.Kind is always ActivationKind.Launch and never ActivationKind.File and b) the file path doesn't appear in the LaunchActivatedEventArgs object anywhere. Obviously if nothing else I will need to get the file path somehow.
Overloading MauiWinUIApplication.OnLaunched in App.xaml.cs was the correct approach, but the args it gets are buggy (they never have ActivationKind.File and never include the path to the file). A workaround for this bug is to get the real args from another API.
protected override void OnLaunched(LaunchActivatedEventArgs buggyArgs)
{
base.OnLaunched(buggyArgs);
var goodArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
switch (goodArgs.Kind)
{
case ExtendedActivationKind.File:
var data = goodArgs.Data as IFileActivatedEventArgs;
var paths = data.Files.Select(file => file.Path).ToArray();
// Do something
break;
}
}
I'm new in xamarin forms and I need to open/save some data. I chose interlanl storage, becosue it won't be a big database. Everything works fine I can save and then read fiel. The problem is showing when I'm trying to move the app from the computer to my smartphone. First, I don't understand why in the folder app/Android/bin/Debug (on my computer) there are two files with .apk extension. One is named "com.companyname.myapp.apk" and the other is named "com.companyname.myapp-Signed.apk". After moving these files to the main folder on my phone I can install only the second one, but after opening it on phone the app is closing immediately. I'm sure that the reason for that is reading file because when I remove the reading method from app, the app is opening normally. Another stranger thing (for me) is that when I plugged the phone into a computer using USB debugging and run the program from a visual studio everything is ok, even if I uninstall app and instal it using the file "com.companyname.myapp-Signed.apk". Here is my reading method:
var path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "test.txt");
if (!File.Exists(path))
{
var writer = new StreamWriter(path);
writer.Write("some data");
}
var reader = new StreamReader(path, true);
string data;
data = reader.ReadToEnd();
Above code is calling in constructor in MainPage.xaml.cs fiel, maybe that is important information.
Is it something in my code or the problem is in the project properties. As I read the app doesn't need any permission to works with internal storage, so I have no idea what is wrong.
I have an ASP.NET MVC web application with a button to download an .exe file for an existing WPF application. It downloads fine, but when clicked in the browser window, it doesn't execute. How would I fix this?
[HttpGet]
public FileResult downloadFile()
{
var fileName = string.Format("MyApp.exe", DateTime.Today.Date.ToString("dd-MM-yyyy") + "_1");
var tempOutPutPath = Server.MapPath(Url.Content("~/File/")) + fileName;
byte[] finalResult = System.IO.File.ReadAllBytes(tempOutPutPath);
if (System.IO.File.Exists(tempOutPutPath))
System.IO.File.Delete(tempOutPutPath);
if (finalResult == null || !finalResult.Any())
throw new Exception(String.Format("No Files found"));
return File(finalResult, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(fileName));
}
This is a security issue, so i would say no. Imagine the disaster if links on malicous sites could download and run programs on the end users pc.
Don't know why you need it, but if you need to launch your wpf app from a browser. Then you could make a link to reference a uri scheme that points to your already installed application. You would have to add it to registry during an install routine or a one time job in your app.
Registering an Application to a URI Scheme in windows 10
Hope this helps
I think this is the closest you can get to run your application from a browser. But making it launch automatically after download is not possible.
like matcsr pointed out what you cannot force a user to execute a file in their download folder. But if what you want is to distribute your WPF app from the web then you need to setup a ClickOnce Web deployment. More info Here: Choose a ClickOnce deployment strategy
We are currently working on a project in UWP where we have to start an external application to modify some documents.
We looked into the Windows.System.Launcher API but it seems that we need more than what it can offer us.
As we launched the application from a file, we use the LaunchFileAsync method, based on the example given by the MSDN :
async void DefaultLaunch()
{
// Path to the file in the app package to launch
string imageFile = #"images\test.png";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
So far, the example suit us well but we also need to be warned when the user is done with the file. The best would be to be able to give the launcher a callback method.
We haven't found anything like that yet in the documentation. Is this even possible ? Do we need to use another solution ?
TL;DR : Is there a solution to open another application from a UWP app and wait for it to return a result object ?
If the external app is also a UWP app then Launcher.LaunchUriForResultsAsync is designed for this. It will launch the target app then wait for the app to call back with the results.
See Launch an app for results for a full walkthrough of how this works.
If the target app isn't a UWP app then you can implement the same thing yourself: both apps declare a protocol. The client launches the server with the server's protocol. When the server's done it notifies the caller by launching the client's protocol.
You might also want to look into App Services which allow a UWP server app to expose a REST-like service to clients on the local system.
The app process isolation model means you can't do this from a UWP app. As you just want to know when an arbitrary program has finished you could write this in traditional .net/win32 and include that in your UWP app via the desktop bridge.
I'm facing a weird issue, when trying to access file on SD card with code:
var path = #"D:\Test\test.txt";
try
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
}
catch (Exception ex) { Debug.WriteLine($"File access failed due to {ex.Message}"); }
path = #"Test\test.txt";
StorageFile file2 = await (await KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault().GetFileAsync(path);
The file is on SD card, I've declared RemovableStorage capability, and added FileTypeAssociation. I can get the file when I first debug the app, but on the second run I get UnauthorizedException with the first StorageFile. Amazingly the second try to get the file via RemovableStorage works every time.
If I only restart the phone and debug app once again - it will again work, but still only for the first time.
Is accessing files by StorageFile.GetFileFromPathAsync() somehow limited?
Am I missing something?
UPDATE:
Seems like on newest version of emulator 10856 I get an exception on every run, what may mean that there will be no way to access file via full path.
The picker allows access because the user provided the file to you, not the path. A winstore app should not be dependent upon a users drive / folder layout, that is why you don't have authority to access files by absolute path. In this case, we should firstly allow access to removable devices in manifest file, then we should use system's RemovebleDevices folder to access files of allowable types.
Also it is strange you can work with the absolute path the first time. In my test device, I always get the UnauthorizedAccessException and "Access is denied" error if using absolute path. My device is of build 10586.11.