tags:

views:

152

answers:

4

Say I want to run an exe from the web, well obviously I rather download it to a temp folder.. run it from there, and delete it (or let the OS do so). Is there any convention how to do that, while being sure not to bomb into permissions issues, overriding exist file etc.?

Thanks.

EDIT I ment from a desktop application. BTW, I do have a lot of guess, but I'm just wondering what's accepted.

I know i can use Path.GetTempFile, but then it already made the file for me, which makes me thinking that it may also add it to some db, and keep track on it - so i can't delete it and replace it by my own file.

I know i can also use Path.TempDirectory (or something similar) and Path.RandomFileName, but the latter add a random extension to the file name, while i need exe, obviously this can be solven very easily as well, but it seems weird to me to rewrite what seems that MS already tried to wrote for us.

A: 

Trusted applets can play with the user's file system, so you could go that route. You're asking for C# advice, but this would work in the Java world if the language is flexible.

Stefan Kendall
A: 

You could look into Isolated Storage.

Blindy
A: 

Have you looked at ClickOnce Deployment ?

Vladimir Georgiev
+2  A: 

I had the need to write a self-updating Windows Service and found myself in similar shoes where I needed to download the latest installer to a temporary location prior to executing. There isn't much information regarding best practices in this area so I relied upon the "tools" which ASP.NET/C# offered.

I built the full path for the downloaded file using Path.GetTempPath() and the downloaded file name. Here's some sample code:

string url = "http://www.domain.com/install.exe";
string path = Path.GetTempPath();
string fileName = Path.GetFileName(url);
string tmpFile = Path.Combine(path, fileName);

The bottom line is I think your guess to use the System.IO.Path Temp* functions is a reasonable approach -- even though I can't find any documentation to support it.

Ben Griswold
Beeing perfectionist, I wonder what if such a common name already exist in the temp folder..
Itay
Well, you should strive to keep your filename unique. You could use either the Path.GetTempFileName() or Path.GetRandomFileName() to generate a filename. The former actually generates a file with a .tmp extension and you'll want to change that to meet your needs. The latter returns a cryptographically strong, random string that can be used as either a folder name or a file name. I hope that helps.
Ben Griswold
yeah it does. do want to bother, but here's the question, the GetRandomFileName return a file name with a random extension. I can easily delete the dot and add EXE, do I have no choice?
Itay
There's not much of a choice, but I suggest using Path.ChangeExtension() to change the extension rather than coding the logic yourself.
Ben Griswold