-1

I have created a Scanner application using C# in visual studio.I am trying to directly save the scan data in GUIDs as the file name

How do I generate a new GUID and then write to a new file in a specified path, using that GUID as the filename?

Thanks in Advance!

Lennart
  • 9,145
  • 15
  • 66
  • 81
Ravi Kumar
  • 23
  • 4

2 Answers2

1

Something like this

var somepath = Path.GetTempPath(); // tempFolder if you want it
var fileName = Path.Combine(somepath,$"{Guid.NewGuid()}.dat");

// Do something with your file name 

Add pepper and salt to taste


Additional resources

Path.Combine Method

Combines strings into a path.

Guid.NewGuid Method

Initializes a new instance of the Guid structure.

Example

public static void Main()
{
    Guid g;
    // Create and display the value of two GUIDs.
    g = Guid.NewGuid();
    Console.WriteLine(g);
    Console.WriteLine(Guid.NewGuid());
}

Output

0f8fad5b-d9cb-469f-a165-70867728950e
7c9e6679-7425-40de-944b-e07fc1f90ae7
TheGeneral
  • 75,627
  • 8
  • 79
  • 119
0

the Guid type has a Guid.NewGuid() method that generates a 'random' guid.

also check out this post that discusses pros and cons of newing up a guid type v using the built in newguid().

Guid.NewGuid() vs. new Guid()

jazb
  • 5,013
  • 6
  • 32
  • 42