如何在 Windows Store 应用程序中运行命令?

问题描述:

我需要从 Windows Store 应用程序运行命令?

I need to run a command from windows store app?

命令是这样的:java -jar abc.jar

the command is something like this : java -jar abc.jar

我该怎么做?

我试过这个,但没有运气.它说找不到文件.

I tried this but with no luck. It says file not found.

       string exeFile = @"C:\DATA\start.bat";

        var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(exeFile);

        if (file != null)
        {
            // Set the option to show the picker
            var options = new Windows.System.LauncherOptions();
            options.DisplayApplicationPicker = true;

            // Launch the retrieved file
            bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
            if (success)
            {
                // File launched
            }
            else
            {
                // File launch failed
            }
        }

应用容器阻止应用商店应用的这种行为.

The app container blocks this behavior for Store apps.

首先,您正试图通过您的软件包 InstalledLocation 文件夹获取 StorageFile,但这是行不通的.InstalledLocation 是一个 StorageFolder,它的 GetFileAsync 只在该直接文件夹中查找文件.这就是它返回找不到文件的原因.

First of all, you're attempting to obtain a StorageFile through your package InstalledLocation folder, which will not work. InstalledLocation is a StorageFolder, and its GetFileAsync looks for files only within that immediate folder. This is why it's returning file not found.

采用任意路径的 API 是 Windows.Storage.StorageFolder.GetFileFromPathAsync.但是,您访问文件的能力受到应用程序容器的限制.默认情况下,您可以访问包文件夹或应用程序数据位置中的文件,或者如果您在清单中声明了访问权限,则可以访问各种媒体库,否则您必须通过文件选择器,以便用户知道您在做什么做并且可以同意.简而言之,这是访问 c:\data 等位置中的文件的唯一方法.您可以使用 Association 启动示例 的场景 1 和选择并启动"按钮.

The API that takes an arbitrary path is Windows.Storage.StorageFolder.GetFileFromPathAsync. However, your ability to access files is limited by the app container. You can access files in your package folder or app data locations by default, or the various media libraries if you've declared access in the manifest, but otherwise you have to go through the file picker so the user is aware of what you're doing and can grant consent. Simply said, this is the only way you'll get to a file in a location like c:\data. You can play with this using Scenario 1 of the Association launching sample and the "Pick and Launch" button.

如果您可以获得该访问权限,那么您将能够启动文件如果它不是被阻止的文件类型.与另一个应用程序关联的数据文件(如 .docx)工作正常,但出于明显的安全原因,可执行文件被完全阻止.您可以使用我上面链接的示例进行尝试——选择 .bat、.cmd、.exe、.msi 等,您会看到 LaunchFileAsync 失败.

If you can get that access permission, then in you'll be able to launch a file if it's not a blocked file type. Data files (like a .docx) that are associated with another app work just fine, but executables are wholly blocked for what should be obvious security reasons. You can try it with the sample I linked to above--pick .bat, .cmd, .exe, .msi, etc. and you'll see that LaunchFileAsync fails.

另请注意,另一个启动器函数 LaunchUriAsync 也出于同样的原因阻止了 file:///.

Also note that the other launcher function, LaunchUriAsync, also blocks file:/// for the same reasons.