c# - Anybody got a good way to ATOMICALLY create a temporary directory in .net managed code? -


the .net framework has gettempfilename, goes straight windows api procedure of same name, , far know threadsafe way create temporary file name not in use. it's hilariously outdated , limited, far know works, long keep temp directory filling up.

but don't see that'll work creating temporary folders. in contentious parallel environment such web server, don't see way avoid race conditions between testing whether folder name in use, , creating it. suppose doable unmanaged api code, there may institutional resistance such solution. there better way?

and if there is, can extended temp file creation, don't have use gettempfilename 4 hex digits of variation?

-- think duplicate: linked other question did not address thread safety. , have develop situation thread safety serious concern -- have 32 cores running every 1 of them attempting create batches of hundred or thousand temp files apiece.

-- update: book secure programming static analysis chess , west says gettempfilename “suffers inherent underlying race condition”. maybe ain't safe either.

if want create unique temporary directory, combination of path.gettemppath , guid.newguid should trick quite nicely:

string getuniquetemppath() {     var temppath = path.gettemppath();     var uniquedirectoryname = guid.newguid().tostring();     var uniquetemppath = path.combine(temppath, uniquedirectoryname);      return uniquetemppath; } 

the fact guid being used part of path should eliminate race condition there's pretty much no chance composite directory name exist there's no (real) need check prior attempting create it.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -