using System; using System.Globalization; using System.IO; namespace System.Web.Util { internal sealed class FileUtils { internal delegate bool CreateTempFile (string path); static Random rnd; internal static Random Rnd { get { lock (rnd) { if (rnd == null) rnd = new Random (); } return rnd; } } internal static void GetTempFileName (string tempdir, CreateTempFile createFile) { GetTempFileName (tempdir, null, createFile); } internal static void GetTempFileName (string tempdir, string extension, CreateTempFile createFile) { if (tempdir == null || tempdir.Length == 0) return; if (createFile == null) return; Random rnd = Rnd; string path = null; bool done = false; 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 { done = createFile (path); } catch (System.IO.IOException) { } catch { throw; } } while (!done); } } }