Umgebungsvariable %USERNAME% funktioniert nicht
-
Hallo Miteinander
Ich habe ein Programm, das auf einen Pfad zugreifen sollte, egal welcher Benutzer angemeldet ist. Dazu verwende ich die Umgebungsvariable %USERNAME%.
Das Programm lässt sich ausführen und gibt keine Fehlermeldung aus. Aber es funktioniert nicht! (Ausnahme: Der System-Befehl erkennt den Pfad und führt den Befehl auch aus.) Das Programm funktioniert einwandfrei, wenn ich statt der Variable den aktuellen Benutzer eingebe. Hier noch der Code:#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <time.h> int main() { //Deklarationen char DateinamemitDatum[255]; char PfadNormal[]="//<servername>/Profile/%USERNAME%/<Pfad>/<Datei>"; char OrginalNormal[]="//<servername>/Profile/%USERNAME%/<Pfad>/<DateiOld>"; time_t datenow; struct tm *dt; char li[30]; //Verarbeitung //Fensterabfrage //Deklaration= MessageBox(Null,"Fenstertext","Fensterüberschrift",Fragetyp | Antworttyp); int Antwort= MessageBox(NULL,"Frage?","Titel",MB_ICONQUESTION | MB_YESNOCANCEL); if(IDYES==Antwort) //Wenn Antwort ja { //Datum und Zeit berechnung time(&datenow); dt = localtime(&datenow); strftime(li, 30, " %d_%m_%Y %H.%M", dt); //Datum und Zeit an Datainame anhängen strcpy(DateinamemitDatum,"//servername>/Profile/%USERNAME%/<Pfad>/<Datei>"); strcat(DateinamemitDatum,li); strcat(DateinamemitDatum,"<dateiendung>"); //Umbennenen von PfadNormal in DateinamemitDatum rename(PfadNormal, DateinamemitDatum); //OrginalNormal nach PfadNormal kopieren CopyFile(OrginalNormal, PfadNormal,0); //Öffnet PfadNormal system(PfadNormal); } if(IDNO==Antwort) //Wenn Antwort nein { //Lösche PfadNormal remove(PfadNormal); //OrginalNormal nach PfadNormal kopieren CopyFile(OrginalNormal, PfadNormal,0); //Öffne PfadNormal system(PfadNormal); } exit(0); return 0; }
Fällt jemandem dazu was ein?
Benutze CodeBlocks unter Windows 7
-
Sooooo einfach ist das nicht.
Schau dir mal die Funktion getenv aus der C-Standardlibrary an.
Den Pfad musst du dann mit strcpy/strcat oder sprintf zusammen setzen.
-
Danke, werde ich gleich mal auspobieren.
-
//Öffnet PfadNormal system(PfadNormal);
das wird vermutlich nix öfnnen.
verzeichniswexel gehen z.b. mit _chdir
hier ein paar leckerbissen:#include <stdio.h> #include <stdlib.h> #include <direct.h> int main ( void ) { char* username = getenv ( "USERNAME" ); char* cwd = _getcwd(NULL,0); if ( username != NULL ) printf ( "The current username is: %s\n", username ); if ( cwd != NULL ) printf ( "The current working directory is: %s\n", cwd ); free ( cwd ); _chdir("c:\\"); cwd = _getcwd(NULL,0); if ( cwd != NULL ) printf ( "The current working directory is: %s\n", cwd ); free ( cwd ); return 0; }
-
Vielen Dank Leute!!
Funktioniert jetzt einwandfrei ohne Fehler!