本帖最后由 pcl_test 于 2016-6-29 14:17 编辑
举个栗子- /*&cls
- @echo off
- set "netpath=%systemroot%\Microsoft.NET\Framework"
- for /f "delims=" %%a in ('dir /ad /b "%netpath%\v?.*"') do (
- if exist "%netpath%\%%a\csc.exe" (
- set "cscpath=%netpath%\%%a\csc.exe"
- goto :0
- )
- )
- echo;未安装.Net Framework 2.0及其上版本组件或相关程序丢失&pause&exit
- :0
- if not exist "$ChangeWallpaper.exe" (
- "%cscpath%" /out:"$ChangeWallpaper.exe" "%~f0"
- )
- $ChangeWallpaper.exe "指定桌面背景图片的路径"
- pause&exit
- */
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using Microsoft.Win32;
- class ChangeWallpaper
- {
- [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
- public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
- static void Main(string[] args)
- {
- if (args.Length<1){
- Console.WriteLine("Usage: $ChangeWallpaper [path]picturename");
- }else{
- if (!File.Exists(args[0])){
- Console.WriteLine("The specified picture is not exist!");
- }else{
- string pic = args[0], ext = pic.Substring(pic.LastIndexOf(".")).ToLower();
- if (ext == ".jpg" || ext == ".bmp" ){
- string dir = Environment.GetEnvironmentVariable("SystemRoot")+"\\Web\\Wallpaper\\Windows";
- if (!Directory.Exists(dir))Directory.CreateDirectory(dir);
- string newpicpath = Path.Combine(dir, Path.GetFileName(pic));
- try{File.Copy(pic, newpicpath, true);}catch{newpicpath = pic;}
- if (SystemParametersInfo(20, 1, newpicpath, 0x2) == 0){
- Console.WriteLine("Setting wallpaper failed!");
- }else{
- Registry.CurrentUser.CreateSubKey(@"Control Panel\Desktop\").SetValue("Wallpaper", newpicpath);
- }
- }else Console.WriteLine("The format is not supported!");
- }
- }
-
- }
- }
复制代码
|