Permalink
Browse files

Fixed Registry keys checks

Registry key to sc1 are added upon user selecting dir
Added Dialog box to confirm download
  • Loading branch information...
1 parent afbc7ba commit 765a6f6ac8f0edbab24868c727d75033ea26344c @pooslice pooslice committed Mar 16, 2010
Showing with 350 additions and 13 deletions.
  1. +30 −3 MapLoader/Form1.cs
  2. +3 −1 MapLoader/MapLoader.csproj
  3. +259 −0 MapLoader/ModifyRegistry.cs
  4. +58 −9 MapLoader/PathFinder.cs
View
@@ -8,6 +8,8 @@
using System.Windows.Forms;
using System.Net;
using System.IO;
+using Microsoft.Win32;
+using Utility.ModifyRegistry;
namespace MapLoader
{
@@ -25,6 +27,7 @@ public Form1()
private void Form1_Load(object sender, EventArgs e)
{
+
// ==========================================================
// Reading XML Extension Config
// ==========================================================
@@ -35,6 +38,7 @@ private void Form1_Load(object sender, EventArgs e)
// Find Pathes for Game Folders etc.
// ==========================================================
pathFinder = new PathFinder();
+
pathFinder.GatherPathes();
pathUser = new PathUserDefined();
@@ -69,7 +73,18 @@ private void Form1_Load(object sender, EventArgs e)
if (extConf.IsValidExtension(extension))
{
lblFilename.Text = fileName; // Display filename
- bgWorker.RunWorkerAsync();
+
+ //ask user if they want to download file
+ if (MessageBox.Show("Do you want to download " + fileName, "Confirm download", MessageBoxButtons.YesNo) == DialogResult.Yes)
+ {
+ // a 'DialogResult.Yes' value was returned from the MessageBox
+ bgWorker.RunWorkerAsync();
+ }
+ else
+ {
+ Application.Exit();
+ }
+
}
else
@@ -78,7 +93,10 @@ private void Form1_Load(object sender, EventArgs e)
// Either link is broken or someone tampered with the XML
// Quit application, maybe should do something better, like pop-up a message, but
- // that would be annoying too.. so lets just exit here
+ // that would be annoying too.. so lets just exit here...
+ // or not. waiting for program to do nothing then quit is stupid
+ // show user a popup!
+ MessageBox.Show("Timeout!", "Timeout!", MessageBoxButtons.OK);
Application.Exit();
}
}
@@ -203,6 +221,15 @@ private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventA
if (realPath == "")
{
realPath = pathUser.QueryUserForPath(this, pathIdentfier);
+
+ if (realPath != "")
+ {
+ //Save path to registry
+ ModifyRegistry mysc1Registry = new ModifyRegistry();
+ mysc1Registry.BaseRegistryKey = Registry.CurrentUser;
+ mysc1Registry.SubKey = "Software\\Blizzard Entertainment\\Starcraft\\";
+ mysc1Registry.Write("InstallPath", realPath);
+ }
}
// 4. Still no path?? Okay lets just save the file somewhere else
@@ -233,7 +260,7 @@ private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventA
// be gone!
- Application.Exit();
+ // Application.Exit();
}
}
}
@@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
- <ProductVersion>9.0.30729</ProductVersion>
+ <ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B0D33570-5487-4F92-AC9F-D568215045BA}</ProjectGuid>
<OutputType>WinExe</OutputType>
@@ -55,12 +55,14 @@
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
+ <Compile Include="ModifyRegistry.cs" />
<Compile Include="PathFinder.cs" />
<Compile Include="PathUserDefined.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
+ <SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
View
@@ -0,0 +1,259 @@
+/* ***************************************
+ * ModifyRegistry.cs
+ * ---------------------------------------
+ * a very simple class
+ * to read, write, delete and count
+ * registry values with C#
+ * ---------------------------------------
+ * if you improve this code
+ * please email me your improvement!
+ * ---------------------------------------
+ * by Francesco Natali
+ * - fn.varie@libero.it -
+ * ***************************************/
+
+using System;
+// it's required for reading/writing into the registry:
+using Microsoft.Win32;
+// and for the MessageBox function:
+using System.Windows.Forms;
+
+namespace Utility.ModifyRegistry
+{
+ /// <summary>
+ /// An useful class to read/write/delete/count registry keys
+ /// </summary>
+ public class ModifyRegistry
+ {
+ private bool showError = false;
+ /// <summary>
+ /// A property to show or hide error messages
+ /// (default = false)
+ /// </summary>
+ public bool ShowError
+ {
+ get { return showError; }
+ set { showError = value; }
+ }
+
+ private string subKey = "SOFTWARE\\" + Application.ProductName.ToUpper();
+ /// <summary>
+ /// A property to set the SubKey value
+ /// (default = "SOFTWARE\\" + Application.ProductName.ToUpper())
+ /// </summary>
+ public string SubKey
+ {
+ get { return subKey; }
+ set { subKey = value; }
+ }
+
+ private RegistryKey baseRegistryKey = Registry.LocalMachine;
+ /// <summary>
+ /// A property to set the BaseRegistryKey value.
+ /// (default = Registry.LocalMachine)
+ /// </summary>
+ public RegistryKey BaseRegistryKey
+ {
+ get { return baseRegistryKey; }
+ set { baseRegistryKey = value; }
+ }
+
+ /* **************************************************************************
+ * **************************************************************************/
+
+ /// <summary>
+ /// To read a registry key.
+ /// input: KeyName (string)
+ /// output: value (string)
+ /// </summary>
+ public string Read(string KeyName)
+ {
+ // Opening the registry key
+ RegistryKey rk = baseRegistryKey;
+ // Open a subKey as read-only
+ RegistryKey sk1 = rk.OpenSubKey(subKey);
+
+ // If the RegistrySubKey doesn't exist -> (null)
+ if ( sk1 == null )
+ {
+ return null;
+ }
+ else
+ {
+ try
+ {
+ // If the RegistryKey exists I get its value
+ // or null is returned.
+ return (string)sk1.GetValue(KeyName.ToUpper());
+ }
+ catch (Exception e)
+ {
+ // AAAAAAAAAAARGH, an error!
+ ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
+ return null;
+ }
+ }
+ }
+
+ /* **************************************************************************
+ * **************************************************************************/
+
+ /// <summary>
+ /// To write into a registry key.
+ /// input: KeyName (string) , Value (object)
+ /// output: true or false
+ /// </summary>
+ public bool Write(string KeyName, object Value)
+ {
+ try
+ {
+ // Setting
+ RegistryKey rk = baseRegistryKey ;
+ // I have to use CreateSubKey
+ // (create or open it if already exits),
+ // 'cause OpenSubKey open a subKey as read-only
+ RegistryKey sk1 = rk.CreateSubKey(subKey);
+ // Save the value
+ sk1.SetValue(KeyName.ToUpper(), Value);
+
+ return true;
+ }
+ catch (Exception e)
+ {
+ // AAAAAAAAAAARGH, an error!
+ ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
+ return false;
+ }
+ }
+
+ /* **************************************************************************
+ * **************************************************************************/
+
+ /// <summary>
+ /// To delete a registry key.
+ /// input: KeyName (string)
+ /// output: true or false
+ /// </summary>
+ public bool DeleteKey(string KeyName)
+ {
+ try
+ {
+ // Setting
+ RegistryKey rk = baseRegistryKey ;
+ RegistryKey sk1 = rk.CreateSubKey(subKey);
+ // If the RegistrySubKey doesn't exists -> (true)
+ if ( sk1 == null )
+ return true;
+ else
+ sk1.DeleteValue(KeyName);
+
+ return true;
+ }
+ catch (Exception e)
+ {
+ // AAAAAAAAAAARGH, an error!
+ ShowErrorMessage(e, "Deleting SubKey " + subKey);
+ return false;
+ }
+ }
+
+ /* **************************************************************************
+ * **************************************************************************/
+
+ /// <summary>
+ /// To delete a sub key and any child.
+ /// input: void
+ /// output: true or false
+ /// </summary>
+ public bool DeleteSubKeyTree()
+ {
+ try
+ {
+ // Setting
+ RegistryKey rk = baseRegistryKey ;
+ RegistryKey sk1 = rk.OpenSubKey(subKey);
+ // If the RegistryKey exists, I delete it
+ if ( sk1 != null )
+ rk.DeleteSubKeyTree(subKey);
+
+ return true;
+ }
+ catch (Exception e)
+ {
+ // AAAAAAAAAAARGH, an error!
+ ShowErrorMessage(e, "Deleting SubKey " + subKey);
+ return false;
+ }
+ }
+
+ /* **************************************************************************
+ * **************************************************************************/
+
+ /// <summary>
+ /// Retrive the count of subkeys at the current key.
+ /// input: void
+ /// output: number of subkeys
+ /// </summary>
+ public int SubKeyCount()
+ {
+ try
+ {
+ // Setting
+ RegistryKey rk = baseRegistryKey ;
+ RegistryKey sk1 = rk.OpenSubKey(subKey);
+ // If the RegistryKey exists...
+ if ( sk1 != null )
+ return sk1.SubKeyCount;
+ else
+ return 0;
+ }
+ catch (Exception e)
+ {
+ // AAAAAAAAAAARGH, an error!
+ ShowErrorMessage(e, "Retriving subkeys of " + subKey);
+ return 0;
+ }
+ }
+
+ /* **************************************************************************
+ * **************************************************************************/
+
+ /// <summary>
+ /// Retrive the count of values in the key.
+ /// input: void
+ /// output: number of keys
+ /// </summary>
+ public int ValueCount()
+ {
+ try
+ {
+ // Setting
+ RegistryKey rk = baseRegistryKey ;
+ RegistryKey sk1 = rk.OpenSubKey(subKey);
+ // If the RegistryKey exists...
+ if ( sk1 != null )
+ return sk1.ValueCount;
+ else
+ return 0;
+ }
+ catch (Exception e)
+ {
+ // AAAAAAAAAAARGH, an error!
+ ShowErrorMessage(e, "Retriving keys of " + subKey);
+ return 0;
+ }
+ }
+
+ /* **************************************************************************
+ * **************************************************************************/
+
+ private void ShowErrorMessage(Exception e, string Title)
+ {
+ if (showError == true)
+ MessageBox.Show(e.Message,
+ Title
+ ,MessageBoxButtons.OK
+ ,MessageBoxIcon.Error);
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 765a6f6

Please sign in to comment.