Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Folderbackground Customizer

3.50/5 (4 votes)
29 May 2008CPOL2 min read 1   330  
Select image and folder, then against the click button set the image as background at that folder.

Introduction

This code sets the background at the specified folder.

Background

Earlier, the user did not have any user interactive/friendly interface. The user could interact with the OS through DOS or any type of command prompt that then became interfaces for interacting with the OS and other applications. Then the user wanted to set different backgrounds at the desktop but user may have also wanted to set the background at many other places, for example at the background of any folder. Now, the user can fulfill his/her desire through different software.

Using the Code

Before going to the code, I want to explain the manual procedure which the code performs during execution.

Select the folder you want to use for your background wallpaper. You MUST convert the folder you want to use to a SYSTEM folder. At the command prompt screen (Start/Run/CMD) which looks something like this: c:\>, type this command: attrib +s c:\myfolder and then hit the return key.

Copy and paste the script below into Notepad and change the line that says IconArea_Image to point to the directory and the file that you want to use for the background 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

The last line in the script tells Windows what color the font will be when you read the files in that folder. For instance:

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

Save the file you just created as desktop.ini and place it into the directory you just converted into a system folder.

Now close that folder and then reopen.

Let's see how the code performs the above procedure automatically:

C#
//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");
        }
    }
}

Now the background has been set at the specified folder.
Tip: If a folder uses the wrong background picture and you can't change it: Start/Run/Regedit and remove this key: HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Bags.

Points of Interest

In this code, I run the command prompt and execute a specified command (attrib: +s "folder path") which converts the folder into the system folder and also does some file handling which changes file attributes and removes the background from the specified folder by deleting the desktop.ini file from the specified folder.

History

  • 29th May, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)