Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get windows directory?

How to get windows directory?

Scheduled Pinned Locked Moved C#
csharpc++tutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I need to read a .ini file under windows directory. But the name of the directory may be different on different machines. In some machine it is winnt while on ohters it is windows. So, how to get the name of the directory. I used GetPrivateProfileString with VC++. and I tried the whole afternoon to get it worked under C# but it just cannot return the string. the declaration of GetPrivateProfileString in VC++ is: DWORD GetPrivateProfileString( LPCTSTR lpAppName, // section name LPCTSTR lpKeyName, // key name LPCTSTR lpDefault, // default string LPTSTR lpReturnedString, // destination buffer DWORD nSize, // size of destination buffer LPCTSTR lpFileName // initialization file name ); And I converted it to static extern int GetPrivateProfileString( string appName, string keyName, sttring default, ref string strReturn, int size, string fileName); But I got System.NullReferenceException. How to solve these two problems or any one of them?

    N 1 Reply Last reply
    0
    • L Lost User

      I need to read a .ini file under windows directory. But the name of the directory may be different on different machines. In some machine it is winnt while on ohters it is windows. So, how to get the name of the directory. I used GetPrivateProfileString with VC++. and I tried the whole afternoon to get it worked under C# but it just cannot return the string. the declaration of GetPrivateProfileString in VC++ is: DWORD GetPrivateProfileString( LPCTSTR lpAppName, // section name LPCTSTR lpKeyName, // key name LPCTSTR lpDefault, // default string LPTSTR lpReturnedString, // destination buffer DWORD nSize, // size of destination buffer LPCTSTR lpFileName // initialization file name ); And I converted it to static extern int GetPrivateProfileString( string appName, string keyName, sttring default, ref string strReturn, int size, string fileName); But I got System.NullReferenceException. How to solve these two problems or any one of them?

      N Offline
      N Offline
      Navin
      wrote on last edited by
      #2

      I don't know a lot about C#, but I do know there is a Windows API to get this directory: UINT GetWindowsDirectory(LPTSTR lpBuffer, UINT uSize); Look it up in MSDN for details. The early bird may get the worm, but the second mouse gets the cheese.

      L 1 Reply Last reply
      0
      • N Navin

        I don't know a lot about C#, but I do know there is a Windows API to get this directory: UINT GetWindowsDirectory(LPTSTR lpBuffer, UINT uSize); Look it up in MSDN for details. The early bird may get the worm, but the second mouse gets the cheese.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        If I declare it as static extern int GetWindowsDirectory(ref string strDir, int size); and call it as string strDir = ""; GetWindowsDirectory(ref strDir, 20); I still got the NullReferenceException. The point is how to get the value back from the function.

        J 1 Reply Last reply
        0
        • L Lost User

          If I declare it as static extern int GetWindowsDirectory(ref string strDir, int size); and call it as string strDir = ""; GetWindowsDirectory(ref strDir, 20); I still got the NullReferenceException. The point is how to get the value back from the function.

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          You need to use the StringBuilder class instead of string. StringBuilder strDir = new StringBuilder(20); GetWindowsDirectory(ref strDir, 20); James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups