CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    SetCurrentDirectory problem ..

    I am using CFileDialog to open different types of files in my application. I open file from c:\stdparamfile directory by File Menu
    Open menu item. (OnOpenDocument) . But when I change directory and open other type of file, that path is set for OnOpenDocument function. I need to select path c:\stdparamfile directory again.

    I am using GetCurrentDirectory , SetCurrentDirectory functions but problem exists.

    I set dialog path by dlg.m_ofn.lpstrInitialDir .

    How to get back to original directory ?

    pl help

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: SetCurrentDirectory problem ..

    If you want to keep the original current directory after CFileDialog::DoModal call, just set OFN_NOCHANGEDIR flag.
    Last edited by ovidiucucu; September 23rd, 2013 at 04:21 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: SetCurrentDirectory problem ..

    Thank you sir , I will ..

  4. #4
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: SetCurrentDirectory problem ..

    Sir , I used the flag as follows ..

    CFileDialog dlg (TRUE , _T("rec"), _T("*.rec"), OFN_HIDEREADONLY|OFN_NOCHANGEDIR, szFilters);

    It has no effect.

    It is changing directory of OnOpenDocument

    What to do ?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: SetCurrentDirectory problem ..

    Quote Originally Posted by new_2012 View Post
    I am using GetCurrentDirectory , SetCurrentDirectory functions but problem exists.

    How to get back to original directory ?

    pl help
    Why doesn't that work? Is the problem that you just want to get back to the original directory, or you don't wan the user to be able to change directory? OFN_NOCHANGEDIR will allow the user to change directory, but it should restore your app's working directory to what it was before the dialog was opened.

  6. #6
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: SetCurrentDirectory problem ..

    When I start my application , it loads file with OnOpenDocument , where I used saved filename in registry. That file is from parameter directory.

    Now , when I record data , I use CFileDialog to open a file to save data. That file is in other directory . I call that record directory.
    At this time , dialog shows parameter directory .

    Now again , if I need to open another parameter file , the open menu item from file menu , shows record data directory. I need to change directory again.

    To avoid this .. I tried ..


    save current directory.

    open record file .

    restore current directory.

    save data.

    close record file.


    but SetCurrentDirectory , GetCurrentDirectory functions didn't work. I am not able to check directory name by GetCurrentDirectory as it is TCHAR.

    I set OFN_NOCHANGEDIR flag also.

    pl guide

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: SetCurrentDirectory problem ..

    OFN_NOCHANGEDIR does not effect GetOpenFileName.

    In some cases, setting the CFileDialog's m_ofn.lpstrInitialDir works.

    You might be able to use a callback function similar to:

    http://forums.codeguru.com/showthrea...61#post2102361
    Last edited by Philip Nicoletti; September 23rd, 2013 at 07:47 AM.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: SetCurrentDirectory problem ..

    Quote Originally Posted by new_2012 View Post
    When I start my application , it loads file with OnOpenDocument , where I used saved filename in registry. That file is from parameter directory.

    Now , when I record data , I use CFileDialog to open a file to save data. That file is in other directory . I call that record directory.
    At this time , dialog shows parameter directory .

    Now again , if I need to open another parameter file , the open menu item from file menu , shows record data directory. I need to change directory again.

    To avoid this .. I tried ..


    save current directory.

    open record file .

    restore current directory.

    save data.

    close record file.


    but SetCurrentDirectory , GetCurrentDirectory functions didn't work. I am not able to check directory name by GetCurrentDirectory as it is TCHAR.

    I set OFN_NOCHANGEDIR flag also.

    pl guide
    Get and Set CurrentDirectory functions do work. You need to be more specific when you say the didn't. Maybe show some code.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: SetCurrentDirectory problem ..

    Are you checking the return value from these functions for success/failure? Is your application multi-threaded as multithreaded applications and shared library code should not use these functions and should avoid using relative path names.

    See
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    As GCDEF suggested, it would be more helpful if you posted the code with which you are having the problems.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: SetCurrentDirectory problem ..

    sir ,

    All the return values are correct and I have checked them.

    When my application starts .. the code executed is

    CString str, buf , currpath;
    TCHAR filename[1024];

    _tgetcwd( filename, _MAX_PATH);
    currpath = filename;

    str = GetProfileString(_T("History"), _T("Filename"), 0);

    _tfinddata_t fr;

    TCHAR* mystr = (LPTSTR)(LPCTSTR) str;

    if (_tfindfirst(mystr, &fr) != -1)
    CDocument * pdc = OpenDocumentFile(str); // opens last saved Parameter File from Parameter Directory
    else {
    str = currpath + _T("\\std.mts\\default.pam"); // opens default file from default Parameter Directory
    pDoc->OnSaveDocument(str);
    }

    Normally the user changes Parameter file if needed and saves back to disk.

    Then he needs to record the file data into record directory.

    In Record Dialog , the code executed is .....

    CFileDialog dlg (TRUE , _T("rec"),_T("*.rec"),OFN_HIDEREADONLY|OFN_NOCHANGEDIR,szFilters);

    dwRet = GetCurrentDirectoryW(_MAX_PATH ,cdir);

    if ((dlg.DoModal()) == IDOK) { // Select Record Directory to save record file
    code to save recorded data;
    }

    _tchdir(cdir); // this is additional I have tried

    qb = SetCurrentDirectoryW(cdir);

    Now problem is ... The Current Directory before CFileDialog is "c:\\mtsex_card\\std.mts"
    In Record File Dialog , I change it to c:\\ResPathTry .

    Now even after executing SetCurrentDirectoryW(cdir) , the OpenFileDialog
    shows directory c:\\ResPathTry.

    Then I added ..

    dwRet = GetCurrentDirectoryW(_MAX_PATH ,cdir);

    and return value in dwRet was 21 which corrosponds to "c:\\mtsex_card\\std.mts"


    The path may be set properly but CFileDialog from OpenDocument is not getting it.




    I can save paths to registry and set the path to CFileDialog using dlg.m_ofn.lpstrInitialDir
    feature but I am not able to use this feature in OnOpenDocument ... ???

    So what Can I do ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured