|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CreateIconedFolder
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
#region CreateIconedFolder
/// <summary>
/// Turns a folder into a "Special" folder, at least one that contains an icon and custom tooltip
/// </summary>
/// <remarks>
/// The default for iconIndex is 0.<br></br>
/// The default for toolTip is "".<br></br>
/// The default for preventSharing is false.<br></br>
/// The default for confirmDelete is true.<br></br>
/// </remarks>
/// <param name="folderName">The fully qualified path to the file.</param>
/// <param name="iconFile">The Icon File to use.</param>
/// <returns>True upon success, otherwise false.</returns>
private bool CreateIconedFolder(string folderName, string iconFile)
{
return CreateIconedFolder(folderName, iconFile, 0, "", false, true);
}
/// <summary>
/// Turns a folder into a "Special" folder, at least one that contains an icon and custom tooltip
/// </summary>
/// <param name="folderName">The fully qualified path to the file.</param>
/// <param name="iconFile">The Icon File to use.</param>
/// <param name="iconIndex">The index of the icon to use.</param>
/// <param name="toolTip">The tooltip or "InfoTip" to use.</param>
/// <param name="preventSharing">True indicates the folder can not be shared. False indicates sharing is allowed.</param>
/// <param name="confirmDelete">True indicates the use will recieve a message "You are deleting a system" when attempting
/// to delete the folder. False indicates that the operating system will not raise this warning.</param>
/// <returns>True upon success, otherwise false.</returns>
private bool CreateIconedFolder(string folderName, string iconFile, int iconIndex, string toolTip,
bool preventSharing, bool confirmDelete)
{
#region Private Variables
DirectoryInfo folder;
string fileName = "desktop.ini";
#endregion Private Variables
#region Data Validation
if (Directory.Exists(folderName) == false)
{
return false;
}
#endregion Data Validation
try
{
folder = new DirectoryInfo(folderName);
fileName = Path.Combine(folderName, fileName);
// Create the file
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine("[.ShellClassInfo]");
sw.WriteLine("ConfirmFileOp={0}", confirmDelete);
sw.WriteLine("NoSharing={0}", preventSharing);
sw.WriteLine("IconFile={0}", iconFile);
sw.WriteLine("IconIndex={0}", iconIndex);
sw.WriteLine("InfoTip={0}", toolTip);
sw.Close();
}
// Update the folder attributes
folder.Attributes = folder.Attributes | FileAttributes.System;
// "Hide" the desktop.ini
File.SetAttributes(fileName, File.GetAttributes(fileName) | FileAttributes.Hidden);
}
catch
{
return false;
}
return true;
}
#endregion CreateIconedFolder
#region UndoIconedFolder
/// <summary>
/// Turns a "Special" folder back into a normal folder.
/// </summary>
/// <param name="folderName">The folder to revert back.</param>
/// <returns>True upon success otherwise false.</returns>
private bool UndoIconedFolder(string folderName)
{
#region Private Variables
DirectoryInfo folder;
#endregion Private Variables
#region Data Validation
if (Directory.Exists(folderName) == false)
{
return false;
}
#endregion Data Validation
try
{
folder = new DirectoryInfo(folderName);
// Remove the file [Desktop.ini]
FileInfo file = new FileInfo(Path.Combine(folderName, "desktop.ini"));
if (file.Exists)
{
file.Delete();
}
folder.Attributes = (folder.Attributes | FileAttributes.System);
}
catch
{
return false;
}
return true;
}
#endregion UndoIconedFolder
#region Events
private void btnCreate_Click(object sender, EventArgs e)
{
bool result = CreateIconedFolder(txtFolder.Text,
txtIconFile.Text,
int.Parse(txtIconIndex.Text),
txtInfoTip.Text,
chkPreventSharing.Checked,
chkConfirmDelete.Checked);
string msg = string.Format("The result of CreateIconedFolder was {0}.", result);
if (result)
{
MessageBox.Show(msg, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(msg, "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txtFolder.Text) == false)
{
fbd.SelectedPath = txtFolder.Text;
}
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK)
{
txtFolder.Text = fbd.SelectedPath;
}
}
private void cmdUndo_Click(object sender, EventArgs e)
{
bool result = UndoIconedFolder(txtFolder.Text);
string msg = string.Format("The result of CreateIconedFolder was {0}.", result);
if (result)
{
MessageBox.Show(msg, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(msg, "Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion Events
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have been in software development for about 15 years or so. I started out with a small book on QuickBASIC, then moved the Visual Basic for DOS, then Visual Basic for Windows, then Visual Basic .NET and eventually Visual C#. When I am not working at my full time job I donate my time to several community efforts like:
Former President of INETA North America, currently Vice President.
President of the Southeast Valley .NET User Group (SEVDNUG) in Chandler, AZ.
Serving on my City's Parks and Recreation board.
I have or have had the following "MVP" awards:
- Visual Basic MVP in 1996
- C# MVP since 2009
- Telerik MVP since 2010
I maintain a Open Source project on CodePlex which wraps the Bing API called BingSharp.
I also help / organize or participate in several community events:
- Desert Code Camp
- AZGiveCamp
- Organizer for the 1st Time MVP event at the MVP Summit
- MVP 2 MVP Sessions at MVP Summit.
- Awesome bean pusher at GeekGive at the MVP Summit.