CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    13

    [RESOLVED] Get path from openFileDialog

    I´m writing a windows form application.

    Consider this code:

    if(openFileDialog1->ShowDialog() == System::Windows::Forms:ialogResult::OK)

    {

    System::IO::Path::GetDirectoryName(openFileDialog1->FileName);
    MessageBox::Show(openFileDialog1->FileName, "Directory");

    }

    How can i store/write the result presenting in MessageBox to a file called "c:\path.txt",
    instead of presenting the result in the MessageBox?

    I would be eternally grateful for your help!

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Get path from openFileDialog

    Assign the result to a string var rather than showing in a message box.
    Create and open a file object.
    Write the contents of the string var to the file.
    Close the file object.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    13

    Re: Get path from openFileDialog

    How do i do that?

    This is what i was trying to do:
    What is wrong with my code?

    if(openFileDialog1->ShowDialog() == System::Windows::Forms:ialogResult::OK)
    {

    String^ path = System::IO::Path::GetDirectoryName(openFileDialog1->FileName);

    fstream File;

    File.open("c:\\path.txt", ios:ut);

    if (File.is_open ())
    {
    for (int i = 0; i <= 50; i++)
    File<< path[i] << endl;
    }

    File.close();

    }


    I get the error:

    An unhandled exception of type 'System.IndexOutOfRangeException' occurred in myapp.exe
    Additional information: Index out of limits for the matrix

  4. #4
    Join Date
    Jun 2011
    Location
    Sweden
    Posts
    13

    Re: Get path from openFileDialog

    Very well.
    Problem is solved.

    I just have do do this:

    System::IO::File::WriteAllText("c:\\path.txt", openFileDialog1->FileName);

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Get path from openFileDialog

    Quote Originally Posted by alderaan View Post
    What is wrong with my code?
    Code:
     
    for (int i = 0; i <= 50; i++)
    File<< path[i] << endl;
    Your path var is a string yet you are trying to write an array of 50 elements in your loop which of course results in index out of range.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured