PDA

Vollständige Version anzeigen : Systemvariable setzen in VB


rw
29.04.2001, 21:53
Wie kann man eine im BS gesetzte Umgebungsvariable aus einem Programm heraus neu setzen (z. B. aus C:\temp wird für die Dauer des Programms F:\temp)

Stefan Kulpa
01.05.2001, 15:02
Hallo,

so sollte es gehen:

<div><link href="http://www.ms-office-forum.net/forum/externals/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Private</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> SetEnvironmentVariable <span class="TOKEN">Lib</span> &quot;kernel32&quot; <span class="TOKEN">Alias</span> _
&quot;SetEnvironmentVariableA&quot; _
(<span class="TOKEN">ByVal</span> lpName <span class="TOKEN">As</span> String, _
<span class="TOKEN">ByVal</span> lpValue <span class="TOKEN">As</span> <span class="TOKEN">String</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Private</span> <span class="TOKEN">Declare</span> <span class="TOKEN">Function</span> GetEnvironmentVariable <span class="TOKEN">Lib</span> &quot;kernel32&quot; <span class="TOKEN">Alias</span> _
&quot;GetEnvironmentVariableA&quot; _
(<span class="TOKEN">ByVal</span> lpName <span class="TOKEN">As</span> String, _
<span class="TOKEN">ByVal</span> lpBuffer <span class="TOKEN">As</span> String, _
<span class="TOKEN">ByVal</span> nSize <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="TOKEN">Dim</span> m_SavePath <span class="TOKEN">As</span> <span class="TOKEN">String</span>
&nbsp;
<span class="TOKEN">Sub</span> ChanceTempPath()
&nbsp;
<span class="TOKEN">Dim</span> sBuffer <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> lResult <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> lLength <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
<span class="REM">'// Pfad zum Temp-Verzeichnis ermitteln</span>
sBuffer = <span class="TOKEN">String</span>(256, 0)
lLength = GetEnvironmentVariable(&quot;TEMP&quot;, sBuffer, Len(sBuffer))
<span class="TOKEN">If</span> lLength &gt; 0 <span class="TOKEN">Then</span>
m_SavePath = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
MsgBox &quot;Alter Temp-Pfad: &quot; &amp; m_SavePath
&nbsp;
<span class="REM">'// Pfad zum Temp-Verzeichnis neu setzen</span>
lResult = SetEnvironmentVariable(&quot;TEMP&quot;, &quot;E:\Temp&quot;)
&nbsp;
<span class="REM">'// Testen</span>
sBuffer = <span class="TOKEN">String</span>(256, 0)
lLength = GetEnvironmentVariable(&quot;TEMP&quot;, sBuffer, Len(sBuffer))
<span class="TOKEN">If</span> lLength &gt; 0 <span class="TOKEN">Then</span>
MsgBox &quot;Neuer Temp-Pfad: &quot; &amp; _
Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
&nbsp;
<span class="REM">'// Und wieder zur&uuml;cksetzen</span>
lResult = SetEnvironmentVariable(&quot;TEMP&quot;, m_SavePath)
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span><hr></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)

Gruß, Stefan

Carsten
30.04.2003, 13:36
Hallo Stefan,
entspricht dieser Weg dem Setzen einer Variablen mit SET in einer Batch-Datei ?
Diese würde in der System-Umgebung nämlich nich zu sehen sein aber für andere Software nutzbar sein.

Gruss Carsten

Stefan Kulpa
03.05.2003, 05:02
Das MSDN informiert wie folgt:

The SetEnvironmentVariable function sets the contents of the specified environment variable for the current process.

This function has no effect on the system environment variables or the environment variables of other processes.

To add or modify system environment variables, the user selects System from the Control Panel, then selects the Environment tab. The user can also add or modify environment variables at a command prompt using the set command. Environment variables created with the set command apply only to the command window in which they are set, and to its child processes. For more information, type set /? at a command prompt.

Also - weder noch! Beide Varianten verändern nicht die Systemvariablen, die beim Systemstart gesetzt werden.

HTH,

Carsten
03.05.2003, 08:23
Jepp...
das ist es- so wie die SET Funktion in DOS.
HGH (hat geholfen)

Gruss Carsten