GetEnvironmentVariable

本文展示了一个使用C/C++编程语言来获取和设置Windows环境变量的简单示例。通过调用`GetEnvironmentVariable`和`SetEnvironmentVariable`函数,演示了如何读取和修改av环境变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	TCHAR temp_buf[256];
	DWORD rs=GetEnvironmentVariable(TEXT("av"),temp_buf,255);
	if(rs)
	MessageBox(NULL,temp_buf,TEXT("TEMP"),MB_OK);
	else
	{
		BOOL as=SetEnvironmentVariable(TEXT("av"),TEXT("Jpanese"));
		if(as)
		{
			DWORD rs=GetEnvironmentVariable(TEXT("av"),temp_buf,255);
			if(rs)
			MessageBox(NULL,temp_buf,TEXT("av"),MB_OK);
		}
	}

	return 0;
}

安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows PS C:\Users\Administrator> # 获取当前系统PATH >> $currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine") -split ';' >> >> # 移除旧版curl路径(如果有) >> $newPath = $currentPath | Where-Object { $_ -ne "C:\Windows\System32" } >> >> # 确保新版curl路径在最前面 >> $curlPath = "E:\curl-8.15.0_4-win64-mingw\bin" >> $newPath = @($curlPath) + $newPath | Select-Object -Unique >> >> # 更新系统PATH >> [Environment]::SetEnvironmentVariable( >> "Path", >> ($newPath -join ';'), >> "Machine" >> ) >> >> # 刷新当前会话PATH >> $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") >> PS C:\Users\Administrator> # 编辑 PowerShell 配置文件 >> $profileContent = @" >> # 移除冲突别名 >> Remove-Item Alias:curl -ErrorAction SilentlyContinue >> >> # 添加自定义 curl 函数 >> function global:curl { >> & "$curlPath\curl.exe" @args >> } >> "@ >> >> # 创建/更新配置文件 >> if (-not (Test-Path $PROFILE)) { >> New-Item -Path $PROFILE -ItemType File -Force | Out-Null >> } >> $profileContent | Set-Content -Path $PROFILE >> >> # 重新加载配置 >> . $PROFILE >> >> # 验证 >> curl --version >> curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd PS C:\Users\Administrator> # 修复不兼容的配置 >> $curlrcPath = "$env:USERPROFILE\.curlrc" >> if (Test-Path $curlrcPath) { >> (Get-Content $curlrcPath) | >> Where-Object { $_ -notmatch "--compressed" } | >> Set-Content $curlrcPath >> } >> PS C:\Users\Administrator> # 测试1:版本验证 >> curl --version >> >> # 测试2:HTTP请求 >> curl -v https://example.com >> >> # 测试3:文件下载 >> curl -LO https://example.com/index.html >> >> # 测试4:JSON API >> curl -H "Accept: application/json" https://jsonplaceholder.typicode.com/todos/1 >> curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd * Host example.com:443 was resolved. * IPv6: (none) * IPv4: 96.7.128.175, 96.7.128.198, 23.192.228.80 * Trying 96.7.128.175:443... * ipv4 connect timeout after 4971ms, move on! * Trying 96.7.128.198:443... * ipv4 connect timeout after 2480ms, move on! * Trying 23.192.228.80:443... * ALPN: curl offers h2,http/1.1 * TLSv1.3 (OUT), TLS handshake, Client hello (1): * CAfile: E:\curl-8.15.0_4-win64-mingw\bin\curl-ca-bundle.crt * CApath: none * TLSv1.3 (IN), TLS handshake, Server hello (2): * TLSv1.3 (IN), TLS handshake, Unknown (8): * TLSv1.3 (IN), TLS handshake, Certificate (11): * TLSv1.3 (IN), TLS handshake, CERT verify (15): * TLSv1.3 (IN), TLS handshake, Finished (20): * TLSv1.3 (OUT), TLS handshake, Finished (20): * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / [blank] / UNDEF * ALPN: server accepted h2 * Server certificate: * subject: C=US; ST=California; L=Los Angeles; O=Internet Corporation for Assigned Names and Numbers; CN=*.example.com * start date: Jan 15 00:00:00 2025 GMT * expire date: Jan 15 23:59:59 2026 GMT * subjectAltName: host "example.com" matched cert's "example.com" * issuer: C=US; O=DigiCert Inc; CN=DigiCert Global G3 TLS ECC SHA384 2020 CA1 * SSL certificate verify ok. * Certificate level 0: Public key type ? (256/128 Bits/secBits), signed using ecdsa-with-SHA384 * Certificate level 1: Public key type ? (384/192 Bits/secBits), signed using ecdsa-with-SHA384 * Certificate level 2: Public key type ? (384/192 Bits/secBits), signed using ecdsa-with-SHA384 * Connected to example.com (23.192.228.80) port 443 * using HTTP/2 * [HTTP/2] [1] OPENED stream for https://example.com/ * [HTTP/2] [1] [:method: GET] * [HTTP/2] [1] [:scheme: https] * [HTTP/2] [1] [:authority: example.com] * [HTTP/2] [1] [:path: /] * [HTTP/2] [1] [user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0] * [HTTP/2] [1] [accept: */*] > GET / HTTP/2 > Host: example.com > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0 > Accept: */* > * Request completely sent off < HTTP/2 200 < accept-ranges: bytes < content-type: text/html < etag: "84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134" < last-modified: Mon, 13 Jan 2025 20:11:20 GMT < content-length: 1256 < cache-control: max-age=2768 < date: Tue, 12 Aug 2025 10:57:29 GMT < alt-svc: h3=":443"; ma=93600,h3-29=":443"; ma=93600 < <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> * Connection #0 to host example.com left intact { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } PS C:\Users\Administrator> # 1. 修复PATH优先级问题 >> $curlPath = "E:\curl-8.15.0_4-win64-mingw\bin" >> $currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine") -split ';' >> $newPath = @($curlPath) + ($currentPath | Where-Object { $_ -ne $curlPath }) | Select-Object -Unique >> [Environment]::SetEnvironmentVariable("Path", ($newPath -join ';'), "Machine") >> $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") >> >> # 2. 创建永久别名方案 >> $profileContent = @" >> # 移除冲突别名 >> Remove-Item Alias:curl -ErrorAction SilentlyContinue >> >> # 添加自定义curl函数 >> function global:curl { >> & "$curlPath\curl.exe" @args >> } >> "@ >> >> if (-not (Test-Path $PROFILE)) { >> New-Item -Path $PROFILE -ItemType File -Force | Out-Null >> } >> $profileContent | Set-Content -Path $PROFILE >> . $PROFILE # 重新加载配置文件 >> >> # 3. 修复.curlrc配置警告 >> $curlrcPath = "$env:USERPROFILE\.curlrc" >> if (Test-Path $curlrcPath) { >> (Get-Content $curlrcPath) | >> Where-Object { $_ -notmatch "--compressed" } | >> Set-Content $curlrcPath >> } >> PS C:\Users\Administrator> # 测试结果输出 >> curl --version >> # 输出: curl 8.15.0 (x86_64-w64-mingw32) ... Release-Date: 2025-07-16 ... >> >> curl -v https://example.com >> # 输出: * Host example.com:443 was resolved... >> # * IPv6: (none) >> # * IPv4: 96.7.128.175, 96.7.128.198, 23.192.228.80 >> # * Trying 96.7.128.175:443... >> curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd * Host example.com:443 was resolved. * IPv6: (none) * IPv4: 96.7.128.175, 96.7.128.198, 23.192.228.80 * Trying 96.7.128.175:443... * ipv4 connect timeout after 4997ms, move on! * Trying 96.7.128.198:443... * ipv4 connect timeout after 2493ms, move on! * Trying 23.192.228.80:443... * ALPN: curl offers h2,http/1.1 * TLSv1.3 (OUT), TLS handshake, Client hello (1): * CAfile: E:\curl-8.15.0_4-win64-mingw\bin\curl-ca-bundle.crt * CApath: none * TLSv1.3 (IN), TLS handshake, Server hello (2): * TLSv1.3 (IN), TLS handshake, Unknown (8): * TLSv1.3 (IN), TLS handshake, Certificate (11): * TLSv1.3 (IN), TLS handshake, CERT verify (15): * TLSv1.3 (IN), TLS handshake, Finished (20): * TLSv1.3 (OUT), TLS handshake, Finished (20): * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / [blank] / UNDEF * ALPN: server accepted h2 * Server certificate: * subject: C=US; ST=California; L=Los Angeles; O=Internet Corporation for Assigned Names and Numbers; CN=*.example.com * start date: Jan 15 00:00:00 2025 GMT * expire date: Jan 15 23:59:59 2026 GMT * subjectAltName: host "example.com" matched cert's "example.com" * issuer: C=US; O=DigiCert Inc; CN=DigiCert Global G3 TLS ECC SHA384 2020 CA1 * SSL certificate verify ok. * Certificate level 0: Public key type ? (256/128 Bits/secBits), signed using ecdsa-with-SHA384 * Certificate level 1: Public key type ? (384/192 Bits/secBits), signed using ecdsa-with-SHA384 * Certificate level 2: Public key type ? (384/192 Bits/secBits), signed using ecdsa-with-SHA384 * Connected to example.com (23.192.228.80) port 443 * using HTTP/2 * [HTTP/2] [1] OPENED stream for https://example.com/ * [HTTP/2] [1] [:method: GET] * [HTTP/2] [1] [:scheme: https] * [HTTP/2] [1] [:authority: example.com] * [HTTP/2] [1] [:path: /] * [HTTP/2] [1] [user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0] * [HTTP/2] [1] [accept: */*] > GET / HTTP/2 > Host: example.com > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0 > Accept: */* > * Request completely sent off < HTTP/2 200 < content-type: text/html < etag: "84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134" < last-modified: Mon, 13 Jan 2025 20:11:20 GMT < cache-control: max-age=2733 < date: Tue, 12 Aug 2025 10:58:04 GMT < alt-svc: h3=":443"; ma=93600,h3-29=":443"; ma=93600 < content-length: 1256 < <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> * Connection #0 to host example.com left intact PS C:\Users\Administrator> # curl_version_check.ps1 >> $installedVersion = (curl --version | Select-String "curl (\d+\.\d+\.\d+)").Matches[0].Groups[1].Value >> $latestVersion = (curl -s https://curl.se/info | >> Select-String "The latest release: curl (\d+\.\d+\.\d+)").Matches[0].Groups[1].Value >> >> if ([version]$installedVersion -lt [version]$latestVersion) { >> Write-Host "⚠️ 发现新版本: $latestVersion (当前: $installedVersion)" -ForegroundColor Yellow >> Write-Host "执行以下命令更新: winget install curl.curl" -ForegroundColor Cyan >> } else { >> Write-Host "✅ 已安装最新版 curl $installedVersion" -ForegroundColor Green >> } >> 无法对 Null 数组进行索引。 所在位置 行:3 字符: 1 + $latestVersion = (curl -s https://curl.se/info | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [],RuntimeException + FullyQualifiedErrorId : NullArray ✅ 已安装最新版 curl 8.15.0 PS C:\Users\Administrator> function Set-CurlVersion { >> param( >> [ValidateSet("8.15", "system")] >> [string]$Version = "8.15" >> ) >> >> $pathItems = [System.Collections.ArrayList]@( >> [Environment]::GetEnvironmentVariable("Path", "Machine") -split ';' >> ) >> >> switch ($Version) { >> "8.15" { >> $pathItems.Remove("C:\Windows\System32") | Out-Null >> if (-not $pathItems.Contains($curlPath)) { >> $pathItems.Insert(0, $curlPath) >> } >> } >> "system" { >> $pathItems.Remove($curlPath) | Out-Null >> if (-not $pathItems.Contains("C:\Windows\System32")) { >> $pathItems.Add("C:\Windows\System32") | Out-Null >> } >> } >> } >> >> $newPath = $pathItems -join ';' >> [Environment]::SetEnvironmentVariable("Path", $newPath, "Machine") >> $env:Path = $newPath >> >> Write-Host "已切换到 curl $Version 版本" -ForegroundColor Green >> } >> PS C:\Users\Administrator> function Invoke-SecureDownload { >> param( >> [string]$Url, >> [string]$ExpectedHash, >> [string]$OutputPath = (Split-Path -Leaf $Url) >> ) >> >> curl -LO $Url >> $actualHash = (Get-FileHash -Path $OutputPath -Algorithm SHA256).Hash >> >> if ($actualHash -ne $ExpectedHash) { >> Remove-Item $OutputPath -Force >> throw "文件哈希验证失败! 可能被篡改或下载不完整" >> } >> >> Write-Host "✅ 文件验证成功: $OutputPath" -ForegroundColor Green >> return $OutputPath >> } >> >> # 使用示例: >> # Invoke-SecureDownload -Url "https://example.com/important.zip" -ExpectedHash "a1b2c3d4..." >> PS C:\Users\Administrator> # 综合测试脚本 >> Write-Host "=== 开始 curl 功能验证测试 ===" -ForegroundColor Cyan >> >> # 测试1: 基本功能 >> Write-Host "测试1: 版本验证" -ForegroundColor Yellow >> curl --version >> >> # 测试2: HTTP请求 >> Write-Host "`n测试2: HTTP请求" -ForegroundColor Yellow >> curl -v https://example.com >> >> # 测试3: 文件下载 >> Write-Host "`n测试3: 文件下载" -ForegroundColor Yellow >> curl -LO https://example.com/index.html >> if (Test-Path .\index.html) { >> Write-Host "✅ 文件下载成功" -ForegroundColor Green >> Remove-Item .\index.html -Force >> } >> >> # 测试4: JSON API >> Write-Host "`n测试4: JSON API调用" -ForegroundColor Yellow >> curl -H "Accept: application/json" https://jsonplaceholder.typicode.com/todos/1 >> >> Write-Host "`n=== 所有测试通过 ===" -ForegroundColor Green >> === 开始 curl 功能验证测试 === 测试1: 版本验证 curl 8.15.0 (x86_64-w64-mingw32) libcurl/8.15.0 LibreSSL/4.1.0 zlib/1.3.1.zlib-ng brotli/1.1.0 zstd/1.5.7 WinIDN libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 ngtcp2/1.14.0 nghttp3/1.11.0 Release-Date: 2025-07-16 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli CAcert HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL SSLS-EXPORT SSPI threadsafe UnixSockets zstd 测试2: HTTP请求 * Host example.com:443 was resolved. * IPv6: (none) * IPv4: 96.7.128.175, 96.7.128.198, 23.192.228.80 * Trying 96.7.128.175:443... * ipv4 connect timeout after 4996ms, move on! * Trying 96.7.128.198:443... * ipv4 connect timeout after 2496ms, move on! * Trying 23.192.228.80:443... * ALPN: curl offers h2,http/1.1 * TLSv1.3 (OUT), TLS handshake, Client hello (1): * CAfile: E:\curl-8.15.0_4-win64-mingw\bin\curl-ca-bundle.crt * CApath: none * TLSv1.3 (IN), TLS handshake, Server hello (2): * TLSv1.3 (IN), TLS handshake, Unknown (8): * TLSv1.3 (IN), TLS handshake, Certificate (11): * TLSv1.3 (IN), TLS handshake, CERT verify (15): * TLSv1.3 (IN), TLS handshake, Finished (20): * TLSv1.3 (OUT), TLS handshake, Finished (20): * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / [blank] / UNDEF * ALPN: server accepted h2 * Server certificate: * subject: C=US; ST=California; L=Los Angeles; O=Internet Corporation for Assigned Names and Numbers; CN=*.example.com * start date: Jan 15 00:00:00 2025 GMT * expire date: Jan 15 23:59:59 2026 GMT * subjectAltName: host "example.com" matched cert's "example.com" * issuer: C=US; O=DigiCert Inc; CN=DigiCert Global G3 TLS ECC SHA384 2020 CA1 * SSL certificate verify ok. * Certificate level 0: Public key type ? (256/128 Bits/secBits), signed using ecdsa-with-SHA384 * Certificate level 1: Public key type ? (384/192 Bits/secBits), signed using ecdsa-with-SHA384 * Certificate level 2: Public key type ? (384/192 Bits/secBits), signed using ecdsa-with-SHA384 * Connected to example.com (23.192.228.80) port 443 * using HTTP/2 * [HTTP/2] [1] OPENED stream for https://example.com/ * [HTTP/2] [1] [:method: GET] * [HTTP/2] [1] [:scheme: https] * [HTTP/2] [1] [:authority: example.com] * [HTTP/2] [1] [:path: /] * [HTTP/2] [1] [user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0] * [HTTP/2] [1] [accept: */*] > GET / HTTP/2 > Host: example.com > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Curl/8.15.0 > Accept: */* > * Request completely sent off < HTTP/2 200 < accept-ranges: bytes < content-type: text/html < etag: "84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134" < last-modified: Mon, 13 Jan 2025 20:11:20 GMT < content-length: 1256 < cache-control: max-age=2684 < date: Tue, 12 Aug 2025 10:58:53 GMT < alt-svc: h3=":443"; ma=93600,h3-29=":443"; ma=93600 < <!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> * Connection #0 to host example.com left intact 测试3: 文件下载 ✅ 文件下载成功 测试4: JSON API调用 { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } === 所有测试通过 === PS C:\Users\Administrator>
08-13
(亲亲)放在那个idea库里吧 今天太晚了 不弄进度条那种花里古哨 华而不实的东西了 我们先把开发做好 ”PS C:\Users\Administrator> # 清理系统环境变量 >> $sysPath = [Environment]::GetEnvironmentVariable("Path", "Machine") >> $sysPathList = $sysPath -split ';' | >> Where-Object { >> $_ -ne "" -and >> $_ -notmatch "Microsoft VS Code" >> } | >> Select-Object -Unique >> $newSysPath = $sysPathList -join ';' >> [Environment]::SetEnvironmentVariable("Path", $newSysPath, "Machine") >> >> # 清理用户环境变量 >> $userPath = [Environment]::GetEnvironmentVariable("Path", "User") >> $userPathList = $userPath -split ';' | >> Where-Object { >> $_ -ne "" -and >> $_ -notmatch "Microsoft VS Code" >> } | >> Select-Object -Unique >> $newUserPath = $userPathList -join ';' >> [Environment]::SetEnvironmentVariable("Path", $newUserPath, "User") >> >> # 添加唯一正确路径(系统级) >> $correctPath = "C:\Program Files\Microsoft VS Code\bin" >> [Environment]::SetEnvironmentVariable("Path", "$newSysPath;$correctPath", "Machine") >> >> # 刷新当前会话 >> $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + >> [System.Environment]::GetEnvironmentVariable("Path","User") >> PS C:\Users\Administrator> function Test-VSCodeInstall { >> $issues = @() >> $fixCommands = @() >> >> # 检查安装路径唯一性 >> $installPaths = $env:Path -split ';' | >> Where-Object { $_ -like "*Microsoft VS Code*" } | >> Sort-Object -Unique >> >> if ($installPaths.Count -ne 1) { >> $issues += "存在多个安装路径: $($installPaths -join ', ')" >> >> # 生成修复命令 >> $fixCommands += "# 清理环境变量命令:" >> $fixCommands += '$sysPath = [Environment]::GetEnvironmentVariable("Path", "Machine")' >> $fixCommands += '$newSysPath = ($sysPath -split ";" | Where-Object { $_ -notmatch "Microsoft VS Code" }) -join ";"' >> $fixCommands += '[Environment]::SetEnvironmentVariable("Path", $newSysPath, "Machine")' >> $fixCommands += '$userPath = [Environment]::GetEnvironmentVariable("Path", "User")' >> $fixCommands += '$newUserPath = ($userPath -split ";" | Where-Object { $_ -notmatch "Microsoft VS Code" }) -join ";"' >> $fixCommands += '[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")' >> $fixCommands += '$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")' >> } >> >> # 检查关键文件 >> $requiredFiles = @( >> "C:\Program Files\Microsoft VS Code\Code.exe", >> "C:\Program Files\Microsoft VS Code\bin\code.cmd" >> ) >> >> foreach ($file in $requiredFiles) { >> if (-not (Test-Path $file)) { >> $issues += "缺少关键文件: $file" >> } else { >> $fileInfo = Get-Item $file >> if ($fileInfo.Name -eq "code.cmd" -and $fileInfo.Length -lt 150) { >> $issues += "文件大小异常: $file ($($fileInfo.Length) bytes)" >> } >> } >> } >> >> # 检查版本命令 >> try { >> $versionOutput = code --version 2>&1 | Select-Object -First 3 >> if (-not $versionOutput) { >> $issues += "无法获取版本信息" >> } else { >> Write-Host "`nVS Code版本信息:" -ForegroundColor Green >> $versionOutput | ForEach-Object { Write-Host " $_" } >> } >> } catch { >> $issues += "code命令执行失败: $_" >> } >> >> if ($issues) { >> Write-Host "`n检测到问题:" -ForegroundColor Red >> $issues | ForEach-Object { Write-Host " - $_" -ForegroundColor Yellow } >> >> if ($fixCommands) { >> Write-Host "`n修复建议:" -ForegroundColor Cyan >> $fixCommands | ForEach-Object { Write-Host $_ } >> } >> >> return $false >> } >> >> Write-Host "`nVS Code安装验证通过" -ForegroundColor Green >> return $true >> } PS C:\Users\Administrator> PS C:\Users\Administrator> # 执行验证 PS C:\Users\Administrator> Test-VSCodeInstall VS Code版本信息: 1.103.0 e3550cfac4b63ca4eafca7b601f0d2885817fd1f x64 VS Code安装验证通过 True PS C:\Users\Administrator> function Test-VSCodeInstall { >> $issues = @() >> $fixCommands = @() >> >> # 检查安装路径唯一性 >> $installPaths = $env:Path -split ';' | >> Where-Object { $_ -like "*Microsoft VS Code*" } | >> Sort-Object -Unique >> >> if ($installPaths.Count -ne 1) { >> $issues += "存在多个安装路径: $($installPaths -join ', ')" >> >> # 生成修复命令 >> $fixCommands += "# 清理环境变量命令:" >> $fixCommands += '$sysPath = [Environment]::GetEnvironmentVariable("Path", "Machine")' >> $fixCommands += '$newSysPath = ($sysPath -split ";" | Where-Object { $_ -notmatch "Microsoft VS Code" }) -join ";"' >> $fixCommands += '[Environment]::SetEnvironmentVariable("Path", $newSysPath, "Machine")' >> $fixCommands += '$userPath = [Environment]::GetEnvironmentVariable("Path", "User")' >> $fixCommands += '$newUserPath = ($userPath -split ";" | Where-Object { $_ -notmatch "Microsoft VS Code" }) -join ";"' >> $fixCommands += '[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")' >> $fixCommands += '$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")' >> } >> >> # 检查关键文件 >> $requiredFiles = @( >> "C:\Program Files\Microsoft VS Code\Code.exe", >> "C:\Program Files\Microsoft VS Code\bin\code.cmd" >> ) >> >> foreach ($file in $requiredFiles) { >> if (-not (Test-Path $file)) { >> $issues += "缺少关键文件: $file" >> } else { >> $fileInfo = Get-Item $file >> if ($fileInfo.Name -eq "code.cmd" -and $fileInfo.Length -lt 150) { >> $issues += "文件大小异常: $file ($($fileInfo.Length) bytes)" >> } >> } >> } >> >> # 检查版本命令 >> try { >> $versionOutput = code --version 2>&1 | Select-Object -First 3 >> if (-not $versionOutput) { >> $issues += "无法获取版本信息" >> } else { >> Write-Host "`nVS Code版本信息:" -ForegroundColor Green >> $versionOutput | ForEach-Object { Write-Host " $_" } >> } >> } catch { >> $issues += "code命令执行失败: $_" >> } >> >> if ($issues) { >> Write-Host "`n检测到问题:" -ForegroundColor Red >> $issues | ForEach-Object { Write-Host " - $_" -ForegroundColor Yellow } >> >> if ($fixCommands) { >> Write-Host "`n修复建议:" -ForegroundColor Cyan >> $fixCommands | ForEach-Object { Write-Host $_ } >> } >> >> return $false >> } >> >> Write-Host "`nVS Code安装验证通过" -ForegroundColor Green >> return $true >> } PS C:\Users\Administrator> PS C:\Users\Administrator> # 执行验证 PS C:\Users\Administrator> Test-VSCodeInstall VS Code版本信息: 1.103.0 e3550cfac4b63ca4eafca7b601f0d2885817fd1f x64 VS Code安装验证通过 True PS C:\Users\Administrator> function Test-VSCodeInstall { >> $issues = @() >> $fixCommands = @() >> >> # 检查安装路径唯一性 >> $installPaths = $env:Path -split ';' | >> Where-Object { $_ -like "*Microsoft VS Code*" } | >> Sort-Object -Unique >> >> if ($installPaths.Count -ne 1) { >> $issues += "存在多个安装路径: $($installPaths -join ', ')" >> >> # 生成修复命令 >> $fixCommands += "# 清理环境变量命令:" >> $fixCommands += '$sysPath = [Environment]::GetEnvironmentVariable("Path", "Machine")' >> $fixCommands += '$newSysPath = ($sysPath -split ";" | Where-Object { $_ -notmatch "Microsoft VS Code" }) -join ";"' >> $fixCommands += '[Environment]::SetEnvironmentVariable("Path", $newSysPath, "Machine")' >> $fixCommands += '$userPath = [Environment]::GetEnvironmentVariable("Path", "User")' >> $fixCommands += '$newUserPath = ($userPath -split ";" | Where-Object { $_ -notmatch "Microsoft VS Code" }) -join ";"' >> $fixCommands += '[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")' >> $fixCommands += '$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")' >> } >> >> # 检查关键文件 >> $requiredFiles = @( >> "C:\Program Files\Microsoft VS Code\Code.exe", >> "C:\Program Files\Microsoft VS Code\bin\code.cmd" >> ) >> >> foreach ($file in $requiredFiles) { >> if (-not (Test-Path $file)) { >> $issues += "缺少关键文件: $file" >> } else { >> $fileInfo = Get-Item $file >> if ($fileInfo.Name -eq "code.cmd" -and $fileInfo.Length -lt 150) { >> $issues += "文件大小异常: $file ($($fileInfo.Length) bytes)" >> } >> } >> } >> >> # 检查版本命令 >> try { >> $versionOutput = code --version 2>&1 | Select-Object -First 3 >> if (-not $versionOutput) { >> $issues += "无法获取版本信息" >> } else { >> Write-Host "`nVS Code版本信息:" -ForegroundColor Green >> $versionOutput | ForEach-Object { Write-Host " $_" } >> } >> } catch { >> $issues += "code命令执行失败: $_" >> } >> >> if ($issues) { >> Write-Host "`n检测到问题:" -ForegroundColor Red >> $issues | ForEach-Object { Write-Host " - $_" -ForegroundColor Yellow } >> >> if ($fixCommands) { >> Write-Host "`n修复建议:" -ForegroundColor Cyan >> $fixCommands | ForEach-Object { Write-Host $_ } >> } >> >> return $false >> } >> >> Write-Host "`nVS Code安装验证通过" -ForegroundColor Green >> return $true >> } >> >> # 执行验证 >> Test-VSCodeInstall >> VS Code版本信息: 1.103.0 e3550cfac4b63ca4eafca7b601f0d2885817fd1f x64 VS Code安装验证通过 True PS C:\Users\Administrator> VS Code版本信息: >> 1.103.0 >> 488a1f239235055e34e673291fb8d8c810886f81 >> x64 >> >> VS Code安装验证通过 >> VS : 无法将“VS”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然 后再试一次。 所在位置 行:1 字符: 1 + VS Code版本信息: + ~~ + CategoryInfo : ObjectNotFound: (VS:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 488a1f239235055e34e673291fb8d8c810886f81 : 无法将“488a1f239235055e34e673291fb8d8c810886f81”项识别为 cmdlet、函数、脚 本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:3 字符: 3 + 488a1f239235055e34e673291fb8d8c810886f81 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (488a1f239235055e34e673291fb8d8c810886f81:String) [], CommandNotFoundExc eption + FullyQualifiedErrorId : CommandNotFoundException x64 : 无法将“x64”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确, 然后再试一次。 所在位置 行:4 字符: 3 + x64 + ~~~ + CategoryInfo : ObjectNotFound: (x64:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException VS : 无法将“VS”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然 后再试一次。 所在位置 行:6 字符: 1 + VS Code安装验证通过 + ~~ + CategoryInfo : ObjectNotFound: (VS:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException “这个我现在应该怎么处理
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值