首页 > 解决方案 > UWP 可以使用 StorageFile 运行进程吗?

问题描述

有这个代码:

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#uwpprocessstoragefilestoragefolder

解决方案


您可以通过“桌面桥”将 Win32 应用打包到 UWP 应用包中,然后使用FullTrustProcessLauncher运行并通过应用服务与其通信。这是一个样本。


推荐阅读