Letterman
Letterman

Reputation: 4216

EDITED: Where and how should download temp files?

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.

Upvotes: 3

Views: 3056

Answers (4)

Ben Griswold
Ben Griswold

Reputation: 18381

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.

Upvotes: 4

Vladimir Georgiev
Vladimir Georgiev

Reputation: 631

Have you looked at ClickOnce Deployment ?

Upvotes: 0

Blindy
Blindy

Reputation: 67487

You could look into Isolated Storage.

Upvotes: 0

Stefan Kendall
Stefan Kendall

Reputation: 67892

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.

Upvotes: 0

Related Questions