Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make an openfile dialog without filters or anything (only choosing the file) and writes the director of the file into a textbox or string
Am on VC++ 2010
thanks in advance
Posted

MIDL
openFileDialog1->ShowDialog();
textBox1->Text = openFileDialog1->FileName;
 
Share this answer
 
While that example most likely will work fine, it requires .NET and the question was about C++.
If you wouldn't want to use .NET, you could use something like this:

BOOL GetFileName(char *filename,int maxlen)
{	OPENFILENAME ofn = {0};

	ofn.lStructSize  = sizeof(ofn);
	ofn.Flags        = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
	ofn.hInstance    = GetModuleHandle(0);
	ofn.lpstrFile    = filename;
	ofn.nMaxFile     = maxlen;
	ofn.nFilterIndex = 1;
	ofn.lpstrFilter	 = "Any file\0*.*\0\0";
	ofn.lpstrDefExt	 = "*";
	return GetOpenFileName(&ofn);
}

char filename[MAX_PATH];
if(!GetFileName(filename,MAX_PATH))
	MessageBox(0,"Canceled by user."," ",0);
else{
	// Do what you want to do with your file
}


Now to extract the path from filename, you could for example use PathRemoveFileSpec[^].
If getting a directory is all you want to do, i.e. if you're not interested in the name of the file, you could also select a directory right away with SHBrowseForFolder[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900