.ini benutzen



  • Hallo,

    wie kann ich Funktionen wie WritePrivateProfileString auch unter .Net benutzen, konnte nach 1 Stunde googeln nichts finden.

    Mfg. AD12ka



  • Vielleicht hilft das weiter:

    ➡ http://msdn.microsoft.com/en-us/library/ms725501(VS.85).aspx
    (Müsste meines erachtens auch mit .NET funktionieren)

    Auszug aus der oben genannten Seite:

    #include <windows.h> 
    #include <tchar.h>
    #include <stdio.h> 
    
    int main() 
    { 
       TCHAR   inBuf[80]; 
       HKEY   hKey1, hKey2; 
       DWORD  dwDisposition; 
       LONG   lRetCode; 
       TCHAR   szData[] = TEXT("USR:App Name\\Section1");
    
       // Create the .ini file key. 
       lRetCode = RegCreateKeyEx ( HKEY_LOCAL_MACHINE, 
           TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\"
                "IniFileMapping\\appname.ini"), 
           0, 
           NULL, 
           REG_OPTION_NON_VOLATILE, 
           KEY_WRITE, 
           NULL, 
           &hKey1, 
           &dwDisposition); 
    
       if (lRetCode != ERROR_SUCCESS)
       { 
          printf ("Error in creating appname.ini key (%d).\n", lRetCode); 
          return (0) ; 
       } 
    
       // Set a section value 
       lRetCode = RegSetValueEx ( hKey1, 
                                  TEXT("Section1"), 
                                  0, 
                                  REG_SZ, 
                                  (BYTE *)szData, 
                                  sizeof(szData)); 
    
       if (lRetCode != ERROR_SUCCESS) 
       { 
          printf ("Error in setting Section1 value\n"); 
          // Close the key
          lRetCode = RegCloseKey( hKey1 );
          if( lRetCode != ERROR_SUCCESS )
          {
             printf("Error in RegCloseKey (%d).\n", lRetCode);
             return (0) ; 
          }
       } 
    
       // Create an App Name key 
       lRetCode = RegCreateKeyEx ( HKEY_CURRENT_USER, 
                                   TEXT("App Name"), 
                                   0, 
                                   NULL, 
                                   REG_OPTION_NON_VOLATILE,
                                   KEY_WRITE, 
                                   NULL, 
                                   &hKey2, 
                                   &dwDisposition); 
    
       if (lRetCode != ERROR_SUCCESS) 
       { 
          printf ("Error in creating App Name key (%d).\n", lRetCode); 
    
          // Close the key
          lRetCode = RegCloseKey( hKey2 );
          if( lRetCode != ERROR_SUCCESS )
          {
             printf("Error in RegCloseKey (%d).\n", lRetCode);
             return (0) ; 
          }
       } 
    
       // Force the system to read the mapping into shared memory 
       // so that future invocations of the application will see it 
       // without the user having to reboot the system 
       WritePrivateProfileStringW( NULL, NULL, NULL, L"appname.ini" ); 
    
       // Write some added values 
       WritePrivateProfileString (TEXT("Section1"), 
                                  TEXT("FirstKey"), 
                                  TEXT("It all worked out OK."), 
                                  TEXT("appname.ini")); 
       WritePrivateProfileString (TEXT("Section1"), 
                                  TEXT("SecondKey"), 
                                  TEXT("By golly, it works!"), 
                                  TEXT("appname.ini")); 
       WritePrivateProfileString (TEXT("Section1"), 
                                  TEXT("ThirdKey"), 
                                  TEXT("Another test..."), 
                                  TEXT("appname.ini")); 
    
       // Test 
       GetPrivateProfileString (TEXT("Section1"), 
                                TEXT("FirstKey"), 
                                TEXT("Error: GPPS failed"), 
                                inBuf, 
                                80, 
                                TEXT("appname.ini")); 
       _tprintf (TEXT("Key: %s\n"), inBuf); 
    
       // Close the keys
       lRetCode = RegCloseKey( hKey1 );
       if( lRetCode != ERROR_SUCCESS )
       {
          printf("Error in RegCloseKey (%d).\n", lRetCode);
          return(0);
       }
    
       lRetCode = RegCloseKey( hKey2 );
       if( lRetCode != ERROR_SUCCESS )
       {
          printf("Error in RegCloseKey (%d).\n", lRetCode);
          return(0);
       }
    
       return(1); 
    }
    


  • Einfach

    #include <windows.h>
    

    und dann einfach verwenden! Oder was hast Du für Probleme damit?



  • Jedoch spukt der Compiler das aus wenn ich die Funktion einfach so benutze und <windows.h> einbinde:

    : error C2664: 'WritePrivateProfileStringW': Konvertierung des Parameters 1 von 'const char [5]' in 'LPCWSTR' nicht möglich
    1> Die Typen, auf die verwiesen wird, sind nicht verknüpft; die Konvertierung erfordert einen reinterpret_cast-Operator oder eine Typumwandlung im C- oder Funktionsformat.

    : error C2664: 'WritePrivateProfileStringW': Konvertierung des Parameters 1 von 'const char [5]' in 'LPCWSTR' nicht möglich
    1> Die Typen, auf die verwiesen wird, sind nicht verknüpft; die Konvertierung erfordert einen reinterpret_cast-Operator oder eine Typumwandlung im C- oder Funktionsformat.

    So hab ich es eingesetzt:

    void SaveSettingsINI() {
    		WritePrivateProfileString("BEREICH","NAME","INHALT","c:\\test.ini");
    	}
    


  • Probier mal:

    WritePrivateProfileString(TEXT("BEREICH"),TEXT("NAME"),TEXT("INHALT"),TEXT("c:\\test.ini"));
    

    Angaben ohne Gewähr das ich grad keine Möglichkeit zum ausprobieren hab 😃

    P.S Noch ne Frage: Wie kann man das wieder auslesen?



  • Danke hat funktioniert!

    GetPrivateProfileString - (http://msdn.microsoft.com/en-us/library/ms724353(VS.85).aspx)


Anmelden zum Antworten