VirtualUserFormFileProvider.cs
上传用户:husern
上传日期:2022-03-24
资源大小:534k
文件大小:4k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. // -- FILE ------------------------------------------------------------------
  2. // name       : VirtualUserFormFileProvider.cs
  3. // project    : Itenso Web User Forms
  4. // created    : Jani Giannoudis - 2008.10.30
  5. // language   : c#
  6. // environment: .NET 2.0
  7. // copyright  : (c) 2008 by Itenso GmbH, Switzerland
  8. // --------------------------------------------------------------------------
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. using System.Collections.Generic;
  13. using Itenso.WebUserForms.Controls;
  14. namespace Itenso.WebUserForms.Runtime
  15. {
  16. // ------------------------------------------------------------------------
  17. public class VirtualUserFormFileProvider : VirtualUserFormProvider, IUserFormProvider
  18. {
  19. // ----------------------------------------------------------------------
  20. public VirtualUserFormFileProvider( string virtualPath, string physicalPath )
  21. : base( virtualPath )
  22. {
  23. if ( string.IsNullOrEmpty( physicalPath ) )
  24. {
  25. throw new ArgumentNullException( "physicalPath" );
  26. }
  27. if ( !Directory.Exists( physicalPath ) )
  28. {
  29. throw new ArgumentException( "invalid physical path" );
  30. }
  31. this.physicalPath = physicalPath;
  32. } // VirtualUserFormFileProvider
  33. // ----------------------------------------------------------------------
  34. public string PhysicalPath
  35. {
  36. get { return this.physicalPath; }
  37. } // PhysicalPath
  38. // ----------------------------------------------------------------------
  39. public string GetVirtualFormName( string formName )
  40. {
  41. if ( string.IsNullOrEmpty( formName ) )
  42. {
  43. throw new ArgumentNullException( "formName" );
  44. }
  45. string virtualFormName = VirtualPath;
  46. if ( !virtualFormName.EndsWith( "/" ) )
  47. {
  48. virtualFormName += "/";
  49. }
  50. virtualFormName += formName;
  51. if ( !virtualFormName.EndsWith( UserFormInfo.FileExtension ) )
  52. {
  53. virtualFormName += UserFormInfo.FileExtension;
  54. }
  55. return virtualFormName;
  56. } // GetVirtualFormName
  57. // ----------------------------------------------------------------------
  58. public List<string> GetAvailableUserForms()
  59. {
  60. return ( (IUserFormProvider)this ).GetAvailableUserForms( null );
  61. } // GetAvailableUserForms
  62. // ----------------------------------------------------------------------
  63. List<string> IUserFormProvider.GetAvailableUserForms( string formType )
  64. {
  65. List<string> formInfos = new List<string>();
  66. DirectoryInfo directoryInfo = new DirectoryInfo( this.physicalPath );
  67. foreach ( FileInfo fileInfo in directoryInfo.GetFiles( "*" + UserFormInfo.FileExtension ) )
  68. {
  69. formInfos.Add( fileInfo.Name.Replace( fileInfo.Extension, string.Empty ) );
  70. }
  71. return formInfos;
  72. } // IUserFormProvider.GetAvailableUserForms
  73. // ----------------------------------------------------------------------
  74. protected override Stream LoadUserForm( string virtualPath )
  75. {
  76. MemoryStream stream = null;
  77. string baseVirtualPath = VirtualPath.TrimStart( '~' );
  78. int pathIndex = virtualPath.IndexOf( baseVirtualPath );
  79. if ( pathIndex >= 0 )
  80. {
  81. string physicalFileName = Path.Combine( 
  82. PhysicalPath,
  83. virtualPath.Substring( pathIndex + baseVirtualPath.Length + 1 ) );
  84. using ( StreamReader streamReader = new StreamReader( physicalFileName ) )
  85. {
  86. stream = new MemoryStream( Encoding.Default.GetBytes( streamReader.ReadToEnd() ) );
  87. }
  88. }
  89. return stream;
  90. } // LoadUserForm
  91. // ----------------------------------------------------------------------
  92. // members
  93. private readonly string physicalPath;
  94. } // class VirtualUserFormFileProvider
  95. } // namespace Itenso.WebUserForms.Runtime
  96. // -- EOF -------------------------------------------------------------------