首页 > 解决方案 > 我无法从 Win32 未打包的应用程序访问 ApplicationData.Current?

问题描述

我的代码:

using System;
using Windows;

namespace SampleSynthesis {
  class Program {
    static async System.Threading.Tasks.Task Main(string[] args) {

      var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
      Windows.Media.SpeechSynthesis.SpeechSynthesisStream stream =
          await synth.SynthesizeTextToStreamAsync("I want to listen to this text");

      using (var reader = new Windows.Storage.Streams.DataReader(stream)) {
          await reader.LoadAsync((uint)stream.Size);
          Windows.Storage.Streams.IBuffer buffer = reader.ReadBuffer((uint)stream.Size);
          Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("audio.wav");
          await Windows.Storage.FileIO.WriteBufferAsync(file, buffer);
    }
  }
}

使用此命令编译:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\Roslyn\csc.exe" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Speech.dll","C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll","C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.runtime.dll","C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17763.0\Windows.winmd","C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Threading.Tasks.dll" tts.cs

我正进入(状态:

Unhandled Exception: System.InvalidOperationException: The process has no package identity. (Exception from HRESULT: 0x80073D54)
   at Windows.Storage.ApplicationData.get_Current()
   at SampleSynthesis.Program.<Main>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at SampleSynthesis.Program.<Main>(String[] args)

也许存在另一种保存wav的方法?我知道我可以使用更简单的方法SetOutputToWaveFile(),但是由于合成音频中的裂缝,我想尝试另一种方法。我需要使用桌面桥吗?而不是Windows.Storage.ApplicationData.Current.LocalFolder我尝试Windows.Storage.ApplicationData.Current.TemporaryFolder并得到了同样的例外。

标签: windowsuwpwindows-10desktop-bridge

解决方案


Windows.Storage.ApplicationData.Current 仅适用于具有标识的打包应用(UWP 应用和桌面桥应用)。未打包的 Win32 EXE 没有自己隔离应用数据的概念。

目前尚不清楚您为什么需要在这里这样做,或者您要完成什么。这应该与音频播放中的裂缝无关。


推荐阅读