[wxMSW][wx3.0.2] Unable to get file name on watch directory notification Topic is solved

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.
Post Reply
Rudra
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 224
Joined: Fri Sep 13, 2013 2:59 pm

[wxMSW][wx3.0.2] Unable to get file name on watch directory notification

Post by Rudra »

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
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: [wxMSW][wx3.0.2] Unable to get file name on watch directory notification

Post by PB »

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?
User avatar
T-Rex
Moderator
Moderator
Posts: 1249
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: [wxMSW][wx3.0.2] Unable to get file name on watch directory notification

Post by T-Rex »

You probably need to add the directory for watching via AddTree() call.
Rudra
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 224
Joined: Fri Sep 13, 2013 2:59 pm

Re: [wxMSW][wx3.0.2] Unable to get file name on watch directory notification

Post by Rudra »

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
Filthy Rich wx Solver
Posts: 224
Joined: Fri Sep 13, 2013 2:59 pm

Re: [wxMSW][wx3.0.2] Unable to get file name on watch directory notification

Post by Rudra »

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
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: [wxMSW][wx3.0.2] Unable to get file name on watch directory notification

Post by PB »

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?
User avatar
T-Rex
Moderator
Moderator
Posts: 1249
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: [wxMSW][wx3.0.2] Unable to get file name on watch directory notification

Post by T-Rex »

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
Filthy Rich wx Solver
Posts: 224
Joined: Fri Sep 13, 2013 2:59 pm

Re: [wxMSW][wx3.0.2] Unable to get file name on watch directory notification

Post by Rudra »

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.
Post Reply