How to improve my c# code?
I made this code (except the getting text from clipboard part) a while back, and am wondering if it can be better? Can anyone help me improve this code?

Thanks

This code basically takes html text from the clipboard, and then attempts to fix the indentation and writes it to a new file.


	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Runtime.InteropServices;
	using System.IO;



	namespace Indent_2._0
	{
		public static class Clipboard
		{
			[DllImport("user32.dll")]
			static extern IntPtr GetClipboardData(uint uFormat);
			[DllImport("user32.dll")]
			static extern bool IsClipboardFormatAvailable(uint format);
			[DllImport("user32.dll", SetLastError = true)]
			static extern bool OpenClipboard(IntPtr hWndNewOwner);
			[DllImport("user32.dll", SetLastError = true)]
			static extern bool CloseClipboard();
			[DllImport("kernel32.dll")]
			static extern IntPtr GlobalLock(IntPtr hMem);
			[DllImport("kernel32.dll")]
			static extern bool GlobalUnlock(IntPtr hMem);

			const uint CF_UNICODETEXT = 13;

			public static string GetText()
			{
				if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
					return null;
				if (!OpenClipboard(IntPtr.Zero))
					return null;

				string data = null;
				var hGlobal = GetClipboardData(CF_UNICODETEXT);
				if (hGlobal != IntPtr.Zero)
				{
					var lpwcstr = GlobalLock(hGlobal);
					if (lpwcstr != IntPtr.Zero)
					{
						data = Marshal.PtrToStringUni(lpwcstr);
						GlobalUnlock(lpwcstr);
					}
				}
				CloseClipboard();

				return data;
			}
		}

		class Program
		{
			static string fix_encoding(string src_str)
			{
				src_str = src_str.Replace("â€‹", "");
				src_str = src_str.Replace("&amp;", "&");
				src_str = src_str.Replace("&nbsp;", " ");
				src_str = src_str.Replace("&#8206;", "");
				src_str = src_str.Replace("&#38;", "&");
				src_str = src_str.Replace("&#160;", " ");
				src_str = src_str.Replace("&#39;", "'");
				src_str = src_str.Replace("&quot;", "\"");
				return src_str;
			}

			static string slice(string src_str, int start, int end)
			{
				if (start < 0)
				{
					start = src_str.Length + start;
				}
				if (end < 0)
				{
					end = src_str.Length + end;
				}
				return src_str.Substring(start, end-start);
			}

			static Boolean prefix_check(string item, string[] tag_exception)
			{
				string b = item.ToLower();
				for (var i=0; i<tag_exception.Length; i++)
				{
					if (b.IndexOf(tag_exception[i]) == 0)
					{
						return true;
					}
				}
				return false;
			}

			static void fix_tags(string src_str, StreamWriter file_id)
			{
				src_str = fix_encoding(src_str);

				int count = 0;
				List<String> item_list = new List<String>();

				while (src_str != "")
				{
					int start_tag_index = src_str.IndexOf("<");
					if (start_tag_index == -1)
					{
						break;
					}

					item_list.Add(slice(src_str, 0, start_tag_index));
					src_str = slice(src_str, start_tag_index, src_str.Length);
					count++;

					int _g_ = src_str.IndexOf("<!--");
					if (_g_ == 0)
					{
						int _f_ = src_str.IndexOf("-->");
						item_list.Add(slice(src_str, _g_, _f_ + 3));
						count++;
						src_str = slice(src_str, _f_ + 3, src_str.Length);
						continue;
					}

					int end_tag_index = src_str.IndexOf(">");    
					string tag = slice(src_str, 0, end_tag_index + 1);
					item_list.Add(tag);
					count++;
					src_str = slice(src_str, end_tag_index + 1, src_str.Length);
				}

				string[] tag_exception = {"<img","<link","<meta","<br","<hr","<img","<input","<area","<param"};
				string indent = "";
				Boolean script = false;

				for (var i = 0; i < count; i++)
				{
					string item = item_list[i];
					if (item.Trim() == "")
					{
						continue;
					}
					int start = item.IndexOf("<");
					int end = item.IndexOf(">");
					int end2 = item.IndexOf("</");

					Boolean is_script = item.IndexOf("<script")==0;
					Boolean is_end_script = item.Length >= 9 && slice(item, -9, item.Length) == "</script>";
					Boolean is_tag = start == 0 && end == item.Length-1;
					Boolean is_closing_tag = end2 == 0;
					Boolean is_comment = item.IndexOf("<!") == 0;
					Boolean is_exception = prefix_check(item, tag_exception);

					if (script)
					{
						if (is_end_script)
						{
							indent = slice(indent,0,-1);   
							file_id.Write(indent + item.Trim().Replace("\n","") + "\n"); 
							script = false;
						}
						else {
							file_id.Write(indent + item.Trim() + "\n");
						}
					}
					else if (is_comment)
					{
						file_id.Write(indent + item.Trim() + "\n");
					}
					else if (is_closing_tag)
					{
						indent = slice(indent, 0, -1);
						file_id.Write(indent + item.Trim().Replace("\n", "") + "\n");
					}
					else if (is_exception)
					{
						file_id.Write(indent + item.Trim().Replace("\n", "") + "\n");
					}
					else if (is_tag)
					{
						file_id.Write(indent + item.Trim().Replace("\n", "") + "\n");
						indent += "\t";  
						if (is_script)
						{
							script = true;
						}
					}
					else {
						file_id.Write(indent + item.Trim() + "\n");
					}
				}
			}

			static void Main(string[] args)
			{
				StreamWriter file_id = new StreamWriter("./fixed_html.html", false, System.Text.Encoding.UTF8);
				fix_tags(Clipboard.GetText(), file_id);
				file_id.Close();
				System.Diagnostics.Process.Start("batch.bat");
			}
		}
	}

