Xml File Comparison C#

Posted on: 11/13/2017 / Admin
Xml File Comparison C# Average ratng: 7,4/10 8791reviews

How to Read, Write, and Delete from the Windows Registry with CIts a common requirement for a software developer to have to access the Windows Registry. Many applications store their settings within the registry, not to mention Windows itself. So in this article I will show you how to do this, and also how to create and delete registry keys. Although many applications use the registry for storing their settings and configurations, nowadays its not the recommended practise any more. Instead you should store your applications settings in an XML file such as the Settings. C project. If you dont know what the Settings. I wrote an article on it which you can read here Using the Settings file in C. Before you start testing any code which manipulates the registry be sure you know exactly what youre editing. Xml File Comparison C#' title='Xml File Comparison C#' />Changing, or even worse, deleting registry keys and values which are used by Windows can cause it to fail to load, and you would have to try to restore Windows back or maybe even reinstall it completely. So dont say you havent been warned On the positive side, the registry manipulation we will be looking at in this article will not affect Windows so it is quite safe if you follow my examples. The Registry and the Registry. Key Classes. These are the classes we will be using to manipulate the registry. They are both found under the Microsoft. Driver Via Chrome9 Hc Igp Family Wddm Windows 7 here. PDF files that contain the Visual Studio 2005 documentation. A quick cheat sheet that highlights some key syntactical differences between C and VB. NET. While this is not all inclusive this cheat sheet proves to be a handy. General. Basic general information about file comparison software. Name Creator FOSS Free First public release date Year of latest stable version. Win. 32 namespace so you will have to add it to your project to use the classes. The Registry class provides us with objects that represent the root keys in the Windows registry. UploadFile/manish1231/welcome-world-first-android-application-in-visual-studio-2010/Images/1.gif' alt='Xml File Comparison C#' title='Xml File Comparison C#' />It also contains static methods to access key and value pairs but we will not be using these in this article. The root key representations are as follows. Registry Value. Class Property. HKEYCLASSESROOT. Registry. Classes. Root. HKEYCURRENTCONFIG. Registry. Current. Config. HKEYCURRENTUSER. Registry. Current. User. KEYDYNDATA. Registry. Dyn. Data. HKEYLOCALMACHINE. Registry. Local. Machine. HKEYPERFORMANCEDATA. Registry. Performance. Data. Registry. Users. The Registry. Key class represents a key level node in the Windows registry and this is the class we will be making the most use of. Creating Keys and Values. So lets start by creating a Registry. Key object which represents HKEYLOCALMACHINE. Create an instance of HKEYLOCALMACHINE registry key. Registry. Key parent. Key Registry. Local. Machine. Next we are going to create another Registry. Key object which represents the SOFTWARE key found under HKEYLOCALMACHINE. We are passing true as the second parameter to the Open. Sub. Key method. This is to indicate that we require write access on that registry key. If we pass false, the key will be read only. Open the SOFTWARE registry sub key under the HKEYLOCALMACHINE parent key. Registry. Key software. Key parent. Key. Open. Sub. KeySOFTWARE, true. Next we are going to create our own key under the SOFTWARE key. For this example I called it Dave. On. C. Create a Dave. On. C registry sub key under the SOFTWARE key. Registry. Key sub. Key software. Key. Create. Sub. KeyDave. On. C. Now lets add a few values to our sub key Dave. On. C. Lets add a name value and two other values for storing URLs and set their value type to string. Add values to Dave. On. C sub key. sub. Key. Set. ValueName, David Azzopardi, Registry. Value. Kind. String. Key. Set. ValueWebsite. Primary, http www. Registry. Value. Kind. String. sub. Key. Set. ValueWebsite. Secondary, http www. Registry. Value. Kind. String. If you have got to this point, once you open a registry editor tool such as regedit. Dave. On. C we created and if you click on that key you will see the three values we just created as well. Now lets create a sub key under the Dave. On. C key, and lets call it Top Articles. Create a Top Articles registry sub key under the Dave. On. C key. Registry. Key articles. Sub. Key sub. Key. Create. Sub. KeyTop Articles. Next well add a few values to the Top Articles key. As you will see in the below code, we are not passing Registry. Value. Kind. String to the Set. Value method this time. This is because the Set. Value assumes the value is of type string when no Registry. Value. Kind is passed as a parameter, so we can avoid typing it. Personally I would take the extra effort and type it in full. There are two main reasons why I would do this It makes the code easier to read because nothing is open to interpretation since everything is written down. If Microsoft ever decide to change the default value type, or the meaning of the parameters accepted by the Set. Value method, the code will fail on compilation and not at runtime, therefore making it easier to spot the error. It is highly unlikely this will happen though. Add values to Top Articles sub key. Sub. Key. Set. ValueThe Message. Box Explained, http www. Sub. Key. Set. ValueFile Comparison in C Part 1, http www. Sub. Key. Set. ValueFile Comparison in C Part 2, http www. Sub. Key. Set. ValueFile Comparison in C Part 3, http www. Sub. Key. Set. ValueC Chat Application Over Asynchronous UDP Sockets Part 1, The Server, http www. Sub. Key. Set. ValueC Chat Application Over Asynchronous UDP Sockets Part 2, The Client, http www. Sub. Key. Set. ValueCreate a Screen Saver Using C Part 1, http www. Sub. Key. Set. ValueCreate a Screen Saver Using C Part 2, http www. If you were to look at the registry again from regedit. Dave. On. C called Top Articles, and it contains the values we just created above. If you export the Dave. On. C key from regedit. Windows Registry Editor Version 5. HKEYLOCALMACHINESOFTWAREDave. On. C. NameDavid Azzopardi. Website. Primaryhttp www. Website. Secondaryhttp www. HKEYLOCALMACHINESOFTWAREDave. On. CTop Articles. The Message. Box Explainedhttp www. File Comparison in C Part 1http www. File Comparison in C Part 2http www. File Comparison in C Part 3http www. C Chat Application Over Asynchronous UDP Sockets Part 1, The Serverhttp www. C Chat Application Over Asynchronous UDP Sockets Part 2, The Clienthttp www. Create a Screen Saver Using C Part 1http www. Create a Screen Saver Using C Part 2http www. Deleting Keys and Values. To delete a value is quite straight forward. Lets delete the Website. Secondary value from the Dave. On. C key. Delete the Website. Secondary value. sub. Key. Delete. ValueWebsite. Secondary. Now lets delete all that we have created so far. Delete the parent registry key. Key. Delete. Sub. Key. TreeDave. On. C. And if you open regedit. It is that easy to delete a registry key with all its values So imagine how easy it is to delete something by mistake be careful when deleting from the registry. Finally, we must remember to close all the Registry. Key objects we created. Close Registry. Key objects. Sub. Key. Close. Key. Close. software. Key. Close. parent. Key. Close. Accessing the Registry in Windows Vista. Because of the added security features in Windows Vista you cannot access the registry by just using the above code. You must add an app. I explained how to use the requested. Execution. Level section of the manifest file in my previous post Writing To The Windows Event Log Using C, so you can have a look towards the end of that article if you want to know a little bit more about the manifest file. Whats important for this example is that we must edit the requested. File Comparison in C Part 3. In this third and final article of my File Comparison in C series we will be creating a method which compares file hashes. I suggest you read File Comparison in C Part 1, and File Comparison in C Part 2 before continuing with this article, if you havent already done so. What is a File Hash First of all a hash function is a mathematical function which converts a large sized amount of data into a much smaller datum. This datum is a representation of the actual large data, so it is ideal to use for data comparisons or database lookups for example. So, a file hash would be the hash representation of a file. When you look at it, a hash is just a long numerical or alpha numerical value, such as for example these two b. B4 1. C B3 5. 4 F3 1. EE 0. D 6. A F5 7. F DA 3. F D5 4. File Comparison using Hashes. To create our file comparison method we are going to use the. NET Hash. Algorithm class. To use this class we need to add the System. Security. Cryptography namespace to our project. The below code is an example of a file hash comparison method. Compare. File. Hashesstring file. Name. 1, string file. Name. 2. Compare file sizes before continuing. If sizes are equal then compare bytes. Compare. File. Sizesfile. Name. 1, file. Name. Create an instance of System. Security. Cryptography. Hash. Algorithm. Hash. Algorithm hash Hash. Algorithm. Create. Declare byte arrays to store our file hashes. Hash. 1. byte file. Hash. 2. Open a System. IO. File. Stream for each file. Note With the using keyword the streams. File. Stream file. Stream. 1 new File. Streamfile. Name. File. Mode. Open. Stream. 2 new File. Streamfile. Name. File. Mode. Open. Compute file hashes. Hash. 1 hash. Compute. Hashfile. Stream. Hash. 2 hash. Compute. Hashfile. Stream. Bit. Converter. To. Stringfile. Hash. Bit. Converter. To. Stringfile. Hash. This method accepts two parameters, which are the full file names of the files we want to compare. In the first line of code we are calling the Compare. File. Sizes method we created in part 1 of this article. Next we are initialising the Hash. Algorithm class which will be used to generate our file hashes. We are also declaring two byte arrays which will be used to store the file hashes. Next we are creating two File. Stream objects for each of the files we passed as parameters, and finally we are using the Compute. Hash method of the Hash. Algorithm class to compute our file hashes. This method returns true if the hashes match and false if they dont. How accurate is this methodThe chances of generating two identical hashes for different files is almost impossible. A tiny change in a file results in a fairly large and unpredictable change in the generated hash. During my entire software development career, I have never encountered equal hashes for different valuesfiles. Although having said this, it is still technically possible to generate equal hashes for different files but it is so rare that it is not something to worry about when working with hashes. So which comparison technique is the best To find out I compared small and large files using the byte by byte comparison, and the file hash comparison methods we created, and I timed them. Below are the results As you can see from the results, there is practically no difference between the comparison methods for small files, but when I compared two large files of around 7. MB each you can clearly see a difference between the methods. The byte by byte method took around 2. This comparison clearly shows that the file hash method is quicker, and given the almost impossible chances of generating equal hashes for different files, I would say that the accuracy of this method is basically the same as that of the byte by byte method. Therefore, if I was creating a comparison method, I would go for the file hash comparison technique I hope you found this article series interesting. Feel free to leave any comments or if you want you can contact me through my contact page. Happy comparingDave.