locked
Making a Relative File Path Issue RRS feed

  • Question

  • So I'm creating a program that will save a text file to the desktop. In Visual Basic the code would be like this for the file path:

    "C:\Users\" + Environment.UserName() + "\Desktop\" + TextBox1.Text + ".txt"

     

    However, I need to convert this to Visual C++ coding.

    WHat I have so far is:

    "C:\\Users\\" /* I need a function to find the users' name here */ "\\Desktop\\" /*Not sure how to put textbox string into file path*/ ".txt"

     

    I'm new to Visual C++.

     

    Wednesday, July 27, 2011 4:12 PM

Answers

  • On 7/27/2011 12:12 PM, Intense Tactics wrote:

    So I'm creating a program that will save a text file to the desktop. In Visual Basic the code would be like this for the file path:
    "C:\Users\" + Environment.UserName() + "\Desktop\" + TextBox1.Text + ".txt"

    Don't do it this way. The exact path differs with the operating system version and language. Use SHGetSpecialFolderPath:

    TCHAR path[MAX_PATH];
    SHGetSpecialFolderPath(NULL, path, CSIDL_DESKTOPDIRECTORY, FALSE);
    PathAppend(path, _T("filename.txt"));

    Igor Tandetnik

    • Proposed as answer by Josh Poley Wednesday, July 27, 2011 4:35 PM
    • Marked as answer by Rob Pan Thursday, August 4, 2011 9:44 AM
    Wednesday, July 27, 2011 4:22 PM

All replies

  • It depends on whether you are using Win32 API programming, or MFC, or .NET/WinForms. 
    Wednesday, July 27, 2011 4:21 PM
  • On 7/27/2011 12:12 PM, Intense Tactics wrote:

    So I'm creating a program that will save a text file to the desktop. In Visual Basic the code would be like this for the file path:
    "C:\Users\" + Environment.UserName() + "\Desktop\" + TextBox1.Text + ".txt"

    Don't do it this way. The exact path differs with the operating system version and language. Use SHGetSpecialFolderPath:

    TCHAR path[MAX_PATH];
    SHGetSpecialFolderPath(NULL, path, CSIDL_DESKTOPDIRECTORY, FALSE);
    PathAppend(path, _T("filename.txt"));

    Igor Tandetnik

    • Proposed as answer by Josh Poley Wednesday, July 27, 2011 4:35 PM
    • Marked as answer by Rob Pan Thursday, August 4, 2011 9:44 AM
    Wednesday, July 27, 2011 4:22 PM
  • FYI, it is a very bad thing to try to build the user profile's path yourself like that.  For example, I have several folders redirected, including my desktop, and that means my desktop is actually in a shared folder in my home server.  Your logic will completely fail in my PC and other PC's with similar setups.  Instead ask the Operating System for special folders ALWAYS.  Never try to guess it yourself.

    Use SHGetKnownFolderPath() with the FOLDERID_Desktop folder ID to determine the user's desktop path.  See http://msdn.microsoft.com/en-us/library/bb762188(VS.85).aspx.


    MCP
    Wednesday, July 27, 2011 4:29 PM
  • On 7/27/2011 12:12 PM, Intense Tactics wrote:

    So I'm creating a program that will save a text file to the desktop. In Visual Basic the code would be like this for the file path:
    "C:\Users\" + Environment.UserName() + "\Desktop\" + TextBox1.Text + ".txt"

    Don't do it this way. The exact path differs with the operating system version and language. Use SHGetSpecialFolderPath:

    TCHAR path[MAX_PATH];
    SHGetSpecialFolderPath(NULL, path, CSIDL_DESKTOPDIRECTORY, FALSE);
    PathAppend(path, _T("filename.txt"));

    Igor Tandetnik

    I'm using a StreamWriter, and when I tried this I got over 15 errors...

    I'm using a windows application if that helps any? With a Form1.cpp... Sorry I'm not sure if that's what you mean

     

    Wednesday, July 27, 2011 4:29 PM
  • FYI, the functions SHGetFolderPath() and SHGetSpecialFolderPath() (which is a subset of the former), seem to be deprecated because now Microsoft likes the use of folder ID's better than CSIDL's.  As of Windows Vista, SHGetFolderPath(), which superseded SHGetSpecialFolderPath() since Windows 2000, is now a wrapper caller for SHGetKnownFolderPath().
    MCP
    Wednesday, July 27, 2011 4:44 PM
  • On 7/27/2011 12:29 PM, Intense Tactics wrote:

    I'm using a windows application if that helps any? With a Form1.cpp... Sorry I'm not sure if that's what you mean

    You seem to be using C++/CLI. In this case, you are probably looking for Environment::GetFolderPath

    http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx


    Igor Tandetnik

    Wednesday, July 27, 2011 4:46 PM
  • On 7/27/2011 12:29 PM, Intense Tactics wrote:

    I'm using a windows application if that helps any? With a Form1.cpp... Sorry I'm not sure if that's what you mean

    You seem to be using C++/CLI. In this case, you are probably looking for Environment::GetFolderPath

    http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx


    Igor Tandetnik

    This was the most helpful...but I still don't understand how to make this work with a StreamWriter. The issue I'm having now is that I have the path, but I can't add a file extension to it like "test.txt".
    Wednesday, July 27, 2011 5:03 PM
  • Use what you get from Environment::GetFolderPath with System::IO::Path::Combine().
    MCP
    Wednesday, July 27, 2011 5:08 PM
  • On 7/27/2011 1:03 PM, Intense Tactics wrote:

    This was the most helpful...but I still don't understand how to make
    this work with a StreamWriter. The issue I'm having now is that I
    have the path, but I can't add a file extension to it like
    "test.txt".

    What's stopping you? Something as simple as path += "test.txt";  should work.


    Igor Tandetnik

    Wednesday, July 27, 2011 5:09 PM
  • I get this error:

    syntax error : missing ';' before '+='

     

    When i do

     

    static String^ path = Environment::GetFolderPath( Environment::SpecialFolder::Desktop );

    path += "\\test.txt";

    Wednesday, July 27, 2011 5:25 PM
  • On 7/27/2011 1:25 PM, Intense Tactics wrote:

    I get this error:

    syntax error : missing ';' before '+='

    When i do

    static String^ path = Environment::GetFolderPath( Environment::SpecialFolder::Desktop );

    path += "\\test.txt";

    Where are you doing this? Is it outside of any function body, by any chance?


    Igor Tandetnik

    Wednesday, July 27, 2011 6:05 PM
  • I would do:

    static String^ path = System::IO::Path::Combine(
      Environment::GetFolderPath(Environment::SpecialFolder::Desktop)
      , "test.txt"
      );
    
    



    MCP
    Wednesday, July 27, 2011 6:39 PM