I have a dropdown combo box that scans a directory for all XML files and adds it to the list so I can select it from the drop down. This functionality works great.
The problem I am running in to is if a user makes any changes to the xml file during run time.
For example... Adding an xml to the path will automatically update the dropdown and all items, however, if a user deleted an xml file, the dropdown doesn't delete the string from the list autmatically.
I was able to get it to work "partially", but it only deletes after you try selecting the item. I would like for it to be removed without any selection required.
Here is my code. Any suggestions please?
here is the portion I'm working with
x = SendMessage(GetDlgItem(hWnd, IDC_XML), CB_GETCURSEL, NULL, NULL);
if(x != CB_ERR)
{
SendMessage(GetDlgItem(hWnd, IDC_XML), CB_GETLBTEXT, (WPARAM)x, (LPARAM)strBuffer0);
sprintf_s(removeNonExistantValue, "c:\\windows\\system32\\sysprep\\%s", strBuffer0);
if(!file_exist(removeNonExistantValue))
{
//MessageBox(0, test, "Test1", MB_OK);
SendMessage(loadXML, CB_DELETESTRING, NULL, (LPARAM)strBuffer0);
}
}
Here is the full source for that section of code
#include <string>
#include <sys/stat.h>
using namespace std;
//Disable unnecessary warnings
#pragma warning(disable: 4244)
//Global Variables
int x;
char strBuffer0[128] = "";
const char * myFileName;
char file [MAX_PATH] = {0};
char removeNonExistantValue[MAX_PATH];
struct file_data
{
std::wstring sLastAccessTime;
__int64 nFileSize;
};
int file_exist (char *filename)
{
struct stat buffer;
return (stat (filename, &buffer) == 0);
}
BOOL GetXML(HWND hwMain)
{
//const char * myFileName;
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
string FilePath = "c:\\windows\\system32\\sysprep\\*.*";
string FileName;
hFind = FindFirstFile( FilePath.c_str(), &FindFileData);
char errorCode[128];
//char file [MAX_PATH] = {0};
x = SendMessage(GetDlgItem(hWnd, IDC_XML), CB_GETCURSEL, NULL, NULL);
if(x != CB_ERR)
{
SendMessage(GetDlgItem(hWnd, IDC_XML), CB_GETLBTEXT, (WPARAM)x, (LPARAM)strBuffer0);
sprintf_s(removeNonExistantValue, "c:\\windows\\system32\\sysprep\\%s", strBuffer0);
if(!file_exist(removeNonExistantValue))
{
//MessageBox(0, test, "Test1", MB_OK);
SendMessage(loadXML, CB_DELETESTRING, NULL, (LPARAM)strBuffer0);
}
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
char * path = "c:\\windows\\system32\\sysprep\\";
wchar_t buff[128];
file_data filedata;
filedata.sLastAccessTime = buff;
filedata.nFileSize = (((__int64)FindFileData.nFileSizeHigh) << 32) + FindFileData.nFileSizeLow;
FileName = FindFileData.cFileName;
myFileName = FileName.c_str();
//Check if the file is an XML extension
if((FileName.substr(FileName.find_last_of(".") + 1) == "XML") || (FileName.substr(FileName.find_last_of(".") + 1) == "xml"))
{
FileName = FindFileData.cFileName;
// char file [MAX_PATH] = {0};
strcpy_s(file,path);
strcat_s(file,myFileName);
//MessageBox(0, myFileName, "Info2", MB_OK);
//"file" includes the whole path and filename. If we will be doing anything with the file, we need that variable.
//For displaying purposes, we don't need to display the entire path, so lets just display the filename
if(SendMessage(loadXML, CB_FINDSTRING, (WPARAM)-1, (LPARAM)myFileName) == CB_ERR)
{
SendMessage(loadXML, CB_ADDSTRING, NULL, (LPARAM)myFileName);
}
}
}
dwError = GetLastError();
FindClose(hFind);
if(dwError != ERROR_NO_MORE_FILES)
{
sprintf_s(errorCode,"ERROR: %s",dwError);
MessageBox(0, errorCode, "Error", MB_ICONWARNING);
return FALSE;
}
return TRUE;
}