C#獲取當前用戶的壁紙的路徑,,要是純色則返回空、
編譯環境:vs2013 .net 4.5
代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 獲取windows桌面背景
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
#region 獲取windows桌面背景
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public static extern int SystemParametersInfo(int uAction, int uParam, StringBuilder lpvParam, int fuWinIni);
private const int SPI_GETDESKWALLPAPER = 0x0073;
#endregion
private void button1_Click(object sender, EventArgs e)
{
//定義存儲緩沖區大小
StringBuilder s = new StringBuilder(300);
//獲取Window 桌面背景圖片地址,使用緩沖區
SystemParametersInfo(SPI_GETDESKWALLPAPER, 300, s, 0);
//緩沖區中字符進行轉換
string wallpaper_path = s.ToString(); //系統桌面背景圖片路徑
textBox1.Text = wallpaper_path;
if (textBox1.Text == "")
{
button2.Visible = false;
MessageBox.Show("桌面壁紙為純色或空");
}
else
{
button2.Visible = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", textBox1.Text);
}
}
}