String Replace/Registry Manipulation

  • Thread starter Thread starter Newbie Coder
  • Start date Start date
N

Newbie Coder

Hi all,

VB.NET 2003 ONLY PLEASE

I have a registry key (HKCU\Software\Newbie\SomeKey) of type string which
holds an XML string as follows:

<root formID="preferencesform" lang="english" ><global welcome="0"
lang="british" soundEffects="0" soundTheme="default" stayOnTop="0"
autoStart="0" /><main ><pos x="761" y="277" /><size width="240"
height="450" /></main><chat fontFamily="tahoma" fontSize="12"
inMsgColor="255" outMsgColor="3368448" /><video webcam="undefined"
/></root>

I need to read the key from the registry, replace the 'autoStart' value from
0 (zero) to 1 (one) & back using a checkbox control.

'Autostart' starts at character 135 & the zero is 11 characters on making it
char. 146. I will also need to do this for the 'stayOnTop' value too, but
both values will share the same method, just different positions.

How do I do this without creating a temporary XML document, changing it &
writing back to the registry?

TIA
 
Try to use the Replace function.

load reg value into mystring
Replace(mystring, "autoStart=" & chr(34) & "0" & chr(34), "autoStart=" &
chr(34) & "1" & chr(34))
write mystring back to reg
 
Thanks for your reply. I thought about doing that, but I was also thinking
on the lines of this:

s = "<root formID="preferencesform" lang="english" ><global welcome="0"
lang="british" soundEffects="0" soundTheme="default" stayOnTop="0"
autoStart="0" /><main ><pos x="761" y="277" /><size width="240"
height="450" /></main><chat fontFamily="tahoma" fontSize="12"
inMsgColor="255" outMsgColor="3368448" /><video
webcam="undefined"/></root>"

Dim i As Integer = s.IndexOf("autoStart") ' i = 'a' of autostart
Dim ss As String = s.Substring(i + 11, 1).Replace("0", "1")

Then concatinating the entire string again, but whereas 's' in the example
above held the compulete string at the beginning now it contains just '1'

So, how to I write it back to the entire string?

Any ideas?
 
I think that you will find that after executing those 2 statements, that s
still holds the while string (unchanged) and that ss has a length of 1 and a
value of "1".

Have you tried simply:

s = s.Replace("autoStart=""0""", "autoStart=""1"")

Also I don't really understand your resistance to using an XmlDocument like
this:

Dim _doc as New XmlDocument
_doc.LoadXml(s)
_doc.SelectSingleNode("//global").Attributes("autoStart").Value = "1"
_s = _doc.OuterXml
 
Stephany,

The registry key is from a third party application. I will release something
on the net that uses this other application, but I cannot be bothered with
creating a temp XML file just to write back the value of a check_changed
event. It seems pointless & will slow the application down
 
Stephany,

I think your idea of just replacing the entire autoStart="0"... is the
simplest, quickest solution

Thank you
 
Ummm ...

Dim _doc as New XmlDocument
_doc.LoadXml(s)
dim autostart As String =
_doc.SelectSingleNode("//global").Attributes("autoStart").Value

I don't know why you think that loading an XML string into an XmlDocument
object is creating a file. All it it doing is parsing it in memory.

In addition I don't know why you think that loading this 'tiny' XML string
into an XmlDocument object is going to create significant overhead to your
program. I'll assume that you are NOT reading this value from the registry
thousands of times in a very short space of time. I think that you will find
that the time to read the value from the registry and load it into an
XmlDocument object on reasonable hardware is less than a single millisecond.

This does beg the question: how often, during the life of an instance of
your program, does an outside entity modify the value in the registry? If
the answer is never then you only need to read it once anyway.
 
Stephany,

An OT question for me.

Why is it an XML string and an XmlDocument?

I am always automatically writing that and than correcting to

a XML string and a XmlDocument.

To make it on thread.

By the way in my idea is the distinct between a document, a file and a
string is not for everybody obvious and is it used as equivalent.

Cor
 
When I refer to an XML string, I mean a string that contains taext that is
well-formed XML, e.g.:

Dim _s As String = "<Node><<Child>ChildValue</Child></Node>"

When I refer to an XmlDocument I mean an object of type System.XmlDocument.

As you can see I was pointing out to the OP that an XmlDocument is NOT a
file, although it can be loaded from a file.

The distinction is very important and, as you are well aware, the use of
incorrect terminology can get one into a lot of trouble, especially in these
newsgroups.
 
Oh, I see what you are asking!

In English, there are 2 forms of the indefinite article, 'a' and 'an'.

The form 'an' is used when it precedes a noun that starts with a vowel
sound, regardless of whether or not it actually starts with a vowel.

Although 'X' is not vowel, it's sound is as if it was 'EX' and theefore the
'an' form of the indefinite article applies.

Just to confuse the issue, in spoken English, you often hear someone say 'an
Hotel' which, of course, appearsis incorrect. They should say 'a Hotel', but
in absolutely correct English the 'H' sound is dropped in the word 'Hotel'
and it is pronounced 'Otel'. This makes 'an Otel' correct.

In written English, however, the 'H' is not dropped from the wiord 'Hotel'
and there fore the written from should always be 'a Hotel".

Just another of the vagaries and nuances of our marvelous English language
that are there just to confuse those for whom English is not thier first
language :)

While we're on the subject and for your edification, you tend to use the
word 'than' where you should use the word 'then', and it does make reading
some of your posts difficult.

'than' is a conjunction to show comparison as in 'I am older than you'.

'then' is primarily used as an adverb and has a variety of meanings
depending on the context. On meaning is to show a progresion as in 'If
<condition> Then <statement>' which you will be familiar with.

Your statement 'I am always automatically writing that and than correcting
to ...' should have been 'I am always automatically writing that and then
correcting to ...'.

And there endeth the English lesson for the day :)
 
Stephany,,

The Than and Then is in Dutch one word (spoken not so different as in
English Than) "Dan".

I go automatically go for the "than" word when it is not the action as
result from a compare. I will change that and take it for every action.

Thank you very much.

Cor
 
It seems that the third party application isn't reading the value from the
registry because I have tested it & the Start With Windows value stays
unchecked within their application when I change the registry setting the
the XML string

The reason I am not creating the XML Doc is because I couldn't be bothered.
If I did I would have written it 3 days ago.

Anyway. The apps on hold for a few days as I have to install a graphics card
for someone, reformat a laptop for another, install a wireless system for
another, upgrade a 20 user network; workstations, servers & cabling not
forgetting the data migration etc. So, I am busy enough for a few day then I
am flying to Sweden. Will check this post as & when I have the chance

Laters
 
Back
Top