locked
using Array to write lines with WriteLinesAsync ?

    Question

  • hi,

    currently i try to write to a file with WriteLinesAsync. Using WriteTextAsync works fine with code like this

     create_task(KnownFolders::DocumentsLibrary->CreateFileAsync("testfile", CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file)
     {
        String^ MySetting = "C:\\folder1";
        FileIO::WriteTextAsync(file, MySetting);
     });

    but when i try to use WriteLinesAsync and try to use fields of an Array as lines then it always crashs. Maybe ist just a simple error in my code. Here is the snippet i try get to work

     create_task(KnownFolders::DocumentsLibrary->CreateFileAsync("testfile", CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file)
         {
    Array<String^>^ lines = {"C:\\folder1", "D:\\folder2"};
    FileIO::WriteLinesAsync(file, lines);
    });

    I think ist the way that i referenced "lines" in wrong way. Can someone help me with the correct one please ? As a guy already wrote in the C# Forum section, there are only 5 hits in Google and they all describe WriteTextAsync perfectly but... none does to WriteLinesAsync. Ist also not in the Windows 8 File Access Sample

    i would be glad about any help

    wfr

    co0Kie

     
    Thursday, September 13, 2012 9:34 PM

Answers

  • Hello,

    The WriteLinesAsync ABI need the parameters are IStorageFile^ and IIterable<String^>^.

    The Array<String ^> is not inherited from IIterable<String^>^, you should use Vector<String^>. Please follow these codes

    	String^ name=ref new String(L"testfile.txt");
    	create_task(KnownFolders::DocumentsLibrary->CreateFileAsync(name, CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file)
    	{
    		Vector<String^>^ lines=ref new Vector<String^>(3);
    		lines->Append(ref new String(L"C:\\folder1"));
    		lines->Append(ref new String(L"D:\\folder2"));
    		FileIO::WriteLinesAsync(file, lines);
    
    	});

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Francis Grave Monday, September 17, 2012 9:33 PM
    Monday, September 17, 2012 5:56 AM
  • Please follow these codes

    lines->Append(ref new String(L"C:\\folder1 ")+chkbAutoStart->IsChecked->ToString());

    Best regards,

    Jesse



    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Francis Grave Monday, September 17, 2012 9:33 PM
    Monday, September 17, 2012 12:14 PM

All replies

  • Hello,

    The WriteLinesAsync ABI need the parameters are IStorageFile^ and IIterable<String^>^.

    The Array<String ^> is not inherited from IIterable<String^>^, you should use Vector<String^>. Please follow these codes

    	String^ name=ref new String(L"testfile.txt");
    	create_task(KnownFolders::DocumentsLibrary->CreateFileAsync(name, CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file)
    	{
    		Vector<String^>^ lines=ref new Vector<String^>(3);
    		lines->Append(ref new String(L"C:\\folder1"));
    		lines->Append(ref new String(L"D:\\folder2"));
    		FileIO::WriteLinesAsync(file, lines);
    
    	});

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Francis Grave Monday, September 17, 2012 9:33 PM
    Monday, September 17, 2012 5:56 AM
  • Thank you very much Jesse,

    how is it when i would want to use something like this

    lines->Append(ref new String(L"Start With Windows  = " + chkbAutoStart->IsChecked->ToString()));

    it says error

    error C2664: 'Platform::String::String(const wchar_t *)

    conversion from String^´to wchar_t* not possible

    i tryed using without L and with different approaches of different string and charactar types ( WCHAR, TCHAR, LPTSTR, LPCTSTR ), but all end in same result. How can i use references in this Scenario ?


    Monday, September 17, 2012 11:55 AM
  • Please follow these codes

    lines->Append(ref new String(L"C:\\folder1 ")+chkbAutoStart->IsChecked->ToString());

    Best regards,

    Jesse



    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Francis Grave Monday, September 17, 2012 9:33 PM
    Monday, September 17, 2012 12:14 PM
  • Ouch :)

    What a guy told me once about old C# programmers going C++ and having Problems with the simplest things sometimes... ok its C++/CX here now but...

    I  did C++ a bit but it was somewhat 5-10 years ago, will watch not to post and ask much on any little problem. Still got there another tiny one with VirtualKey but will do another question post.

    marked as answer

    file closed

    thx

    Monday, September 17, 2012 9:33 PM