CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Posts
    19

    Getting and Setting current Directory

    void main() {
    char olddir[MAX_PATH];
    if (GetCurrentDirectory(MAX_PATH, olddir) == 0) {
    errormessage();
    //exit(1);
    }
    MessageBox(NULL,olddir,L"File",MB_OK);
    _tprintf(TEXT("Current Directory %s\n"),olddir);
    if(SetCurrentDirectory("C:\\Documents and settings") == 0)
    errormessage();
    if (GetCurrentDirectory(MAX_PATH, olddir) == 0) {
    errormessage();
    }
    _tprintf(TEXT("Current Directory %s\n"),olddir);
    getch();
    }

    I have been using this code to get and set my current directory but when setting the directory it always displays the error "The System can't find the file specified"

    while getting the file it displays this as the current directory
    Current Directory c:\Documents and Settings\Administrator\My Documents\Visual St
    udio 2005\Projects\Scorta\TEST

  2. #2
    Join Date
    Aug 2008
    Location
    Germany / NRW
    Posts
    37

    Re: Getting and Setting current Directory

    I does not look wrong actually. I assume your "errormessage()" prints
    the GetLastError() result.

    You should try to add a backslash "\\" as last character even if SetCurrentDirectory should do
    that for you.
    If that does not work, check if you have enough permissions on the directory.

  3. #3
    Join Date
    Dec 2008
    Posts
    19

    Re: Getting and Setting current Directory

    thankx a lot its done
    actually i changed the line
    if(SetCurrentDirectory("C:\\Documents and settings") == 0)
    to
    if(SetCurrentDirectory(L"C:\\Documents and settings") == 0)
    converting intowide character.

    I don't have any idea why it worked can you explain it
    thnx in advance

  4. #4
    Join Date
    May 2002
    Posts
    1,435

    Re: Getting and Setting current Directory

    Your original code never would have compiled because of the mix of character types. Therefore, you never would have got an error message. So why did you post the question?

  5. #5
    Join Date
    Dec 2008
    Posts
    19

    Re: Getting and Setting current Directory

    Quote Originally Posted by 0xC0000005 View Post
    Your original code never would have compiled because of the mix of character types. Therefore, you never would have got an error message. So why did you post the question?
    The code compiled on visual studio 2005 but I don't know Why ?

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