Windows Form to open folder

closed account (Gy7oizwU)
Hi guys

Im pretty new to the GUI side of C++ programming. All i want to do is open a folder location in windows explorer when clicking on the button in the form?

I have three buttons, the first opens the dialogue "Hello World" and the third closes the app. Not sure how to open a folder for the second button? Eg - i want to open C:/windows

1
2
3
4
5
6
7
8
9
10
11
12
		private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 	
					MessageBox::Show("Hello, World!");

			 }
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
				 Application::Exit();
			 }
	private: System::Void button2_Click_1(System::Object^  sender, System::EventArgs^  e) {
			 };


Thanks
I'm not sure how to do it with .net, but I am pretty sure you can use ShellExecute in C++ to open a directory.
Last edited on
closed account (Gy7oizwU)
Sorry for the late reply.

So i tried to use ShellExecute:

ShellExecute(NULL,"open","C:/test.txt",NULL,NULL,SW_SHOWNORMAL);

But then i get the following error:

error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
ShellExecuteW is the unicode Version of the function, to use it you will need
to prefix the strings with 'L'

eg: L"open" and L"C:/text.txt"

or specify the Ansi function by using ShellExecuteA(...)
closed account (Gy7oizwU)
I have tried that before as well. I then get the below error:

1>Form Test.obj : error LNK2028: unresolved token (0A000015) "extern "C" struct HINSTANCE__ * __stdcall ShellExecuteW(struct HWND__ *,wchar_t const *,wchar_t const *,wchar_t const *,wchar_t const *,int)" (?ShellExecuteW@@$$J224YGPAUHINSTANCE__@@PAUHWND__@@PB_W111H@Z) referenced in function "private: void __clrcall FormTest::Form1::button2_Click_1(class System::Object ^,class System::EventArgs ^)" (?button2_Click_1@Form1@FormTest@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Form Test.obj : error LNK2019: unresolved external symbol "extern "C" struct HINSTANCE__ * __stdcall ShellExecuteW(struct HWND__ *,wchar_t const *,wchar_t const *,wchar_t const *,wchar_t const *,int)" (?ShellExecuteW@@$$J224YGPAUHINSTANCE__@@PAUHWND__@@PB_W111H@Z) referenced in function "private: void __clrcall FormTest::Form1::button2_Click_1(class System::Object ^,class System::EventArgs ^)" (?button2_Click_1@Form1@FormTest@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
closed account (Gy7oizwU)
Ok, so i managed to prevent the error. And i can open a folder or file on my PC. But when i try to open a folder on a network, the button does nothing but the program compiles fine.

ShellExecute(NULL,L"open",L"\\testpc",NULL,NULL,SW_SHOWNORMAL);
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace System;
using namespace System::Diagnostics;
//...
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
    Process  ^p;
    ProcessStartInfo ^pInfo;

    pInfo = gcnew ProcessStartInfo("explorer.exe");
    pInfo->Arguments = "c:\\windows\\.";

    p = Process::Start(pInfo);			 
}
Topic archived. No new replies allowed.