locked
fopen file access is denied RRS feed

  • Question

  • hello ~

     i use fopen in metro . for using copy file to appdata folder from music Lib foder ( pick up the file

    by using filepicker).

    but occur the error (file access is denied) . is there solution to resolve this problem?

    is there any option or curse in using visual studio 2012 for windows 8 .

    please help me... 

    thanks.


    jang

    Wednesday, July 11, 2012 1:46 AM

Answers

  • That approach should work fine. This is a snippet that works for me (updated version of what Raman provided in an earlier post demonstrating this).

    	FileOpenPicker^ openPicker = ref new FileOpenPicker();
        openPicker->ViewMode = PickerViewMode::Thumbnail;
        openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
        openPicker->FileTypeFilter->Append(".jpg");
        openPicker->FileTypeFilter->Append(".jpeg");
        openPicker->FileTypeFilter->Append(".png");
    	
    	create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile ^file)
            {
                if (file)
    			{
    				OutputTextBlock->Text = "Picked photo: " + file->Name;
    				create_task(file->CopyAsync((Windows::Storage::ApplicationData::Current)->TemporaryFolder, file->Name, Windows::Storage::NameCollisionOption::ReplaceExisting)).then([this](StorageFile ^file2)
    					{
    						if (file2)
    							{
    								OutputTextBlock->Text = "Copied File to: " + file2->Path;
    								auto ls = file2->Path->Data();
    								FILE *pFile;
    								HRESULT hr = _wfopen_s(&pFile,ls,L"rb");
    								//fread_s code was here
    								fclose( pFile );
    							}
    					 });
    	        }
                else
                {
                    OutputTextBlock->Text = "Operation cancelled.";
                }
            });


    David Lamb

    • Proposed as answer by Helen Zhao Thursday, July 12, 2012 5:33 AM
    • Marked as answer by Jesse Jiang Tuesday, July 17, 2012 5:52 AM
    Thursday, July 12, 2012 12:13 AM
    Moderator
  • @Pierre: the book on C++ /CX is coming. please refer to the announcement @ http://sridharpoduri.com/2012/05/23/announcement-book-on-c-cx-and-metro-style-app-development/

    Thanks,

    Sridhar

    • Marked as answer by Jesse Jiang Tuesday, July 17, 2012 5:52 AM
    Friday, July 13, 2012 6:49 AM

All replies

  • Hi jang,

    Your app has direct access to the file system only in its install and application data directories. It doesn't have permission to read from the music library, so fopen won't work.

    To access the files elsewhere you need to use the StorageFile object returned from the file picker (or via contracts). This object provides brokered access to the file: the broker has the user's full access and can read any file the user has permissions to read. The broker uses this access on your app's behalf to provide a stream with the file's contents through the StorageFile object.

    --Rob

    • Proposed as answer by Helen Zhao Thursday, July 12, 2012 5:33 AM
    Wednesday, July 11, 2012 1:51 AM
    Moderator
  • is there any option or curse in using visual studio 2012 for windows 8 .

    This might help you: Charles Petzold is writing a new "Programming Windows 8" book in C#, but code samples are available in C++/CX as well. You can download the PDF version for $20 http://www.charlespetzold.com/blog/2012/05/Programming-Windows-6th-Edition-for-the-CPlusPlus-Programmer.html. The book is not finished yet, but I don't think there is a C++/CX book available today.

    Wednesday, July 11, 2012 2:04 AM
  • hi Rob 

    thanks for answer.

    yes i already konw if i want direct access to the file system i have to use install and application data 

    so i copy file from music library to application data. source is below.

                     

    FileOpenPicker openPicker =new FileOpenPicker();
               openPicker.ViewMode = PickerViewMode.List;
               openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
               openPicker.FileTypeFilter.Add(".log");
               openPicker.FileTypeFilter.Add(".mp3");
               openPicker.FileTypeFilter.Add(".dcf");
              StorageFile file = await openPicker.PickSingleFileAsync();
              if(null == file){


              }

    else{

                 StorageFile Tempfile = await file.CopyAsync(ApplicationData.Current.LocalFolder, file.Name, NameCollisionOption.ReplaceExisting);

    }

    and i use fopen by using Tempfile path . 

    isn't it right??



    jang

    Wednesday, July 11, 2012 2:10 AM
  • That approach should work fine. This is a snippet that works for me (updated version of what Raman provided in an earlier post demonstrating this).

    	FileOpenPicker^ openPicker = ref new FileOpenPicker();
        openPicker->ViewMode = PickerViewMode::Thumbnail;
        openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
        openPicker->FileTypeFilter->Append(".jpg");
        openPicker->FileTypeFilter->Append(".jpeg");
        openPicker->FileTypeFilter->Append(".png");
    	
    	create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile ^file)
            {
                if (file)
    			{
    				OutputTextBlock->Text = "Picked photo: " + file->Name;
    				create_task(file->CopyAsync((Windows::Storage::ApplicationData::Current)->TemporaryFolder, file->Name, Windows::Storage::NameCollisionOption::ReplaceExisting)).then([this](StorageFile ^file2)
    					{
    						if (file2)
    							{
    								OutputTextBlock->Text = "Copied File to: " + file2->Path;
    								auto ls = file2->Path->Data();
    								FILE *pFile;
    								HRESULT hr = _wfopen_s(&pFile,ls,L"rb");
    								//fread_s code was here
    								fclose( pFile );
    							}
    					 });
    	        }
                else
                {
                    OutputTextBlock->Text = "Operation cancelled.";
                }
            });


    David Lamb

    • Proposed as answer by Helen Zhao Thursday, July 12, 2012 5:33 AM
    • Marked as answer by Jesse Jiang Tuesday, July 17, 2012 5:52 AM
    Thursday, July 12, 2012 12:13 AM
    Moderator
  • @Pierre: the book on C++ /CX is coming. please refer to the announcement @ http://sridharpoduri.com/2012/05/23/announcement-book-on-c-cx-and-metro-style-app-development/

    Thanks,

    Sridhar

    • Marked as answer by Jesse Jiang Tuesday, July 17, 2012 5:52 AM
    Friday, July 13, 2012 6:49 AM