Windows 8 Metro App文件共享访问

我正在开发一个Windows 8 Metro应用程序,我们打算将它部署到我们公司内的几个平板电脑上。 它不适用于Windowsapp store。

我们需要应用程序访问公司网络共享上的某些目录,但强制用户使用FilePicker不是我们想要的。

我们的第一次尝试是使用await StorageFolder.GetFolderFromPathAsync("J:\\"); 。 这不起作用,并产生以下exception:

mscorlib.dll中发生了未处理的“System.UnauthorizedAccessException”类型exception

WinRT信息:无法访问指定的文件或文件夹(J:\)。 该项目不在应用程序可以访问的位置(包括应用程序数据文件夹,可通过function访问的文件夹以及StorageApplicationPermissions列表中的持久项目)。 validation文件未标记系统或隐藏文件属性。

其他信息:访问被拒绝。 (来自HRESULT的exception:0x80070005(E_ACCESSDENIED))

所以我们尝试用驱动器映射到的网络路径替换"J:\" 。 这也没有用,我们得到了这个例外:

mscorlib.dll中发生了未处理的“System.UnauthorizedAccessException”类型exception

WinRT信息:无法访问指定的文件(\\ domain \ path \ JDrive)。 validation清单中是否为此类型的文件声明了文件类型关联,并且该文件未标记系统或隐藏文件属性。

其他信息:访问被拒绝。 (来自HRESULT的exception:0x80070005(E_ACCESSDENIED))

我们的应用程序具有以下function:

  • 企业认证
  • 互联网(客户端)
  • 专用网络(客户端和服务器)

我们的应用程序没有声明

对于Windowsapp store应用来说,这一切都非常合理,但对于没有去商店的简单内部应用,是否有任何解决方法?

这是一个关于JavaScript和VB / C#/ C ++文件访问的快速入门。

此外,有关Windowsapp store应用中的文件访问和权限的这篇文章可能很有用。 从这篇文章看,你看起来正在使用正确的function,但有一个注意事项:

注意:您必须向应用清单添加文件类型关联,以声明应用可以在此位置访问的特定文件类型。

这与您所看到的错误消息有关。 你能试试吗? 这是一篇关于如何做到这一点的文章: http : //msdn.microsoft.com/en-us/library/windows/apps/hh452684.aspx

我还假设您已经检查并确保您要访问的文件未标记系统或隐藏文件属性(根据错误消息)。

我们目前正在通过WCF Web Service访问文件共享来解决这个问题。 它远非理想,但它让我们得到了我们所需要的。

下面是管理从运行Windows 8.1 RT的WinRT设备(Microsoft Surface RT)将文件写入网络共享的代码。 基本要点是:

  • 使用UNC路径访问共享
  • 分享是公开的
  • application manifest提到了写入的文件类型
  • 应用程序具有适当的function

这里描述了基本机制: http : //msdn.microsoft.com/en-US/library/windows/apps/hh967755.aspx

作为奖励,这显示了如何在WinRT应用程序中捕获输出到标准输出。

代码:

 #include "App.xaml.h" #include "MainPage.xaml.h" #include  using namespace TestApp; using namespace Platform; using namespace Windows::UI::Xaml::Navigation; using namespace Concurrency; // Anything that is written to standard output in this function will be saved to a file on a network share int MAIN(int argc, char** argv) { printf("This is log output that is saved to a file on a network share!\n"); return 0; } static char buffer[1024*1024]; Windows::Foundation::IAsyncOperation^ MainPage::RunAsync() { return create_async([this]()->int { return MAIN(1, NULL); }); } using namespace concurrency; using namespace Platform; using namespace Windows::Storage; using namespace Windows::System; void MainPage::Run() { //capture stdout in buffer setvbuf(stdout, buffer, _IOFBF, sizeof(buffer)); task testTask(RunAsync()); testTask.then([this](int test_result) { size_t origsize = strlen(buffer) + 1; wchar_t* wcstring = new wchar_t[sizeof(buffer)* sizeof(wchar_t)]; size_t converted_chars = 0; mbstowcs_s(&converted_chars, wcstring, origsize, buffer, _TRUNCATE); String^ contents = ref new Platform::String(wcstring); delete [] wcstring; Platform::String^ Filename = "log_file.txt"; Platform::String^ FolderName = "\\\\MY-SHARE\\shared-folder"; create_task(Windows::Storage::StorageFolder::GetFolderFromPathAsync(FolderName)).then([this, Filename, contents](StorageFolder^ folder) { create_task(folder->CreateFileAsync(Filename, CreationCollisionOption::ReplaceExisting)).then([this, contents](StorageFile^ file) { create_task(FileIO::WriteTextAsync(file, contents)).then([this, file, contents](task task) { try { task.get(); OutputBox->Text = ref new Platform::String(L"File written successfully!"); } catch (COMException^ ex) { OutputBox->Text = ref new Platform::String(L"Error writing file!"); } }); }); }); }); } MainPage::MainPage() { InitializeComponent(); Run(); } 

清单:

     TestApp Someone Assets\StoreLogo.png TestApp   6.3 6.3                  Text file  .txt                 

看看这个问题: 在WinRT中访问网络共享路径

无法访问Win8 App中的共享位置。