Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C#

Avoiding IDisposable while still working with unmanaged resources

Rate me:
Please Sign up or sign in to vote.
2.82/5 (14 votes)
9 Oct 2005CPOL14 min read 61.7K   358   12  
The IDisposable pattern and a way to avoid it.
using System;
using System.IO;
using System.Reflection;
using System.Resources;

using Pvax.Shell;

using NUnit.Framework;

namespace Pvax.Shell.Tests
{
	[TestFixture]
	public class TestShellLink
	{
		private string testFolderPath;

		[TestFixtureSetUp]
		public void FixtureInit()
		{
			testFolderPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Pvax.Shell.Tests");
			Directory.CreateDirectory(testFolderPath);
		}

		[TestFixtureTearDown]
		public void FixtureDone()
		{
			string[] files = Directory.GetFiles(testFolderPath, "*.*");
			foreach(string file in files)
				File.Delete(file);
			Directory.Delete(testFolderPath);
		}

		[Test]
		public void TestArguments()
		{
			ShellLink link = new ShellLink();
			link.Arguments = null;
			Assert.AreEqual("", link.Arguments);
			link.Arguments = "/b";
			Assert.AreEqual("/b", link.Arguments);
		}

		[Test]
		public void TestDescription()
		{
			ShellLink link = new ShellLink();
			link.Description = null;
			Assert.AreEqual("", link.Description);
			link.Description = "Some textual description";
			Assert.AreEqual("Some textual description", link.Description);
		}

		private const short hotkey = 0x09 | (0x04 << 8); // VK_TAB | (HOTKEYF_ALT << 8);

		[Test]
		public void TestHotkey()
		{
			ShellLink link = new ShellLink();
			link.Hotkey = hotkey;
			Assert.AreEqual(hotkey, link.Hotkey);
		}

		[Test]
		public void TestIconPath()
		{
			ShellLink link = new ShellLink();
			link.IconPath = null;
			Assert.AreEqual("", link.IconPath);
			link.IconPath = @"C:\Windows\System32\shell32.dll";
			Assert.AreEqual(@"C:\Windows\System32\shell32.dll", link.IconPath);
		}

		[Test]
		public void TestIconIndex()
		{
			ShellLink link = new ShellLink();
			link.IconIndex = 3;
			Assert.AreEqual(3, link.IconIndex);
		}

		[Test]
		public void TestPath()
		{
			ShellLink link = new ShellLink();
			link.Path = null;
			Assert.AreEqual("", link.Path);
			link.Path = @"C:\Windows\System32\user32.dll";
			Assert.AreEqual(@"C:\Windows\System32\user32.dll", link.Path);
		}

		[Test]
		public void TestShowCmd()
		{
			ShellLink link = new ShellLink();
			link.ShowCmd = ShellLink.ShowCommand.ShowMinimized;
			Assert.AreEqual(ShellLink.ShowCommand.ShowMinimized, link.ShowCmd);
		}

		[Test]
		public void TestWorkingDirectory()
		{
			ShellLink link = new ShellLink();
			link.WorkingDirectory = null;
			Assert.AreEqual("", link.WorkingDirectory);
			link.WorkingDirectory = @"C:\";
			Assert.AreEqual(@"C:\", link.WorkingDirectory);
		}

		[Test]
		public void TestSaveLoad()
		{
			{
				ShellLink link = new ShellLink();
				link.Path = @"%SystemRoot%\System32\kernel32.dll";
				link.Description = "Windows\' kernel API";
				link.Save(System.IO.Path.Combine(testFolderPath, "kernel32.lnk"));
			}
			{
				ShellLink link = new ShellLink();
				link.Load(System.IO.Path.Combine(testFolderPath, "kernel32.lnk"));
				Assert.AreEqual(System.IO.Path.Combine(Environment.SystemDirectory, "kernel32.dll").ToLower(), link.Path.ToLower());
				Assert.AreEqual("Windows\' kernel API", link.Description);
			}
		}
	}
}

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.

License

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


Written By
Web Developer
Russian Federation Russian Federation
I'm a system administrator from Moscow, Russia. Programming is one of my hobbies. I presume I'm one of the first Russians who created a Web site dedicated to .Net known that time as NGWS. However, the Web page has been abandoned a long ago.

Comments and Discussions