【问题标题】:UWP Possible to run a process using StorageFile?UWP 可以使用 StorageFile 运行进程吗?
【发布时间】:2019-11-09 18:06:19
【问题描述】:

拥有此代码:

StorageFolder storageFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(file.folderToken);            
StorageFile storageFile = await storageFolder.CreateFileAsync("run.exe", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(storageFile, Properties.Resources.youtube_dl1);

我可以使用文件夹令牌在用户选择的位置创建文件。我现在想在一个进程中运行该文件:

Process checkforupdates = new Process();
checkforupdates.StartInfo.FileName = storageFile.Path;
checkforupdates.StartInfo.Arguments = "-x https://www.youtube.com/watch?v=uzr5jm9ueja";
checkforupdates.StartInfo.UseShellExecute = false;
checkforupdates.StartInfo.RedirectStandardOutput = true;
checkforupdates.StartInfo.RedirectStandardOutput = true;
checkforupdates.StartInfo.RedirectStandardError = true;
checkforupdates.StartInfo.CreateNoWindow = true;
checkforupdates.EnableRaisingEvents = true;
checkforupdates.OutputDataReceived += (s, er) =>
{
Debug.WriteLine("dsds" + er.Data);
};
checkforupdates.Start();
checkforupdates.BeginOutputReadLine();
checkforupdates.BeginErrorReadLine();
checkforupdates.WaitForExit();  

如果我使用,这很好用:

 StorageFolder storageFolder = ApplicationData.Current.LocalFolder;

但只要我使用用户选择的文件夹,我就会收到异常。我确定异常是 UnauthorizedAccessException 但我需要一个 bunlded exe 在文件夹中解压缩,然后运行命令以便在用户选择的文件夹中生成文件。这样做时,我需要解析正在运行的命令行实用程序的输出。

有什么办法吗?

【问题讨论】:

    标签: c# uwp process storagefile storagefolder


    【解决方案1】:

    您可以通过“桌面桥”将 Win32 应用打包到 UWP 应用包中,然后使用 FullTrustProcessLauncher 运行并通过应用服务与其通信。 Here is a samples.

    【讨论】:

    • 是的,我看到了,但我有 2 个问题:1) 如何在清单声明中为可执行路径赋予变量? 2)我怎样才能收到流程的输出?我需要 ReadLine 输出而不是 ReadToEnd 输出。我知道我可以使用 localsettings 传递任意参数,这足以满足我的需要
    • 您将使用宿主应用中的应用服务与 Win32 应用进行通信。
    猜你喜欢
    • 2017-09-18
    • 2018-08-22
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 2011-01-19
    • 1970-01-01
    相关资源
    最近更新 更多