Windows 8 Metro App File Share Access

前端 未结 4 1412
一整个雨季
一整个雨季 2021-01-06 03:59

I am developing a Windows 8 Metro app, and we intend to deploy it to only a few tablets within our company. It\'s not meant for the Windows Store.

We need the app to

4条回答
  •  广开言路
    2021-01-06 04:20

    Below is the code that manages to write a file to a network share from WinRT device (Microsoft Surface RT) running Windows 8.1 RT. The essential bits are that:

    • share is accessed using a UNC path
    • share is public
    • application manifest mentions the file type that is written
    • application has the proper capabilities

    The basic mechanism is described here: http://msdn.microsoft.com/en-US/library/windows/apps/hh967755.aspx

    As a bonus, this shows how to capture output to standard output in a WinRT application.

    The code:

    #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();
    }
    

    The manifest:

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

提交回复
热议问题