How to improve my c# code?
===

I made this code (except the getting text from clipboard part) a while back, and am wondering if it can be better? Can anyone help me improve this code?

Thanks

This code basically takes html text from the clipboard, and then attempts to fix the indentation and writes it to a new file.


	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Runtime.InteropServices;
	using System.IO;



	namespace Indent_2._0
	{
		public static class Clipboard
		{
			[DllImport("user32.dll")]
			static extern IntPtr GetClipboardData(uint uFormat);
			[DllImport("user32.dll")]
			static extern bool IsClipboardFormatAvailable(uint format);
			[DllImport("user32.dll", SetLastError = true)]
			static extern bool OpenClipboard(IntPtr hWndNewOwner);
			[DllImport("user32.dll", SetLastError = true)]
			static extern bool CloseClipboard();
			[DllImport("kernel32.dll")]
			static extern IntPtr GlobalLock(IntPtr hMem);
			[DllImport("kernel32.dll")]
			static extern bool GlobalUnlock(IntPtr hMem);

			const uint CF_UNICODETEXT = 13;

			public static string GetText()
			{
				if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
					return null;
				if (!OpenClipboard(IntPtr.Zero))
					return null;

				string data = null;
				var hGlobal = GetClipboardData(CF_UNICODETEXT);
				if (hGlobal != IntPtr.Zero)
				{
					var lpwcstr = GlobalLock(hGlobal);
					if (lpwcstr != IntPtr.Zero)
					{
						data = Marshal.PtrToStringUni(lpwcstr);
						GlobalUnlock(lpwcstr);
					}
				}
				CloseClipboard();

				return data;
			}
		}

		class Program
		{
			static string fix_encoding(string src_str)
			{
				src_str = src_str.Replace("â€‹", "");
				src_str = src_str.Replace("&amp;", "&");
				src_str = src_str.Replace("&nbsp;", " ");
				src_str = src_str.Replace("&#8206;", "");
				src_str = src_str.Replace("&#38;", "&");
				src_str = src_str.Replace("&#160;", " ");
				src_str = src_str.Replace("&#39;", "'");
				src_str = src_str.Replace("&quot;", "\"");
				return src_str;
			}

			static string slice(string src_str, int start, int end)
			{
				if (start < 0)
				{
					start = src_str.Length + start;
				}
				if (end < 0)
				{
					end = src_str.Length + end;
				}
				return src_str.Substring(start, end-start);
			}

			static Boolean prefix_check(string item, string[] tag_exception)
			{
				string b = item.ToLower();
				for (var i=0; i<tag_exception.Length; i++)
				{
					if (b.IndexOf(tag_exception[i]) == 0)
					{
						return true;
					}
				}
				return false;
			}

			static void fix_tags(string src_str, StreamWriter file_id)
			{
				src_str = fix_encoding(src_str);

				int count = 0;
				List<String> item_list = new List<String>();

				while (src_str != "")
				{
					int start_tag_index = src_str.IndexOf("<");
					if (start_tag_index == -1)
					{
						break;
					}

					item_list.Add(slice(src_str, 0, start_tag_index));
					src_str = slice(src_str, start_tag_index, src_str.Length);
					count++;

					int _g_ = src_str.IndexOf("<!--");
					if (_g_ == 0)
					{
						int _f_ = src_str.IndexOf("-->");
						item_list.Add(slice(src_str, _g_, _f_ + 3));
						count++;
						src_str = slice(src_str, _f_ + 3, src_str.Length);
						continue;
					}

					int end_tag_index = src_str.IndexOf(">");    
					string tag = slice(src_str, 0, end_tag_index + 1);
					item_list.Add(tag);
					count++;
					src_str = slice(src_str, end_tag_index + 1, src_str.Length);
				}

				string[] tag_exception = {"<img","<link","<meta","<br","<hr","<img","<input","<area","<param"};
				string indent = "";
				Boolean script = false;

				for (var i = 0; i < count; i++)
				{
					string item = item_list[i];
					if (item.Trim() == "")
					{
						continue;
					}
					int start = item.IndexOf("<");
					int end = item.IndexOf(">");
					int end2 = item.IndexOf("</");

					Boolean is_script = item.IndexOf("<script")==0;
					Boolean is_end_script = item.Length >= 9 && slice(item, -9, item.Length) == "</script>";
					Boolean is_tag = start == 0 && end == item.Length-1;
					Boolean is_closing_tag = end2 == 0;
					Boolean is_comment = item.IndexOf("<!") == 0;
					Boolean is_exception = prefix_check(item, tag_exception);

					if (script)
					{
						if (is_end_script)
						{
							indent = slice(indent,0,-1);   
							file_id.Write(indent + item.Trim().Replace("\n","") + "\n"); 
							script = false;
						}
						else {
							file_id.Write(indent + item.Trim() + "\n");
						}
					}
					else if (is_comment)
					{
						file_id.Write(indent + item.Trim() + "\n");
					}
					else if (is_closing_tag)
					{
						indent = slice(indent, 0, -1);
						file_id.Write(indent + item.Trim().Replace("\n", "") + "\n");
					}
					else if (is_exception)
					{
						file_id.Write(indent + item.Trim().Replace("\n", "") + "\n");
					}
					else if (is_tag)
					{
						file_id.Write(indent + item.Trim().Replace("\n", "") + "\n");
						indent += "\t";  
						if (is_script)
						{
							script = true;
						}
					}
					else {
						file_id.Write(indent + item.Trim() + "\n");
					}
				}
			}

			static void Main(string[] args)
			{
				StreamWriter file_id = new StreamWriter("./fixed_html.html", false, System.Text.Encoding.UTF8);
				fix_tags(Clipboard.GetText(), file_id);
				file_id.Close();
				System.Diagnostics.Process.Start("batch.bat");
			}
		}
	}
