Hi - I'm trying to get the filename and path of a file I'm opening with the OpenFileDialog in Visual C++. I can open and display the contents of the file OK, but I'm having problems accessing the filename and path, I guess they're stored in a structure somewhere? Code so far below...Thanks in advance!

private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				if(openFileDialog1->ShowDialog() == DialogResult::OK)
				{
					String* get_FileNames();
					
					System::IO::StreamReader * sr = new
					System::IO::StreamReader(openFileDialog1->FileName);
					MessageBox::Show(sr->ReadToEnd());
					sr->Close();
				}

			 }

I've been working on that very thing the past few days -- learning Windows Forms. You can't call c++ new operator -- instead you have to use gcnew.

private: System::Void toolStripMenuOpen_Click(System::Object^  sender, System::EventArgs^  e) {

             if( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
             {
                try
                {
                    StreamReader^ myStream;
                    if(openFileDialog1->FileName == nullptr)
                        return;
                    if ((myStream = gcnew StreamReader(openFileDialog1->FileName)) != nullptr)
                    {

Thanks Ancient Dragon, I used this to display the file and path

System::IO::StreamReader(openFileDialog1->FileName);
MessageBox::Show(openFileDialog1->FileName);

Now today's task is to work out how I want to use it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.