FileUtils.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // System.Web.Compilation.AppCodeCompiler: A compiler for the App_Code folder
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Globalization;
  31. using System.IO;
  32. namespace System.Web.Util
  33. {
  34. internal sealed class FileUtils
  35. {
  36. internal delegate object CreateTempFile (string path);
  37. static Random rnd = new Random ();
  38. internal static object CreateTemporaryFile (string tempdir, CreateTempFile createFile)
  39. {
  40. return CreateTemporaryFile (tempdir, null, null, createFile);
  41. }
  42. internal static object CreateTemporaryFile (string tempdir, string extension, CreateTempFile createFile)
  43. {
  44. return CreateTemporaryFile (tempdir, null, extension, createFile);
  45. }
  46. internal static object CreateTemporaryFile (string tempdir, string prefix, string extension, CreateTempFile createFile)
  47. {
  48. if (tempdir == null || tempdir.Length == 0)
  49. return null;
  50. if (createFile == null)
  51. return null;
  52. string path = null;
  53. object ret = null;
  54. int num;
  55. do {
  56. lock (rnd) {
  57. num = rnd.Next ();
  58. }
  59. path = Path.Combine (tempdir,
  60. String.Format ("{0}{1}{2}", (prefix != null) ? prefix + "." : "",
  61. num.ToString ("x", CultureInfo.InvariantCulture),
  62. (extension != null) ? "." + extension : ""));
  63. try {
  64. ret = createFile (path);
  65. } catch (System.IO.IOException) {
  66. } catch { throw; }
  67. } while (ret == null);
  68. return ret;
  69. }
  70. }
  71. }