下面是管理从运行 Windows 8.1 RT 的 WinRT 设备 (Microsoft Surface RT) 将文件写入网络共享的代码。基本的位是:
- 使用 UNC 路径访问共享
- 共享是公开的
- 应用程序清单提到了写入的文件类型
- 应用程序具有适当的功能
这里描述了基本机制:http:
//msdn.microsoft.com/en-US/library/windows/apps/hh967755.aspx
作为奖励,这显示了如何在 WinRT 应用程序中将输出捕获到标准输出。
编码:
#include "App.xaml.h"
#include "MainPage.xaml.h"
#include <ppltasks.h>
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<int>^ 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<int> 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<void> 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();
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest">
<Identity Name="6f9dc943-75a5-4195-8a7f-9268fda4e548" Publisher="CN=Someone" Version="1.1.0.1" />
<Properties>
<DisplayName>TestApp</DisplayName>
<PublisherDisplayName>Someone</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
<Description>TestApp</Description>
</Properties>
<Prerequisites>
<OSMinVersion>6.3</OSMinVersion>
<OSMaxVersionTested>6.3</OSMaxVersionTested>
</Prerequisites>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="TestApp.App">
<m2:VisualElements DisplayName="TestApp" Description="TestApp" BackgroundColor="#222222" ForegroundText="light" Square150x150Logo="Assets\Logo.png" Square30x30Logo="Assets\SmallLogo.png">
<m2:DefaultTile>
<m2:ShowNameOnTiles>
<m2:ShowOn Tile="square150x150Logo" />
</m2:ShowNameOnTiles>
</m2:DefaultTile>
<m2:SplashScreen Image="Assets\SplashScreen.png" />
</m2:VisualElements>
<Extensions>
<Extension Category="windows.fileTypeAssociation">
<FileTypeAssociation Name="text">
<DisplayName>Text file</DisplayName>
<SupportedFileTypes>
<FileType ContentType="text/plain">.txt</FileType>
</SupportedFileTypes>
</FileTypeAssociation>
</Extension>
</Extensions>
</Application>
</Applications>
<Capabilities>
<Capability Name="musicLibrary" />
<Capability Name="picturesLibrary" />
<Capability Name="videosLibrary" />
<Capability Name="internetClient" />
<Capability Name="internetClientServer" />
<Capability Name="enterpriseAuthentication" />
<Capability Name="privateNetworkClientServer" />
</Capabilities>
</Package>