【问题标题】:C# Access Denied UWPC# 访问被拒绝 UWP
【发布时间】:2021-09-18 20:17:24
【问题描述】:

我尝试使用这样的参数运行控制台应用程序

public void ExecuteCommand(String command)
        {
            Process p = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = @"/k " + command; // cmd.exe spesific implementation
            p.StartInfo = startInfo;
            p.Start();
        }

private void Button_Click(object sender, RoutedEventArgs e)
        {
            ExecuteCommand($@"C:\Users\conta\Desktop\Project1.exe {Username.Text} {getdif()}");
        }

首先,我搜索并找到了如何运行程序,但由于某种原因 /c 不起作用,控制台窗口立即消失,所以我使用 /k,所以我可以看到控制台窗口,它说访问被拒绝,这就是问题所在我该如何解决?我搜了一下,发现你不能给 UWP 应用管理员权限,有什么办法可以解决这个问题吗?

【问题讨论】:

标签: c# .net uwp


【解决方案1】:

由于 uwp 被沙盒化,Process.start() 在 uwp 中不受支持。如果你想从 uwp 应用程序启动一个 exe 文件,我建议你可以使用FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync 来执行此操作。

请参考以下步骤。

  1. 右键项目->添加引用->通用Windows->扩展->Windows Desktop Extensions for the UWP
  2. 右键资产文件夹->添加->现有项目->你的.exe文件
  3. 在 Package.appxmanifest 中添加 windows.fullTrustProcess 扩展,例如 this

Package.appxmanifest:

<Package xmlns=http://schemas.microsoft.com/appx/manifest/foundation/windows10
….
    xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
    xmlns:rescap=http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities
IgnorableNamespaces="uap mp">
  ...
  
<Applications>    
<Application Id="App"..>
      ..  
      <Extensions>
          <desktop:Extension Category="windows.fullTrustProcess" Executable="Assets\filename.exe" />         
        </Extensions>    
</Application>
</Applications>

<Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="runFullTrust"/>
</Capabilities>
</Package>

用法:

private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
       }

【讨论】:

  • 当我点击按钮应用程序立即关闭
  • 调试时返回异常用户未处理
  • “立即关闭”是什么意思?单击按钮时exe没有运行吗?
  • 是的 exe 没有运行并且 uwp 应用程序本身崩溃
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
  • 2016-08-12
相关资源
最近更新 更多