September 4th, 2011, 04:37 AM
#1
[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!
September 4th, 2011, 06:16 AM
#2
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.
September 5th, 2011, 03:39 PM
#3
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
September 5th, 2011, 03:59 PM
#4
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);
September 5th, 2011, 04:46 PM
#5
Re: Get path from openFileDialog
Originally Posted by
alderaan
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
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks