How can I create a temp file with a specific extension with .NET?
Guaranteed to be (statistically) unique:
string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".csv";
(To quote from the wiki article on the probabilty of a collision:
...one's annual risk of being hit by a
meteorite is estimated to be one
chance in 17 billion [19], that means
the probability is about 0.00000000006
(6 × 10−11), equivalent to the odds of
creating a few tens of trillions of
UUIDs in a year and having one
duplicate. In other words, only after
generating 1 billion UUIDs every
second for the next 100 years, the
probability of creating just one
duplicate would be about 50%. The
probability of one duplicate would be
about 50% if every person on earth
owns 600 million UUIDs
EDIT: Please also see JaredPar's comments.
Generate a unique temporary file name with a given extension using .NET
Please See: How can I create a temp file with a specific extension with .net ?
How do I create a temp file in a directory other than temp?
Path.Combine(directoryYouWantTheRandomFile, Path.GetRandomFileName())
Is there a way to execute an exe stored with a temp file name?
I think it is possible.. set the Process.StartInfo.UseShellExecute property to False prior to starting the process, e.g.:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"c:\tmp\a123.tmp";
p.StartInfo.UseShellExecute = false;
p.Start();
This will start the process directly and the file should be considered to be executable itself. Does it work for you?
Writing temp files
You should be able to do this without creating a temporary file.
Just create $img
like:
$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]::new($User.thumbnailPhoto))
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
Don't forget to remove the form from memory after closing with $form.Dispose()
If you insist on using a temporary file, then be aware that the $img
object keeps a reference to the file untill it is disposed of.
Something like:
# get a temporary file name
$Filename = [System.IO.Path]::GetTempFileName()
[System.IO.File]::WriteAllBytes($Filename, $User.thumbnailPhoto)
# get an Image object using the data from the temporary file
$img = [System.Drawing.Image]::FromFile($Filename)
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
$form.Controls.Add($pictureBox)
$form.ShowDialog()
# here, when all is done and the form is no longer needed, you can
# get rid of the $img object that still has a reference to the
# temporary file and then delete that file.
$img.Dispose()
Remove-Item $Filename
# clean up the form aswell
$form.Dispose()
How to handle properly temporary files?
Once you write something on the disk you can't be 100% that you will able to delete it. Moreover, even if you delete the file, you can't be sure that file can't be recovered.
So you have to ask why I need to delete the file. If it contains some secret, keep it in memory. If you can't fit the file into memory, write it encrypted on the disk and keep only key in the memory.
If you relax 100% to 99%, I would go for creating a file with Path.GetTempFileName
and deleting it in finally block.
If 99% is not enough but 99.98% is, I would store names of created temporary files in persistent storage and regularly check if they are deleted.
How can I associate a specific file extension with my application?
Read Hans Passant comment.
Using direclty Setup Deployment Project and defining there file associations is quick and easy. Perfect.
Related Topics
Do the New C# 5.0 'Async' and 'Await' Keywords Use Multiple Cores
Httpclient and Using Proxy - Constantly Getting 407
Design Pattern for Handling Multiple Message Types
Generating an Array of Letters in the Alphabet
Unit Testing ASP.NET MVC Authorize Attribute to Verify Redirect to Login Page
ASP.NET MVC - How to Show Unauthorized Error on Login Page
This Row Already Belongs to Another Table Error When Trying to Add Rows
How to Cast Object to Its Actual Type
How to Create a Real-Time Excel Automation Add-In in C# Using Rtdserver
Namespace Not Recognized (Even Though It Is There)
Best Hashing Algorithm in Terms of Hash Collisions and Performance for Strings
Draw on the Screen Without a Form
Dbarithmeticexpression Arguments Must Have a Numeric Common Type
Change Connection String & Reload App.Config at Run Time
C# - How to Determine Whether a Type Is a Number
How to Dynamic New Anonymous Class