#pragma once

namespace Registry1 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace Microsoft::Win32;
	using namespace System::Security::Permissions;

	/// <summary>
	/// Zusammenfassung für Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Konstruktorcode hier hinzufügen.
			//
		}

	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	internal: System::Windows::Forms::Button^  Button3;
	protected: 
	internal: System::Windows::Forms::Button^  Button2;
	internal: System::Windows::Forms::Button^  Button1;

	private:
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Erforderliche Methode für die Designerunterstützung.
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
		/// </summary>
		void InitializeComponent(void)
		{
			this->Button3 = (gcnew System::Windows::Forms::Button());
			this->Button2 = (gcnew System::Windows::Forms::Button());
			this->Button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// Button3
			// 
			this->Button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->Button3->Location = System::Drawing::Point(56, 174);
			this->Button3->Name = L"Button3";
			this->Button3->Size = System::Drawing::Size(173, 23);
			this->Button3->TabIndex = 8;
			this->Button3->Text = L"Existiert der Registry-Schlüssel\?";
			this->Button3->UseVisualStyleBackColor = true;
			this->Button3->Click += gcnew System::EventHandler(this, &Form1::Button3_Click);
			// 
			// Button2
			// 
			this->Button2->Location = System::Drawing::Point(56, 120);
			this->Button2->Name = L"Button2";
			this->Button2->Size = System::Drawing::Size(173, 23);
			this->Button2->TabIndex = 7;
			this->Button2->Text = L"Registry-Schlüssel entfernen";
			this->Button2->UseVisualStyleBackColor = true;
			this->Button2->Click += gcnew System::EventHandler(this, &Form1::Button2_Click);
			// 
			// Button1
			// 
			this->Button1->Location = System::Drawing::Point(56, 66);
			this->Button1->Name = L"Button1";
			this->Button1->Size = System::Drawing::Size(173, 23);
			this->Button1->TabIndex = 6;
			this->Button1->Text = L"Registry-Schlüssel hinzufügen";
			this->Button1->UseVisualStyleBackColor = true;
			this->Button1->Click += gcnew System::EventHandler(this, &Form1::Button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Controls->Add(this->Button3);
			this->Controls->Add(this->Button2);
			this->Controls->Add(this->Button1);
			this->Name = L"Form1";
			this->Text = L"Windows Registry";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
					try
					{
						RegistryKey^ regkey;
						regkey = Registry::LocalMachine->OpenSubKey("SOFTWARE\\Meine Anwendung");

						if(regkey == nullptr)
							SchluesselAnlegen();
						else
						{
							DateTime ablauf = DateTime::Parse(regkey->GetValue("Limit")->ToString());
							DateTime heute = DateTime::Today;
							if(heute > ablauf.AddMonths(6))
							{
								MessageBox::Show("Ihre Nutzungszeit ist abgelaufen. Schalten Sie das Programm unter ... frei.");
								Application::Exit();
							}
						}
					}
					catch(Exception^ e)
					{
						MessageBox::Show(e->Message);
						Application::Exit();
					}
				}


	private: bool SchluesselAnlegen()
				{
					try
					{
						RegistryKey^ regkey;
						//Registrierungsschlüssel anlegen unter HKEY_LOCAL_MACHINE\SOFTWARE
						regkey = Registry::LocalMachine->CreateSubKey("SOFTWARE\\Meine Anwendung");
						//Zeichenfolgen und Werte anlegen
						regkey->SetValue("", "1");
						regkey->SetValue("Limit", DateTime::Today.ToString());
						regkey->Close();
						return true;
					}
					catch(Exception^ e)
					{
						MessageBox::Show(e->Message);
						Application::Exit();
					}
				}

				//Registry-Schlüssel anlegen
	private: System::Void Button1_Click(System::Object^  sender, System::EventArgs^  e) {
					try
					{
						RegistryKey^ regkey = Registry::LocalMachine->OpenSubKey("SOFTWARE\\Meine Anwendung", RegistryKeyPermissionCheck::ReadWriteSubTree);
						regkey->CreateSubKey("IrgendeinSchlüssel");
						regkey->Close();
					}
					catch(Exception^ e)
					{
						MessageBox::Show(e->Message);
					}
				}

				//Registry-Schlüssel entfernen
	private: System::Void Button2_Click(System::Object^  sender, System::EventArgs^  e) {
					try
					{
						RegistryKey^ regkey = Registry::LocalMachine->OpenSubKey("SOFTWARE\\Meine Anwendung", RegistryKeyPermissionCheck::ReadWriteSubTree);
						if(regkey->OpenSubKey("IrgendeinSchlüssel", RegistryKeyPermissionCheck::ReadWriteSubTree))
							regkey->DeleteSubKey("IrgendeinSchlüssel");						
						else
						{
							MessageBox::Show("Der Schlüssel existiert nicht");							
						}
					}
					catch(Exception^ e)
					{
						MessageBox::Show(e->Message);
					}
				}

				//Registry-Schlüssel anzeigen, falls vorhanden
	private: System::Void Button3_Click(System::Object^  sender, System::EventArgs^  e) {
					try
					{
						RegistryKey^ regkey = Registry::LocalMachine->OpenSubKey("SOFTWARE\\Meine Anwendung\\IrgendeinSchlüssel", RegistryKeyPermissionCheck::ReadSubTree);
						if(regkey == nullptr)
							MessageBox::Show("Der Schlüssel existiert nicht");							
						else
						{
							MessageBox::Show(regkey->Name);					
						}
					}
					catch(Exception^ e)
					{
						MessageBox::Show(e->Message);
					}
				}
	};
}

