How to get windows directory?
-
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?
-
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?
-
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.
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.
-
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.
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