rendered paste bodyusing System;
using System.Globalization;
using System.IO;
namespace System.Web.Util
{
internal sealed class FileUtils
{
internal delegate object CreateTempFile (string path);
static Random rnd;
internal static Random Rnd
{
get {
lock (rnd) {
if (rnd == null)
rnd = new Random ();
}
return rnd;
}
}
internal static object CreateTemporaryFile (string tempdir, CreateTempFile createFile)
{
return CreateTemporaryFile (tempdir, null, createFile);
}
internal static object CreateTemporaryFile (string tempdir, string extension, CreateTempFile createFile)
{
if (tempdir == null || tempdir.Length == 0)
return null;
if (createFile == null)
return null;
Random rnd = Rnd;
string path = null;
bool done = false;
object ret = null;
do {
int num = rnd.Next ();
num++;
path = Path.Combine (tempdir, num.ToString ("x", CultureInfo.InvariantCulture));
if (extension != null)
path = String.Format ("{0}.{1}", path, extension);
try {
ret = createFile (path);
} catch (System.IO.IOException) {
} catch { throw; }
} while (ret == null);
return ret;
}
}
}