英文:
How to open a media file in the specified directory without adding the file to the project
问题
我想要在本地网络上的计算机上以完整目录打开一个文件。类似这样的:
var path = "C:/User/Folder/video.mp4";
var stream = ...;
// 代码
mediaElement.SetSource(stream, "");
我在互联网上没有找到任何方法。只有添加文件到项目的选项,但这不是我所需要的。
英文:
I want to open a file on a computer on a local network with its full directory. Something like that:
var path = "C:/User/Folder/video.mp4";
var stream = ...;
\\code
mediaElement.SetSource(stream,"");
I haven't found any ways on the internet. There were options only to add a file to the project, but this is not what I need.
答案1
得分: 0
以下是翻译好的部分:
FileOpenPicker
第一种方法是使用FileOpenPicker。当您使用它时,它会显示一个窗口,让用户选择并打开文件。它不需要任何额外的权限,但用户需要知道文件在哪里,并自行选择文件。
代码:
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add("*");
StorageFile file = await openPicker.PickSingleFileAsync();
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
mediaPlayer.Play();
broadFileSystemAccess
第二种方法是使用名为broadFileSystemAccess的受限权限。您需要将该权限添加到您的应用程序清单文件中,如下所示:
<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
然后,您可以使用Windows.Storage Namespace中的API通过路径直接获取文件。例如:
StorageFile file = await StorageFile.GetFileFromPathAsync("C:\\Users\\Administrator\\Desktop\\video.mp4");
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
mediaPlayer.Play();
当您使用此权限时,请确保用户在安装应用程序后已启用文件系统隐私设置。您可以在设置 > 隐私 > 文件系统中找到这些设置。
英文:
>How to open a media file in the specified directory without adding the file to the project
There are two ways to do it. FileOpenPicker and broadFileSystemAccess
FileOpenPicker
The first one is to use FileOpenPicker. When you use it, it will show a window that lets the user choose and open files. It does not require any extra permissions but users need to know where the file is and pick the file by themself.
Code:
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add("*");
StorageFile file = await openPicker.PickSingleFileAsync();
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
mediaPlayer.Play();
broadFileSystemAccess
The second one is to use the restricted capability called broadFileSystemAccess. You need to add the capability into your app's manifestfile like this:
<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
Then you could use APIs from Windows.Storage Namespace to get files directly via path. Like:
StorageFile file = await StorageFile.GetFileFromPathAsync("C:\\Users\\Administrator\\Desktop\\video.mp4");
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
mediaPlayer.Play();
When you use this capability, please make sure that user has enabled the file system privacy setting after installed the app. You could find the settings in Settings > Privacy > File system.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论