|
Hi all,
I have a prob ,how we can read the registry keys in c# and display or write it into a text file
Please help
Thanks in advance
Rakesh
|
|
|
|
|
Do a search on the internet - you will get tons of answers.
See here[^] for the first link my search returned.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
Navigate the Given Link to open Google Search Engine for tons of Answer for your question
How to Read Registry Key[^]
Hope it Will works for you.
|
|
|
|
|
Include the required Header Files and then open ur required key
RegistryKey Key = Registry.CurrentUser;
Once Opening the key open ur sub key as needed..
RegistryKey SubKey = Key.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders");
Read the values of sub keys into string (if it is a string else required data type)
string str = SubKey.GetValue("AppData").ToString();
Then ur almost done write this str value into file stream, so that ur task is acheived....
If u like my answer.... rate it Thanks
|
|
|
|
|
Pardhu2 wrote: Include the required Header Files
This is the C# forum.
|
|
|
|
|
Here ya go. Paste this code into a class and you're all set:
using System;
using System.Linq;
using Microsoft.Win32;
using System.Collections.Generic;
namespace Marois.Common.RegistryProcedures
{
public static class RegistryProcedures
{
#region enum BaseKeys
public enum BaseKey
{
HKEY_CLASSES_ROOT,
HKEY_CURRENT_CONFIG,
HKEY_CURRENT_USER,
HKEY_DYN_DATA,
HKEY_LOCAL_MACHINE,
HKEY_PERFORMANCE_DATA
}
#endregion
#region Method CreateSubkey
public static void CreateSubkey(BaseKey BaseKey, string KeyToAdd)
{
CreateSubkey(BaseKey, "", KeyToAdd);
}
public static void CreateSubkey(BaseKey BaseKey, string SubKey, string KeyToAdd)
{
RegistryKey baseKey;
if (SubKey != "")
baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey, true);
else
baseKey = _GetRegistryBaseKey(BaseKey);
if (baseKey == null && SubKey != "")
{
throw new Exception("Registry subkey not found");
}
else
{
baseKey.CreateSubKey(KeyToAdd);
}
}
#endregion
#region Method DeleteSubkey
public static void DeleteSubkey(BaseKey BaseKey, string SubKey)
{
RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
baseKey.DeleteSubKey(SubKey);
}
#endregion
#region Method DeleteSubkeyTree
public static void DeleteSubKeyTree(BaseKey BaseKey, string sSubKey)
{
RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
baseKey.DeleteSubKeyTree(sSubKey);
}
#endregion
#region Method DeleteSubkeyTreeValues
public static void DeleteSubkeyTreeValues(BaseKey BaseKey, string sSubKey)
{
List<string> nodeNames = GetSubKeyNodeNames(BaseKey, sSubKey);
if(nodeNames.Count > 0)
{
RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
baseKey = baseKey.OpenSubKey(sSubKey, true);
foreach(string nodeName in nodeNames)
{
baseKey.DeleteValue(nodeName);
}
}
}
#endregion
#region Method DeleteValue
private static void DeleteValue(BaseKey BaseKey, string SubKey, string ValueToDelete)
{
RegistryKey baseKey;
if (SubKey != "")
baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey, true);
else
baseKey = _GetRegistryBaseKey(BaseKey);
if (baseKey == null && SubKey != "")
{
throw new Exception("Registry subkey not found");
}
else
{
baseKey.DeleteValue(ValueToDelete);
}
}
#endregion Method DeleteValue
#region Method GetSubkeyNames
public static List<string> GetSubkeyNames(BaseKey BaseKey, string SubKey)
{
List<string> retVal = null;
RegistryKey baseKey;
if (SubKey != "")
baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey);
else
baseKey = _GetRegistryBaseKey(BaseKey);
if (baseKey == null && SubKey != "")
{
throw new Exception("Registry subkey not found");
}
else
{
string[] names = baseKey.GetSubKeyNames();
retVal = (from n in names
select n).ToList();
}
return retVal;
}
#endregion
#region Method GetSubKeyNodeNames
public static List<string> GetSubKeyNodeNames(BaseKey BaseKey, string SubKey)
{
List<string> retVal = null;
RegistryKey baseKey;
if (SubKey != "")
baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey);
else
baseKey = _GetRegistryBaseKey(BaseKey);
if (baseKey == null && SubKey != "")
{
throw new Exception("Registry subkey not found");
}
else
{
string[] names = baseKey.GetValueNames();
retVal = (from n in names
select n).ToList();
}
return retVal;
}
#endregion
#region Method GetSubKeyValue
public static object GetSubKeyValue(BaseKey BaseKey, string SubKey, string Node)
{
object retVal = null;
RegistryKey baseKey;
if (SubKey != "")
baseKey = _GetRegistryBaseKey(BaseKey).OpenSubKey(SubKey);
else
baseKey = _GetRegistryBaseKey(BaseKey);
if (baseKey == null && SubKey != "")
{
throw new Exception("Registry subkey not found");
}
else
{
retVal = baseKey.GetValue(Node);
}
return retVal;
}
#endregion
#region Method SetKeyValue
public static void SetKeyValue(BaseKey BaseKey, string SubKey, string NodeName, string Value)
{
RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
baseKey = baseKey.OpenSubKey(SubKey, true);
if (baseKey == null && SubKey != "")
{
throw new Exception("Registry subkey not found");
}
else
{
baseKey.SetValue(NodeName, Value);
}
}
public static void SetKeyValue(BaseKey BaseKey, string SubKey, string NodeName, int Value)
{
RegistryKey baseKey = _GetRegistryBaseKey(BaseKey);
baseKey = baseKey.OpenSubKey(SubKey, true);
if (baseKey == null && SubKey != "")
{
throw new Exception("Registry subkey not found");
}
else
{
baseKey.SetValue(NodeName, Value);
}
}
#endregion
#region Method _GetRegistryBaseKey
private static RegistryKey _GetRegistryBaseKey(BaseKey BaseKey)
{
RegistryKey key = null; ;
switch (BaseKey)
{
case BaseKey.HKEY_CLASSES_ROOT:
key = Registry.ClassesRoot;
break;
case BaseKey.HKEY_CURRENT_CONFIG:
key = Registry.CurrentConfig;
break;
case BaseKey.HKEY_CURRENT_USER:
key = Registry.CurrentUser;
break;
case BaseKey.HKEY_DYN_DATA:
key = Registry.DynData;
break;
case BaseKey.HKEY_LOCAL_MACHINE:
key = Registry.LocalMachine;
break;
case BaseKey.HKEY_PERFORMANCE_DATA:
key = Registry.PerformanceData;
break;
}
return key;
}
#endregion
}
}
Everything makes sense in someone's mind
|
|
|
|
|
I have a ListView control with 4 columns set to details view. I have implemented a column sort so the user can click a column header and resort the ListView on the desired column. The default sort is column 0.
When the default view is displayed, the user can give the ListView focus, start typing, and ListView will automatically scroll the list finding matches to what is typed. Perfect!
My problem is, when they change the sort column to some other column other than the first. Typing always searches the first column only.
Does anybody know if I can change what column is searched when the keyboard is typed on a ListView control? If so, how?
|
|
|
|
|
Hi, guys .
I write a simple application for drawing a vector primitives such a ellipse, line, rectangle, polygon, etc. I want to add a class for a new shape - ellipse with two lines in it, but I'm stuck on this . Can you give me some help?
This is the code of my Ellipse class:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace _2D_Vector_Graphics
{
[Serializable]
class EllipseShape : Shape
{
public RectangleF SetLocation
{
set
{
this.Location = value.Location;
this.ModelSize = value.Size;
}
}
public EllipseShape()
{
this.ModelSize = new SizeF(0, 0);
this.Location = new PointF(-10, -10);
this.selectionUnit = new CoveringRectangle(Rectangle.Round(ReturnBounds()));
}
public EllipseShape(Color newFillColor, Color newBorderColor, int newBorderWidth, SizeF newModelSize, PointF newlocation)
{
this.FillColor = newFillColor;
this.BorderColor = newBorderColor;
this.BorderWidth = newBorderWidth;
this.ModelSize = newModelSize;
this.selectionUnit = new CoveringRectangle(Rectangle.Round(ReturnBounds()));
}
public override void DrawYourSelf(Graphics graphics)
{
GraphicsPath path=new GraphicsPath();
path.AddEllipse(new RectangleF(Location, ModelSize));
path.Transform(this.TMatrix.TransformationMatrix);
Pen pen = new Pen(this.BorderColor, this.BorderWidth);
if (IS_FILLED)
{
SolidBrush brush = new SolidBrush(this.FillColor);
graphics.FillPath(brush, path);
}
graphics.DrawPath(pen,path);
if (this.Selected)
{
this.selectionUnit = new CoveringRectangle(Rectangle.Round(ReturnBounds()));
this.selectionUnit.DrawYourSelf(graphics);
}
}
public override RectangleF ReturnBounds()
{
GraphicsPath path = new GraphicsPath();
path.AddEllipse(new RectangleF(Location, ModelSize));
path.Transform(this.TMatrix.TransformationMatrix);
return path.GetBounds();
}
}
}
And this is the code of my Line class:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace _2D_Vector_Graphics
{
[Serializable]
class LineShape : Shape
{
protected Point begin,end;
public Point SetBegin
{
set
{
begin= value ;
}
}
public Point SetEnd
{
set
{
end = value;
}
}
public LineShape()
{
this.selectionUnit = new CoveringRectangle(Rectangle.Round(ReturnBounds()));
}
public LineShape(Point begin, Point end)
{
this.begin = begin;
this.end = end;
this.selectionUnit = new CoveringRectangle(Rectangle.Round(ReturnBounds()));
}
public LineShape(Point begin, Point end, Color newBorderColor, int newBorderWidth)
{
this.begin = begin;
this.end = end;
this.BorderColor = newBorderColor;
this.BorderWidth = newBorderWidth;
this.selectionUnit = new CoveringRectangle(Rectangle.Round(ReturnBounds()));
}
public override void DrawYourSelf(Graphics graphics)
{
GraphicsPath path = new GraphicsPath();
path.AddLine(begin,end);
path.Transform(this.TMatrix.TransformationMatrix);
Pen pen = new Pen(this.BorderColor, this.BorderWidth);
graphics.DrawPath(pen, path);
if (this.Selected)
{
this.selectionUnit = new CoveringRectangle(Rectangle.Round(ReturnBounds()));
this.selectionUnit.DrawYourSelf(graphics);
}
}
public override RectangleF ReturnBounds()
{
GraphicsPath path = new GraphicsPath();
path.AddLine(begin, end);
path.Transform(this.TMatrix.TransformationMatrix);
return path.GetBounds();
}
}
}
|
|
|
|
|
You have an ellipse class and a line class, so you have everything you need to work with.
If you want an ellipse with two lines in it as a separate object, simply create a new third class that has an ellipse and 2 line objects as members and simply iterate over the 3 objects in the DrawYourself method.
You'll have to do some min/max calculations to do the ReturnBounds method, but that would be how I would approach it.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Can you show me with some code how to do because I could not do it.
|
|
|
|
|
Did you write the code above? Because if you did, you would know very easily how to do this.
If this is a homework assignment of some kind, then you need to make an honest attempt.
Try writing a new class as I described above and then ask questions based on that. You'll get loads of support here if you approach it that way. Asking us to simply give you the answer teaches you nothing and at some point, some senior dev out there who will mentor you in the workforce later on will curse us for doing so if we did.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Hi,
I am building a simple windows phone 7 app. I need to pass more that one data item between pages. I know you need to make a small change to sending one data item across but im not sure what this change is. Currently I send one item across like this:
phoneApplicationPage.NavigationService.Navigate(new Uri("/Pages/View_Stickies.xaml?string=" + info, UriKind.RelativeOrAbsolute));
Where in this case info is the string sent.
I unpack it on page 2 like this:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (this.NavigationContext.QueryString.ContainsKey("string"))
{
string GPS = this.NavigationContext.QueryString["string"];
MessageBox.Show(GPS);
}
base.OnNavigatedTo(e);
}
Im sure there is a very simple answer to the question and any help would be great.
Thanks
|
|
|
|
|
As you are passing a query string across, all you need to do is add the second key value pair after a &, so your example could become:
string.Format("/Pages/View_Stickies.xaml?string={0}&id={1}", info, id)
|
|
|
|
|
|
|
Hi ,
I have array and I am try to read as a block where block start from 55 55 and end with 204 204 and within the start and the end I am read message where each message should start with 99 and end with 13 (all as a Hex) and if the message length is 30 I will do operation and if it is 6 I will do another operation.
but I am receive some exceptions when I read different bytes from different bytes.
Here's a sample from different blocks ( Which Stored In arraylist called :VopMessageDetails)
55 55 1C 09 0A 0B
99 1E 3F 41 56 42 30 31 31 30 31 31 4E 4E 20 20 30 31 30 20 20 20 20 01 08 42 0B 59 CF 0D
99 1E 3F 41 56 42 30 32 31 30 31 32 4E 4E 20 20 30 32 30 20 20 20 31 02 08 93 0D D3 CE 0D
99 0A 41 2F 01 08 E7 03 00 0D
99 1E 3F 41 56 43 30 33 31 30 31 32 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D
99 1E 3F 41 56 41 31 35 31 30 31 32 4E 4E 20 20 31 35 30 20 20 20 20 04 08 00 00 00 00 0D
CC CC
==============================================
55 55 1C 09 0A 0C
CC CC
============================================
55 55 1C 09 0A 0D
55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30
32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32
1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC
99 0A 41 2F 02 08 78 03 00 0D
CC CC
===========================================
55 55 1C 09 0A 0E
99 1E 3F 41 56 41 31 33 31 30 31 35 4E 4E 20 20 31 33 30 20 20 20 20 01 08 A0 0B F7 DB 0D
99 0A 41 2F 01 08 E7 03 00 0D
99 1E 3F 41 56 42 30 31 31 30 31 35 4E 4E 20 20 30 31 30 20 20 20 20 02 08 42 0B 59 CF 0D
99 0A 41 2F 02 08 E7 03 00 0D
99 1E 3F 41 56 43 30 33 31 30 31 35 4E 4E 20 20 30 33 30 20 20 20 20 03 08 58 0F 98 CE 0D
99 0A 41 2F 03 08 E7 03 00 0D
99 1E 3F 41 56 41 30 34 31 30 31 35 4E 4E 20 20 30 34 30 20 20 20 20 04 08 DB 10 C5 CE 0D
99 0A 41 2F 04 08 E7 03 00 0D
CC CC
================================
55 55 16 0C 0D 2B 99 1E 3F 41 56 41 30 36 31 33
34 33 4E 4E 20 20 30 36 30 20 20 20 20 01 08 C8
11 4E D2 0D 99 1E 3F 48 56 41 30 31 31 33 34 34
59 59 53 34 30 34 30 43 4C 59 36 01 00 0E 1F 2C
DA 0D 99 1E 3F 42 56 41 31 33 31 33 34 35 4E 4E
20 20 31 33 30 20 20 20 20 01 0A D4 0E 48 DE 0D
CC CC 55 55 16 0C 0E 13 99 1E 3F 41 56 41 30 33
31 34 31 39 4E 4E 20 20 31 33 30 20 20 4E 20 01
08 92 0C 64 D4 0D 99 1E 3F 41 56 41 30 38 31 34
31 39 4E 4E 20 20 30 38 30 20 20 20 20 02 08 90
11 EA D6 0D CC CC
|
|
|
|
|
and here's My code
<br />
#region 1: Count Numbers of blocks.<br />
int blockCounter = 0;<br />
int countNumberOfBytes = 0;<br />
for (int i = 0; i < VopMessageDetails.Count; i++)<br />
{<br />
if (VopMessageDetails[countNumberOfBytes].ToString() == "85")<br />
{<br />
<br />
}<br />
else if (VopMessageDetails[countNumberOfBytes].ToString() == "204" & VopMessageDetails[countNumberOfBytes - 1].ToString() == "204")<br />
{<br />
blockCounter++;<br />
}<br />
countNumberOfBytes++;<br />
<br />
}<br />
Console.WriteLine(blockCounter);<br />
#endregion<br />
<br />
<br />
<br />
<br />
<br />
List<string>[] catchBlockBytes = new List<string>[blockCounter];<br />
blockCounter = 0;<br />
catchBlockBytes[blockCounter] = new List<string>();<br />
<br />
int PrevBlockCounter = 0;<br />
<br />
for (int k = 0; k < VopMessageDetails.Count; k++)<br />
{<br />
if (VopMessageDetails[0 + PrevBlockCounter].ToString() == "85" & VopMessageDetails[1 + PrevBlockCounter].ToString() == "85")<br />
{<br />
if (VopMessageDetails[k].ToString() != "204")<br />
{<br />
catchBlockBytes[blockCounter].Add(VopMessageDetails[k].ToString());<br />
}<br />
else<br />
{<br />
PrevBlockCounter = k + 2;<br />
k++;<br />
<br />
if (VopMessageDetails[PrevBlockCounter - 1].ToString() == "204" & VopMessageDetails[PrevBlockCounter - 2].ToString() == "204")<br />
{<br />
blockCounter++;<br />
}<br />
<br />
<br />
if (k != VopMessageDetails.Count - 1)<br />
{<br />
catchBlockBytes[blockCounter] = new List<string>(); <br />
}<br />
<br />
}<br />
}<br />
<br />
<br />
|
|
|
|
|
Why are you converting byte values to strings to test for numeric equality? Just test against the decimal or hex value.
And please use <pre></pre> tags not <code></code> to surround your code blocks; that is why the button is called "code block".
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
Hi,
your code is hard to read, you should use PRE tags instead of CODE tags, as that would preserve formatting and indentation.
You also should HTML-escape the < and > and & signs, possibly using the widgets the edit page offers. That way the generic lists would show properly. You could still edit and improve the existing message.
Here are some comments:
1.
When the data is numeric, it is just silly to convert it to string for comparison purposes. You really should be using something like
if (myIntOrMyByte==0x55)...
and not mix decimal and hexadecimal all the time. C# knows about hex literals!
2.
Is this an official standard you're implementing? I'm not familiar with double 0x55 and double 0xCC as special markers. And what makes one so sure they will never appear in the data?
3.
If you're getting exceptions, you should watch the stack traceback; for a debug build it holds class names, file names *and* line numbers, pointing to the exact line that bombs. And if you can't figure it out, show the exact exception message and indicate the line where it happens.
|
|
|
|
|
What exceptions are you receiving? If you could elaborate it may help get the root of the problem.
"Simplicity carried to the extreme becomes elegance."
-Jon Franklin
|
|
|
|
|
when I have block start with 55 55 but not content any message and end with with 204 204
like this
55 55 1C 09 0A 0C
CC CC
and like those
55 55 1B 0B 0A 17 99 1E 3F 48 56 41 30 35 31 30
32 36 4E 4E 20 20 30 35 30 20 20 20 20 01 00 32
1E F4 D5 0D CC CC 55 55 1B 0B 0B 04 CC CC
99 0A 41 2F 02 08 78 03 00 0D
CC CC
|
|
|
|
|
Yes that's fine.
But what error message or exception do you get?
Or do you not get an error where you expected one?
If you won't help us, we can't help you.
What is the error?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Let me get this straight, because I think there are language barriers here. In reference to your example data... a 'BLOCK' is considered everything between 0x55 and 0xCC right? If this is the case, then a couple of your example data sets have multiple blocks. Luc is right in his previous post that data has to escape the fencepost identifiers if they appear inside the payload. And the issue is that your dividing into BLOCKS is not working as expected? No exceptions, just not valid results, ya?
"Simplicity carried to the extreme becomes elegance."
-Jon Franklin
|
|
|
|
|
1) The first 'for' loop does not use its indexer. You can easily change the 'for' loop to a 'foreach' loop and iterate through the bytes. This will simplify your code a little.
2) Do comparisons of bytes, don't convert to strings.
3) If you're working in hex numbers use hex numbers throughout your code, don't mix up number bases. This will make your code easier to understand.
4) Your logical comparisons should use the logical && operator and not the & operator. The & operator returns a bitwise operation resulting in a number, not true or false. They are VERY different.
5) You should always put magic numbers in your code as constants.
public const Byte StartDelimiter = 0x55;
You should never sprinkle those into code manually, you run a huge risk if you are forced to refactor your work. Not to mention that you may mistype one.
6) I would avoid doing tricks with your loop variable like incrementing/decrementing it inside your loop block unless it is absolutely necessary - which should be never.
7) Match your braces. Just because you don't necessarily have to put them in after an 'if' statement doesn't mean you shouldn't. IMHO.
8) Do yourself a favor and look up the State Machine design pattern, it works very well for these kinds of problems.
9) When I ran your program (with healthy fixups) I found that you presumed that every 0xCC,0xCC was an end delimiter. This, apparently, is a false presumption. CCCC appears in your third dataset as valid information, and should not be treated as a fencepost delimiter. fix that up and I think you should get valid results, or at least get you further along.
Regards,
"Simplicity carried to the extreme becomes elegance."
-Jon Franklin
|
|
|
|
|
This is one of the best, and most thorough answers I have ever read, and I agree with every bit of it. I even learned a bit. Thanks!
Will Rogers never met me.
|
|
|
|
|