none
[UWP][C++]How to catch exception of StorageFile::GetFileFromPathAsync() ? RRS feed

答案

  • Great! It's almost all perfect! 

    Probably, the error is caused due to StorageFile's scope issue. GetImagePropertiesAsync() is executed actually in the line "props = t.get()".  However "file" is not captured in the lambda expression in which t.get() exists. Try appending "file" to [ ] in anyway.

    /*[]( task<FileProperties::ImageProperties^> t )*/ 
    
    [file]( task<FileProperties::ImageProperties^> t ) 


    • 已编辑 FEC-4RP 2017年1月17日 16:24
    • 已标记为答案 lc-soft 2017年1月18日 14:20
    2017年1月17日 15:32

全部回复

  • Hello lc-soft,

    Probably you could catch the error, as follows.

    	String^ path = "No_such_a_file.png";
    	
    	create_task(StorageFile::GetFileFromPathAsync(path)).then([](task<StorageFile^> fileTask)
    	{
    		try
    		{
    			StorageFile^ file = fileTask.get();
    			// TODO: Put code to handle the file when it is opened successfully.
    		}
    		catch (Exception^ e)
    		{
    
    		}
    	});

    2017年1月15日 8:57
  • I want to get the basic properties of the file, if it is an image, then get image size, my code is roughly the following, I feel this is not the best way to write, do you have suggestions for improvement? I want to do somethings after getting all properties of file, How can I modify the following code?

    int TestGetFile( const wchar_t *wpath )
    {
    	String ^path = ref new String( wpath );
    	create_task( StorageFile::GetFileFromPathAsync( path ) ).then( [](task<StorageFile^> fileTask) {
    		try {
    			StorageFile^ file = fileTask.get();
    			// TODO: Put code to handle the file when it is opened successfully.
    			create_task( file->GetBasicPropertiesAsync() ).then( [](FileProperties::BasicProperties ^props) {
    				// TODO: Put code to handle the properties when it is get successfully.
    			} );
    			if( /* this file is image */ ) {
    				create_task( file->Properties->GetImagePropertiesAsync() ).then( [](FileProperties::ImageProperties ^props) {
    					// TODO: Put code to handle the properties when it is get successfully.
    				} );
    			}
    		} catch( Exception^ e ) {
    			
    		}
    	} );
    	return 0;
    }


    • 已编辑 lc-soft 2017年1月15日 12:12 update
    2017年1月15日 12:00
  • Unfortunately,  I can't clearly answer how to write following tasks since I'm not an expert at all. But to my understanding, I think your code is written in a right manner. For details of Concurrency, please refer to documentation.

    Creating Asynchronous Operations in C++ for Windows Store Apps
    https://msdn.microsoft.com/en-us/library/hh750082.aspx

    Concurrency Runtime
    https://msdn.microsoft.com/en-us/library/dd504870.aspx

    2017年1月15日 15:52
  • I tried to modify the code to make it a continuous task, but there is a problem when it getting image properties will thrown a exception: "WinRT originate error". 

    Target file is an existing image file. this code can sometimes get to the image properties.

    static void TestGetFile( const wchar_t *wpath )
    {
    	String ^path = ref new String( wpath );
    	create_task( StorageFile::GetFileFromPathAsync( path ) ).then( []( task<StorageFile^> fileTask ) {
    		StorageFile^ file = nullptr;
    		try {
    			file = fileTask.get();
    		} catch( Exception^ e ) {
    			return file;
    		}
    		return file;
    	} ).then( [](StorageFile^ file) {
    		if( !file ) {
    			return create_task( []() -> int {
    				return -ENOENT;
    			} );
    		}
    		// TODO: Put code to handle the file when it is opened successfully.
    
    		auto t = create_task( file->GetBasicPropertiesAsync() ).then( []( FileProperties::BasicProperties ^props ) {
    			// TODO: Put code to handle the properties when it is get successfully.
    			printf( "mtime: %llu\n", props->DateModified.UniversalTime );
    			return 0;
    		} );
    		if( true/* this file is image */ ) {
    			t = t.then( [file](int ret) {
    				return file->Properties->GetImagePropertiesAsync();
    			} ).then( []( task<FileProperties::ImageProperties^> t ) {
    				FileProperties::ImageProperties ^props;
    				try {
    					props = t.get();
    					printf( "image size: %d,%d\n", props->Width, props->Height );
    				} catch( Exception^ e ) {
    
    				}
    				// TODO: Put code to handle the properties when it is get successfully.
    				return 0;
    			} );
    		}
    		return t;
    	} ).then( []( int ret ) {
    		printf( "return: %d\n", ret );
    	} );
    }





    • 已编辑 lc-soft 2017年1月17日 14:29 update
    2017年1月17日 13:50
  • Great! It's almost all perfect! 

    Probably, the error is caused due to StorageFile's scope issue. GetImagePropertiesAsync() is executed actually in the line "props = t.get()".  However "file" is not captured in the lambda expression in which t.get() exists. Try appending "file" to [ ] in anyway.

    /*[]( task<FileProperties::ImageProperties^> t )*/ 
    
    [file]( task<FileProperties::ImageProperties^> t ) 


    • 已编辑 FEC-4RP 2017年1月17日 16:24
    • 已标记为答案 lc-soft 2017年1月18日 14:20
    2017年1月17日 15:32
  • Good job! the code works fine after modification.
    2017年1月18日 14:20