我有这两个函数来保存和读取Windows应用商店项目中的文件:
public async Task GravaFicheiro(string url,string nome)
{
HttpClient client = new HttpClient();
HttpResponseMessage message = await client.GetAsync(url);
StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await myfolder.CreateFileAsync(nome, CreationCollisionOption.ReplaceExisting);
byte[] file = await message.Content.ReadAsByteArrayAsync();
await FileIO.WriteBytesAsync(sampleFile, file);
var files = await myfolder.GetFilesAsync();
}
public async Task<StorageFile> LeFicheiro(string nome)
{
StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await myfolder.CreateFileAsync(nome, CreationCollisionOption.OpenIfExists);
return sampleFile;
}
现在我试图获取一个文件并使用默认应用程序打开它来打开这些文件,在这个特殊情况下我试图打开一个pdf。
public async void DefaultLaunch(string path)
{
// Path to the file in the app package to launch
string p2 = Windows.Storage.ApplicationData.Current.LocalFolder.Path +"\\"+ path;
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(p2);
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
但是当我将文件名传递给函数时,它不会被识别出来
其他信息:文件名,目录名或卷标语法不正确。 (HRESULT异常:0x8007007B)
任何人都可以帮忙吗?
答案 0 :(得分:1)
这是错误的来源:
string p2 = Windows.Storage.ApplicationData.Current.LocalFolder.Path +"\\"+ path;
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(p2);
您正在GetFileAsync
Package.Current.InstalledLocation
上呼叫StorageFolder
,但是您正在向其传递指向不同位置StorageFile
的路径ApplicationData.Current.LocalFolder
。
如果您想从LocalFolder
获取文件,请获取对它的引用:
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await local.GetFileAsync(path);