locked
Calling CreateFileAsync() is fail because the file is being opened by other software. How to avoid this error ?

    Question

  • Help !

    I want to open a file but It was opened by other software.

    Therefore, When It calls CreateFileAsync(), My App has failed and then close.

    How to know this file is openning by other software?

    Let my app works well.

    Below my code about opening file function .

     create_task( KnownFolders::DocumentsLibrary->CreateFileAsync( XXXFILE , CreationCollisionOption::ReplaceExisting) ).then([ this ]( StorageFile^ file )
     



    • Edited by KC-Steven Friday, January 24, 2014 1:20 AM
    Friday, January 24, 2014 1:18 AM

All replies

  • Catch the exception, handle it.  Wait for other software to release the handle.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by KC-Steven Monday, January 27, 2014 7:27 AM
    • Unmarked as answer by KC-Steven Thursday, February 13, 2014 7:05 AM
    Friday, January 24, 2014 2:02 PM
    Moderator
  • I got it. thank you very much.

    Monday, January 27, 2014 12:54 AM
  • How do it add try..catch ?

    I always fail.

    Code like below

    try{
    create_task( ApplicationData::Current->LocalFolder->CreateFileAsync( SaveFile , CreationCollisionOption::ReplaceExisting ) ).then([ PDFFILE ,this ]( StorageFile^ file )
    {
    if( g_Report.m_nSize == 0 )
    cancel_current_task();		
    (*PDFFILE) = file;
    std::vector<byte> PDFByteData;	
    PDFByteData.clear();
    PDFByteData.reserve( g_Report.m_nSize );		
    
    Platform::Array<byte>^ managedBytes = ref new Platform::Array<byte>( g_Report.m_nSize );
    for( int i = 0 ; i < g_Report.m_nSize ; i++ )
    {
    managedBytes[i] = g_Report.m_Data[i];
    }
    InMemoryRandomAccessStream^ memoryStream = ref new InMemoryRandomAccessStream();
    DataWriter^  Datawriter = ref new DataWriter( memoryStream->GetOutputStreamAt(0) );
    Datawriter->WriteBytes( managedBytes );
    IBuffer^ Buffer = Datawriter->DetachBuffer();
    return create_task( FileIO::WriteBufferAsync( file, Buffer) );
    }).then( [ PDFFILE ,this]()
    {
    // Launch the retrieved file
    concurrency::task<bool> launchFileOperation( Windows::System::Launcher::LaunchFileAsync( (*PDFFILE) ) );
    launchFileOperation.then([]( bool success )
    {
    	if (success)
    	{
    	// File launched
    	}
    	else
    	{
    	// File launch failed
    	}
    });
    });
    }
    catch(Platform::Exception^ msg)
    {
    };

     

     

    Thursday, February 13, 2014 7:04 AM
  • Can you try leaving out the exception type so that all exceptions get caught? Do you every get to the catch clause? If not, you might be running into a native error. What's the exact exception?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 13, 2014 1:11 PM
    Moderator
  • First, Many thanks for your help.

    Exception is below:

    The XXXFILE has opened by other. ( I guess at the result. )

    If the XXXFILE has not opened by other, It will work well.

    ---------------------------------------------------------------------------------

    My C++/Cx is just so so.

    This is my first about using try..catch

    Could you tell me how to do it ?

    New clue:

    I find the file open by different program (like open file use note or work) maybe work well.

    I don't know how to find the cause of this problem now.



    • Edited by KC-Steven Friday, February 14, 2014 1:26 AM
    Friday, February 14, 2014 12:44 AM
  • What is the scenario here - why is the file being opened by other processes?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 20, 2014 4:52 PM
    Moderator
  • Hello! Sorry , I left for some time.

    In my app , Existing the funciotn is to open PDF file by Adobe Reader or others . The crash happended in working twice by Adobe Reader( open the same file ).

    But,It work well when the file was opend by this(). I don't known why, so I guess the error.



    • Edited by KC-Steven Tuesday, March 11, 2014 12:16 AM
    Tuesday, March 11, 2014 12:14 AM