65.9K
CodeProject 正在变化。 阅读更多。
Home

Folderbackground Customizer

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (4投票s)

2008年5月29日

CPOL

2分钟阅读

viewsIcon

40044

downloadIcon

347

选择图像和文件夹,然后单击按钮,将图像设置为该文件夹的背景。

引言

这段代码将背景设置为指定的文件夹。

背景

过去,用户没有用户交互/友好的界面。用户只能通过DOS或任何类型的命令行与操作系统交互,这些命令行后来变成了与操作系统和其他应用程序交互的界面。然后用户希望在桌面上设置不同的背景,但用户也可能希望在许多其他地方设置背景,例如在任何文件夹的背景上。现在,用户可以通过不同的软件来实现他们的愿望。

Using the Code

在进入代码之前,我想解释代码执行期间执行的手动过程。

选择您要用作背景壁纸的文件夹。您*必须*将要使用的文件夹转换为系统文件夹。在命令提示符屏幕(开始/运行/CMD)中,看起来像这样:c:\>,输入此命令:attrib +s c:\myfolder 然后按回车键。

将下面的脚本复制并粘贴到记事本中,并将说IconArea_Image 的那一行更改为指向您要用作背景图像的目录和文件。

[ExtShellFolderViews]
{BE098140-A513-11D0-A3A4-00C04FD706EC}={BE098140-A513-11D0-A3A4-00C04FD706EC}

[{BE098140-A513-11D0-A3A4-00C04FD706EC}] 
Attributes=1
IconArea_Image=C:\YOUR LOCATION\OF\ANY\IMAGE.JPG
IconArea_Text=0x00000000

脚本的最后一行告诉Windows在您读取该文件夹中的文件时字体颜色。例如

0x00000000 = black
0x00FF0000 = blue
0x0000FF00 = green
0x000000FF = red
0x00C000C0 = purple

将您刚刚创建的文件保存为desktop.ini,并将其放入您刚刚转换为系统文件夹的目录中。

现在关闭该文件夹,然后重新打开。

让我们看看代码如何自动执行上述过程

//this code will be used for selecting the folder location        
private void folderpbtn_Click(object sender, EventArgs e)
{
    FolderBrowserDialog openFolder = new FolderBrowserDialog();
    //openFolder.RootFolder = System.Environment.SpecialFolder ("C:\\");
    if (openFolder.ShowDialog() == DialogResult.OK)
    {
        if ((folderpath = openFolder.SelectedPath) != null)
            this.folderptxt.Text = folderpath;
    }
}  //now for image selection as the background of the specified folder
private void bgibtn_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.InitialDirectory = "c:\\";
    openFileDialog1.Filter = "Jpg files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp|" +
                             "Icon files (*.ico)|*.ico|Jpeg files (*.jpeg)|*.jpeg|" +
                             "Exif files (*.bmp)|*.exif|TIFF files (*.tiff)|*.tiff";
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.RestoreDirectory = true;
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        if ((imagepath = openFileDialog1.FileName) != null)
        {
            this.bgitxt.Text = imagepath;
            // Set the SizeMode property to the StretchImage value. This
            // will shrink or enlarge the image as needed to fit into
            // the PictureBox.
            this.imagepicbox.SizeMode = PictureBoxSizeMode.StretchImage;
            this.imagepicbox.Image = System.Drawing.Image.FromFile(imagepath);
        }
    }
}  //now for converting the selected folder into the system folder 
void run()
{
    try
    {
        path = String.Concat("\"", path);
        path = String.Concat(path, "\"");
        FileStream aFile = new FileStream("test.bat", FileMode.Create);
        StreamWriter sw = new StreamWriter(aFile);
        //Write data to the file
        sw.WriteLine(String.Concat("attrib +s ", path));
        //MessageBox.Show(String.Concat("attrib +s ", path));
        // Directory.Move(path, "C:\\hello");
        sw.Close();
        //Console.WriteLine("Hello");
    }
    catch (IOException e)
    {
        Console.WriteLine("An IO exception have been thrown !");
        Console.WriteLine(e.ToString());
        Console.ReadLine();
        return;
    }
}

//for running the batch file which will convert the folder into the system folder and 
// batch file lies at absolute path
public void OpenApplication()
{
    Process.Start(".\\test.bat");
}

// code for final step creating desktop.ini file in the specified folder
class background
{
    string imagepath,folderpath;
    public background(string imageaddress,string folderaddress)
    {
        imagepath = imageaddress;
        folderpath = folderaddress;
    }
    public void setbackground()
    {
        set();
    }

    void set()
    {
        string file=String.Concat(folderpath, "\\desktop.ini");
        if (File.Exists(file))
        {
            if (MessageBox.Show("It may be change your Operating system setting?", 
                "User Database Application",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                File.Delete(file);
                makefile(file);
            }
        }
        else
            makefile(file);
    }
    void makefile(string filepath)
    {
        try
        {
            FileStream aFile = new FileStream
				(filepath, FileMode.CreateNew, FileAccess.Write);
            File.SetAttributes(filepath, FileAttributes.Hidden);
            StreamWriter sw = new StreamWriter(aFile);
            sw.WriteLine("[ExtShellFolderViews]");
            sw.WriteLine("{BE098140-A513-11D0-A3A4-00C04FD706EC}=
				{BE098140-A513-11D0-A3A4-00C04FD706EC}");
            sw.WriteLine("[{BE098140-A513-11D0-A3A4-00C04FD706EC}] ");
            sw.WriteLine("Attributes=1");
            sw.WriteLine(String.Concat("IconArea_Image = ", imagepath));
            sw.WriteLine("IconArea_Text=0x00000000");
            sw.Close();
        }
        catch (IOException e)
        {
            MessageBox.Show("Background can not be set on the specified folder");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Background can not be set on the specified folder");
        }
    }
}

现在背景已设置为指定的文件夹。
提示:如果某个文件夹使用错误的背景图片并且您无法更改它:开始/运行/Regedit并删除此键:HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Bags

关注点

在这段代码中,我运行命令提示符并执行指定的命令(attrib: +s "folder path"),该命令将文件夹转换为系统文件夹,并执行一些文件处理,通过删除指定文件夹中的desktop.ini文件来更改文件属性并删除背景。

历史

  • 2008年5月29日:初始发布
© . All rights reserved.