Generation of UUID

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to generates UUID with -s option and copy it to a file. I need to do this programmtically i.e through my application this should get generated and get copied to desired file. Can anyone suggest how can this be done in C#?
 
Harry,

Not sure what you mean with the -s option. Besides from .NET, I've only
created UUID values
with the UuidCreate API and it does not take any parameters. However, this
might get you started
anyway. You can use the Guid structure in .NET like this

Guid myGuid = Guid.NewGuid();
StreamWriter writer = new StreamWriter(@"myFile.txt")
stream.WriteLine(myGuid.ToString());
writer.Flush();
writer.Close();

You could use the overload of the ToString() method on the Guid struct to
format the Guid when it
is converted to a string, the following formatter characters are available

- N xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- D xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
- B {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
- P (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

So to format it with, for example, P you would do

stream.WriteLine(myGuid.ToString("P"));

HTH,

//Andreas

harry said:
I need to generates UUID with -s option and copy it to a file. I need to
do this programmtically i.e through my application this should get generated
and get copied to desired file. Can anyone suggest how can this be done in
C#?
 
Hi Andrea
Thank you very much for you help
Actually by "-s" i meant that if you type uuidgen /? in the command promt then you will see the options that can be used along with this command
on typing "C:\uuidgen -s" you will get the GUID in the following format
"INTERFACENAME = { /* 12ab2558-f953-43db-bba5-4cb6e8e804c0 *
0x12ab2558
0xf953
0x43db
{0xbb, 0xa5, 0x4c, 0xb6, 0xe8, 0xe8, 0x04, 0xc0
};" were you see "0x" appended to GUID

My question was
How can we do create process in C# as we do in C++ by using CreateProcess API
Guid.NewGuid(); doesn't generates the GUID in the format which i want i.e with "0x" appended.
 
How can we do create process in C# as we do in C++ by using CreateProcess API?

System.Diagnostics.Process.Start()

But launching Uuidgen just to get the right formatting, if that's what
you have in mind, seems a bit extreme.

Guid.NewGuid(); doesn't generates the GUID in the format which i want i.e with "0x" appended.

It's trivial to do your own formatting. For example

byte[] guidBytes = Guid.NewGuid().ToByteArray();
Console.WriteLine(
"{{ 0x{0:x8}, 0x{1:x4}, 0x{2:x4}, {{ 0x{3:x2}, 0x{4:x2}, 0x{5:x2},
0x{6:x2}, 0x{7:x2}, 0x{8:x2}, 0x{9:x2}, 0x{10:x2} }} }}",
BitConverter.ToUInt32( guidBytes, 0 ),
BitConverter.ToUInt16( guidBytes, 4 ),
BitConverter.ToUInt16( guidBytes, 6 ),
guidBytes[8], guidBytes[9], guidBytes[10], guidBytes[11],
guidBytes[12], guidBytes[13], guidBytes[14], guidBytes[15] );



Mattias
 
Back
Top