我无法从Win32未打包的应用程序访问ApplicationData.Current吗?

时间:2019-04-26 16:31:40

标签: windows uwp windows-10 desktop-bridge

我的代码:

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(),但是由于cracks in the synthesized audio,我想尝试其他方法。我需要使用Desktop Bridge吗?我尝试使用Windows.Storage.ApplicationData.Current.LocalFolder而不是Windows.Storage.ApplicationData.Current.TemporaryFolder并得到了相同的异常。

1 个答案:

答案 0 :(得分:2)

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

目前尚不清楚您为什么需要在这里进行此操作,或者您要完成什么。这与音频播放中的裂纹无关。