All pastes #247809 Raw Edit

Someone

public text v1 · immutable
#247809 ·published 2006-11-13 22:06 UTC
rendered paste body
using System;
using System.Globalization;
using System.IO;

namespace System.Web.Util
{
	internal sealed class FileUtils
	{
		internal delegate object CreateTempFile (string path);

		static Random rnd = new Random ();
		
		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;
			string path = null;
			bool done = false;
			object ret = null;
			int num;
			
			do {
				lock (rnd) {
					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;
		}
	}
}