If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Rudra
Filthy Rich wx Solver
Posts: 224 Joined: Fri Sep 13, 2013 2:59 pm
Post
by Rudra » Mon Jul 02, 2018 8:02 am
HI,
In my wx application, using wxFileSystemWatcher, I am watching a directory for file being added to it. I get notification when a file is added but I can't get it name/path. The event.GetPath().GetFullPath() gives the watching directory path. My code is as follows,
Code: Select all
bool CUFrame::CreateFSWatcher()
{
if(m_watcher)
return false;
m_watcher = new wxFileSystemWatcher();
m_watcher->SetOwner(this);
this->Connect(wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler(CUFrame::OnFileSystemEvent));
return true;
}
bool CUFrame::AddFSEntry(const wxString& dirPath)
{
if(m_watcher == NULL)
return false;
if(wxDirExists(dirPath) == false)
return false;
return m_watcher->Add(dirPath, wxFSW_EVENT_CREATE);
}
void CUFrame::OnFileSystemEvent(wxFileSystemWatcherEvent& event)
{
std::string path = event.GetPath().GetFullPath().ToStdString();
}
Please suggest.
Thanks,
R.
PB
Part Of The Furniture
Posts: 4204 Joined: Sun Jan 03, 2010 5:45 pm
Post
by PB » Mon Jul 02, 2018 9:23 am
I would have thought you may get an error or warning but as you do not connect events with those flags (IMO you certainly should) I have no clue.
Did you check the string provided by event.ToString() to see if there's anything useful?
T-Rex
Moderator
Posts: 1249 Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:
Post
by T-Rex » Mon Jul 02, 2018 10:35 am
You probably need to add the directory for watching via
AddTree() call.
Rudra
Filthy Rich wx Solver
Posts: 224 Joined: Fri Sep 13, 2013 2:59 pm
Post
by Rudra » Mon Jul 02, 2018 10:42 am
PB wrote: Did you check the string provided by event.ToString() to see if there's anything useful?
Yes, it gives : FSW_EVT type=1 (CREATE) path='C:\Users\Rudresh Pandey\Documents\Rudresh\Send' which is the watching directory path.
Rudra
Filthy Rich wx Solver
Posts: 224 Joined: Fri Sep 13, 2013 2:59 pm
Post
by Rudra » Mon Jul 02, 2018 11:08 am
T-Rex wrote: You probably need to add the directory for watching via
AddTree() call.
I tried AddTree() with wxFSW_EVENT_ALL event and see same issue.
event.ToString() is as follows,
FSW_EVT type=1 (CREATE) path='C:\Users\Rudresh Pandey\Documents\Rudresh\Send'
FSW_EVT type=8 (MODIFY) path='C:\Users\Rudresh Pandey\Documents\Rudresh\Send'
FSW_EVT type=8 (MODIFY) path='C:\Users\Rudresh Pandey\Documents\Rudresh\Send'
PB
Part Of The Furniture
Posts: 4204 Joined: Sun Jan 03, 2010 5:45 pm
Post
by PB » Mon Jul 02, 2018 11:21 am
Just a wild guess: Is the folder in question a regular folder and not just a shortcut to an actual filesystem directory or something like that? I do not know if it is actually possible to do that though.
Can you reproduce the behaviour on a regular (c:\fstest\test") folder and test that folder of yours with the fswatcher sample?
T-Rex
Moderator
Posts: 1249 Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:
Post
by T-Rex » Tue Jul 03, 2018 7:32 am
Code: Select all
m_watcher = new wxFileSystemWatcher();
m_watcher->SetOwner(this);
m_watcher->AddTree(wxFileName(wxT("D:\\Projects\\wxTest")), wxFSW_EVENT_CREATE, wxT("*.*"));
Connect(wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler(wxTestApp::OnFS));
void wxTestApp::OnFS(wxFileSystemWatcherEvent & e)
{
wxMessageBox(e.GetPath().GetFullPath());
}
Works for me.
https://www.screencast.com/t/sbT7KJegmkL3
Rudra
Filthy Rich wx Solver
Posts: 224 Joined: Fri Sep 13, 2013 2:59 pm
Post
by Rudra » Fri Jul 06, 2018 5:49 am
T-Rex wrote: Code: Select all
m_watcher->AddTree(wxFileName(wxT("D:\\Projects\\wxTest")), wxFSW_EVENT_CREATE, wxT("*.*"));
Yes, the above code works for me as well. On comparing with my code, I found that the issue is in how I was adding the path.
Code: Select all
bool CUFrame::AddFSEntry(const wxString& dirPath)
{
if(m_watcher == NULL)
return false;
if(wxDirExists(dirPath) == false)
return false;
return m_watcher->Add(dirPath, wxFSW_EVENT_CREATE);
}
The Add() expects wxFileName but wxString was getting passed. Passing it as wxFileName fixes the issue for both AddTree and Add().
Thanks for the help.
R.