PageRenderTime 1243ms CodeModel.GetById 704ms app.highlight 312ms RepoModel.GetById 212ms app.codeStats 1ms

/IZWebFileManager/FileManagerController.cs

http://izwebfilemanager.googlecode.com/
C# | 1580 lines | 1432 code | 118 blank | 30 comment | 154 complexity | a232e93a4fcc00a8f1f532991a27ff34 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1// Copyright (C) 2006 Igor Zelmanovich <izwebfilemanager@gmail.com>
   2//
   3// This program is free software; you can redistribute it and/or modify
   4// it under the terms of the GNU General Public License as published by
   5// the Free Software Foundation; either version 2 of the License, or
   6// (at your option) any later version.
   7//
   8// This program is distributed in the hope that it will be useful,
   9// but WITHOUT ANY WARRANTY; without even the implied warranty of
  10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11// GNU General Public License for more details.
  12//
  13// You should have received a copy of the GNU General Public License
  14// along with this program; if not, write to the Free Software
  15// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  16
  17using System;
  18using System.Collections.Generic;
  19using System.Text;
  20using System.Web.UI;
  21using System.ComponentModel;
  22using System.Resources;
  23using IZ.WebFileManager.Components;
  24using System.Collections;
  25using System.Web.UI.WebControls;
  26using System.Collections.ObjectModel;
  27using System.IO;
  28using System.Web;
  29using System.Security.Permissions;
  30using System.Threading;
  31using System.Drawing;
  32using System.Globalization;
  33using System.Text.RegularExpressions;
  34using System.Runtime.Serialization;
  35using System.Drawing.Design;
  36using System.Web.Configuration;
  37using System.Security;
  38
  39namespace IZ.WebFileManager
  40{
  41	[PersistChildren (false)]
  42	[ParseChildren (true)]
  43	public sealed partial class FileManagerController : Control, ICallbackEventHandler, IPostBackEventHandler
  44	{
  45		#region Fields
  46
  47		internal static readonly Unit SmallImageWidth = 16;
  48		internal static readonly Unit SmallImageHeight = 16;
  49		internal static readonly Unit LargeImageWidth = 32;
  50		internal static readonly Unit LargeImageHeight = 32;
  51		static readonly Hashtable _imageExtension;
  52		static readonly bool _supportThumbnails;
  53		const string ThumbnailHandler = "IZWebFileManagerThumbnailHandler.ashx";
  54
  55		Hashtable _fileExtensions;
  56		Hashtable _specialFolderPaths;
  57		ArrayList _hiddenFilesArray;
  58		Style _hiddenItemStyle;
  59		ArrayList _prohibitedFilesArray;
  60		readonly FileTypeCollection _fileTypeCollection = new FileTypeCollection ();
  61		readonly NewDocumentTemplateCollection _templates = new NewDocumentTemplateCollection ();
  62		readonly RootDirectoryCollection _rootDirectories = new RootDirectoryCollection ();
  63		readonly SpecialFolderCollection _specialFolders = new SpecialFolderCollection ();
  64
  65		string _defaultFolderSmallImage;
  66		string _defaultFolderLargeImage;
  67		string _defaultFileSmallImage;
  68		string _defaultFileLargeImage;
  69		string _defaultRootFolderSmallImage;
  70		string _defaultRootFolderLargeImage;
  71		Dictionary<ToolbarImages, string> _toolbarImages;
  72		bool _toolbarImagesInitialized;
  73		bool _defailtsInitialized;
  74
  75		string _callbackResult;
  76		string _callbackCommandArgument;
  77		FileManagerCommands _callbackCommand;
  78		FileManagerControlBase _callbackControl;
  79
  80		#endregion
  81
  82		#region Properties
  83
  84		internal bool SupportThumbnails {
  85			get { return _supportThumbnails; }
  86		}
  87
  88		[Themeable (false)]
  89		[Localizable (false)]
  90		[DefaultValue (false)]
  91		[Category ("Behavior")]
  92		public bool AllowOverwrite {
  93			get { return ViewState ["AllowOverwrite"] == null ? false : (bool) ViewState ["AllowOverwrite"]; }
  94			set { ViewState ["AllowOverwrite"] = value; }
  95		}
  96
  97		[Themeable (false)]
  98		[Localizable (false)]
  99		[DefaultValue (true)]
 100		[Category ("Behavior")]
 101		public bool AllowUpload {
 102			get { return ViewState ["AllowUpload"] == null ? true : (bool) ViewState ["AllowUpload"]; }
 103			set { ViewState ["AllowUpload"] = value; }
 104		}
 105
 106		/// <summary>
 107		/// When is set true, Delete, Move and Rename are not allowed, Default value it true.
 108		/// </summary>
 109		[Themeable (false)]
 110		[Localizable (false)]
 111		[DefaultValue (true)]
 112		[Category ("Behavior")]
 113		public bool AllowDelete {
 114			get { return ViewState ["AllowDelete"] == null ? true : (bool) ViewState ["AllowDelete"]; }
 115			set { ViewState ["AllowDelete"] = value; }
 116		}
 117
 118		[Themeable (false)]
 119		[Localizable (false)]
 120		[DefaultValue (false)]
 121		[Category ("Behavior")]
 122		public bool ReadOnly {
 123			get { return ViewState ["ReadOnly"] == null ? false : (bool) ViewState ["ReadOnly"]; }
 124			set { ViewState ["ReadOnly"] = value; }
 125		}
 126
 127		[Themeable (false)]
 128		[Localizable (false)]
 129		[DefaultValue (false)]
 130		[Category ("Behavior")]
 131		public bool DownloadOnDoubleClick {
 132			get { return ViewState ["DownloadOnDoubleClick"] == null ? false : (bool) ViewState ["DownloadOnDoubleClick"]; }
 133			set { ViewState ["DownloadOnDoubleClick"] = value; }
 134		}
 135
 136		[DefaultValue ("IZWebFileManagerResource")]
 137		[Category ("Data")]
 138		public string ResourceClassKey {
 139			get { return ((ViewState ["ResourceClassKey"] == null) ? "IZWebFileManagerResource" : (string) ViewState ["ResourceClassKey"]); }
 140			set { ViewState ["ResourceClassKey"] = value; }
 141		}
 142
 143		[DefaultValue (null)]
 144		[Category ("Appearance")]
 145		[Themeable (true)]
 146		public CultureInfo Culture {
 147			get { return ((ViewState ["Culture"] == null) ? null : (CultureInfo) ViewState ["Culture"]); }
 148			set { ViewState ["Culture"] = value; }
 149		}
 150
 151		public Boolean IsRightToLeft { get { return CurrentUICulture.TextInfo.IsRightToLeft; } }
 152
 153		[Browsable (false)]
 154		public CultureInfo CurrentUICulture { get { return Culture == null ? CultureInfo.CurrentUICulture : Culture; } }
 155
 156		[DefaultValue ("")]
 157		[Category ("Action")]
 158		[Themeable (false)]
 159		[Localizable (false)]
 160		public string ClientOpenItemFunction {
 161			get { return ((ViewState ["ClientOpenItemFunction"] == null) ? String.Empty : (string) ViewState ["ClientOpenItemFunction"]); }
 162			set { ViewState ["ClientOpenItemFunction"] = value; }
 163		}
 164
 165		[DefaultValue ("")]
 166		[Category ("Behavior")]
 167		[Themeable (false)]
 168		[Localizable (false)]
 169		public string HiddenFiles {
 170			get { return ((ViewState ["HiddenFiles"] == null) ? String.Empty : (string) ViewState ["HiddenFiles"]); }
 171			set { ViewState ["HiddenFiles"] = value; }
 172		}
 173
 174		[DefaultValue ("")]
 175		[Category ("Behavior")]
 176		[Themeable (false)]
 177		[Localizable (false)]
 178		public string HiddenFilesAndFoldersPrefix {
 179			get { return (string) (ViewState ["HiddenFolderPrefix"] ?? String.Empty); }
 180			set { ViewState ["HiddenFolderPrefix"] = value; }
 181		}
 182
 183		[DefaultValue (false)]
 184		[Category ("Behavior")]
 185		[Themeable (false)]
 186		[Localizable (false)]
 187		public bool ShowHiddenFilesAndFolders {
 188			get { return (bool) (ViewState ["ShowHiddenFolders"] ?? false); }
 189			set { ViewState ["ShowHiddenFolders"] = value; }
 190		}
 191
 192		internal ArrayList HiddenFilesArray {
 193			get {
 194				if (_hiddenFilesArray == null)
 195					_hiddenFilesArray = InitExtensionsArray (HiddenFiles);
 196				return _hiddenFilesArray;
 197			}
 198		}
 199
 200		internal Style HiddenItemStyle {
 201			get {
 202				if (_hiddenItemStyle == null) {
 203					_hiddenItemStyle = new HiddenItemStyle ();
 204				}
 205				return _hiddenItemStyle;
 206			}
 207		}
 208
 209		[DefaultValue ("")]
 210		[Category ("Behavior")]
 211		[Themeable (false)]
 212		[Localizable (false)]
 213		public string ProhibitedFiles {
 214			get { return ((ViewState ["ProhibitedFiles"] == null) ? String.Empty : (string) ViewState ["ProhibitedFiles"]); }
 215			set { ViewState ["ProhibitedFiles"] = value; }
 216		}
 217
 218		internal ArrayList ProhibitedFilesArray {
 219			get {
 220				if (_prohibitedFilesArray == null)
 221					_prohibitedFilesArray = InitExtensionsArray (ProhibitedFiles);
 222				return _prohibitedFilesArray;
 223			}
 224		}
 225
 226		[MergableProperty (false)]
 227		[Category ("Behavior")]
 228		[PersistenceMode (PersistenceMode.InnerProperty)]
 229		[Localizable (false)]
 230		[Themeable (false)]
 231		public RootDirectoryCollection RootDirectories {
 232			get { return _rootDirectories; }
 233		}
 234
 235		[MergableProperty (false)]
 236		[Category ("Behavior")]
 237		[PersistenceMode (PersistenceMode.InnerProperty)]
 238		[Localizable (false)]
 239		[Themeable (false)]
 240		public SpecialFolderCollection SpecialFolders {
 241			get { return _specialFolders; }
 242		}
 243
 244		[MergableProperty (false)]
 245		[PersistenceMode (PersistenceMode.InnerProperty)]
 246		[Category ("Behavior")]
 247		[Localizable (false)]
 248		[Themeable (false)]
 249		public NewDocumentTemplateCollection Templates {
 250			get { return _templates; }
 251		}
 252
 253		[MergableProperty (false)]
 254		[PersistenceMode (PersistenceMode.InnerProperty)]
 255		[Category ("Behavior")]
 256		[Localizable (false)]
 257		[Themeable (false)]
 258		public FileTypeCollection FileTypes {
 259			get { return _fileTypeCollection; }
 260		}
 261
 262		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 263		[DefaultValue ("")]
 264		[UrlProperty]
 265		[Bindable (true)]
 266		public string FileSmallImageUrl {
 267			get { return ViewState ["FileSmallIconUrl"] == null ? String.Empty : (string) ViewState ["FileSmallIconUrl"]; }
 268			set { ViewState ["FileSmallIconUrl"] = value; }
 269		}
 270
 271		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 272		[DefaultValue ("")]
 273		[UrlProperty]
 274		[Bindable (true)]
 275		public string FileLargeImageUrl {
 276			get { return ViewState ["FileLargeIconUrl"] == null ? String.Empty : (string) ViewState ["FileLargeIconUrl"]; }
 277			set { ViewState ["FileLargeIconUrl"] = value; }
 278		}
 279
 280		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 281		[DefaultValue ("")]
 282		[UrlProperty]
 283		[Bindable (true)]
 284		public string FolderSmallImageUrl {
 285			get { return ViewState ["FolderSmallIconUrl"] == null ? String.Empty : (string) ViewState ["FolderSmallIconUrl"]; }
 286			set { ViewState ["FolderSmallIconUrl"] = value; }
 287		}
 288
 289		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 290		[DefaultValue ("")]
 291		[UrlProperty]
 292		[Bindable (true)]
 293		public string FolderLargeImageUrl {
 294			get { return ViewState ["FolderLargeIconUrl"] == null ? String.Empty : (string) ViewState ["FolderLargeIconUrl"]; }
 295			set { ViewState ["FolderLargeIconUrl"] = value; }
 296		}
 297
 298		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 299		[DefaultValue ("")]
 300		[UrlProperty]
 301		[Bindable (true)]
 302		public string RootFolderSmallImageUrl {
 303			get { return ViewState ["RootFolderSmallIconUrl"] == null ? String.Empty : (string) ViewState ["RootFolderSmallIconUrl"]; }
 304			set { ViewState ["RootFolderSmallIconUrl"] = value; }
 305		}
 306
 307		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 308		[DefaultValue ("")]
 309		[UrlProperty]
 310		[Bindable (true)]
 311		public string RootFolderLargeImageUrl {
 312			get { return ViewState ["RootFolderLargeIconUrl"] == null ? String.Empty : (string) ViewState ["RootFolderLargeIconUrl"]; }
 313			set { ViewState ["RootFolderLargeIconUrl"] = value; }
 314		}
 315
 316		[DefaultValue ("")]
 317		[Bindable (true)]
 318		public string ImagesFolder {
 319			get { return ViewState ["IconsFolder"] == null ? String.Empty : (string) ViewState ["IconsFolder"]; }
 320			set { ViewState ["IconsFolder"] = value; }
 321		}
 322
 323		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 324		[DefaultValue ("")]
 325		[UrlProperty]
 326		[Bindable (true)]
 327		public string CopyImageUrl {
 328			get { return ViewState ["CopyIconUrl"] == null ? String.Empty : (string) ViewState ["CopyIconUrl"]; }
 329			set { ViewState ["CopyIconUrl"] = value; }
 330		}
 331
 332		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 333		[DefaultValue ("")]
 334		[UrlProperty]
 335		[Bindable (true)]
 336		public string MoveImageUrl {
 337			get { return ViewState ["MoveIconUrl"] == null ? String.Empty : (string) ViewState ["MoveIconUrl"]; }
 338			set { ViewState ["MoveIconUrl"] = value; }
 339		}
 340
 341		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 342		[DefaultValue ("")]
 343		[UrlProperty]
 344		[Bindable (true)]
 345		public string DeleteImageUrl {
 346			get { return ViewState ["DeleteIconUrl"] == null ? String.Empty : (string) ViewState ["DeleteIconUrl"]; }
 347			set { ViewState ["DeleteIconUrl"] = value; }
 348		}
 349
 350		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 351		[DefaultValue ("")]
 352		[UrlProperty]
 353		[Bindable (true)]
 354		public string RenameImageUrl {
 355			get { return ViewState ["RenameIconUrl"] == null ? String.Empty : (string) ViewState ["RenameIconUrl"]; }
 356			set { ViewState ["RenameIconUrl"] = value; }
 357		}
 358
 359		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 360		[DefaultValue ("")]
 361		[UrlProperty]
 362		[Bindable (true)]
 363		public string FolderUpImageUrl {
 364			get { return ViewState ["FolderUpIconUrl"] == null ? String.Empty : (string) ViewState ["FolderUpIconUrl"]; }
 365			set { ViewState ["FolderUpIconUrl"] = value; }
 366		}
 367
 368		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 369		[DefaultValue ("")]
 370		[UrlProperty]
 371		[Bindable (true)]
 372		public string NewFolderImageUrl {
 373			get { return ViewState ["NewFolderImageUrl"] == null ? String.Empty : (string) ViewState ["NewFolderImageUrl"]; }
 374			set { ViewState ["NewFolderImageUrl"] = value; }
 375		}
 376
 377		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 378		[DefaultValue ("")]
 379		[UrlProperty]
 380		[Bindable (true)]
 381		public string ViewImageUrl {
 382			get { return ViewState ["ViewIconUrl"] == null ? String.Empty : (string) ViewState ["ViewIconUrl"]; }
 383			set { ViewState ["ViewIconUrl"] = value; }
 384		}
 385
 386		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 387		[DefaultValue ("")]
 388		[UrlProperty]
 389		[Bindable (true)]
 390		public string ProcessImageUrl {
 391			get { return ViewState ["ProcessIconUrl"] == null ? String.Empty : (string) ViewState ["ProcessIconUrl"]; }
 392			set { ViewState ["ProcessIconUrl"] = value; }
 393		}
 394
 395		[Editor ("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
 396		[DefaultValue ("")]
 397		[UrlProperty]
 398		[Bindable (true)]
 399		public string RefreshImageUrl {
 400			get { return ViewState ["RefreshImageUrl"] == null ? String.Empty : (string) ViewState ["RefreshImageUrl"]; }
 401			set { ViewState ["RefreshImageUrl"] = value; }
 402		}
 403
 404		#endregion
 405
 406		static FileManagerController () {
 407			_imageExtension = new Hashtable (StringComparer.OrdinalIgnoreCase);
 408			object o = new object ();
 409			_imageExtension [".gif"] = o;
 410			_imageExtension [".jpg"] = o;
 411			_imageExtension [".jpeg"] = o;
 412			_imageExtension [".png"] = o;
 413
 414			try {
 415				HttpHandlerActionCollection handlers = ((HttpHandlersSection) WebConfigurationManager.GetSection ("system.web/httpHandlers")).Handlers;
 416				foreach (HttpHandlerAction action in handlers)
 417					if (String.Compare (action.Path, ThumbnailHandler, StringComparison.OrdinalIgnoreCase) == 0) {
 418						_supportThumbnails = true;
 419						break;
 420					}
 421			}
 422			catch (SecurityException) {
 423				_supportThumbnails = true;
 424			}
 425		}
 426
 427		protected override void OnPreRender (EventArgs e) {
 428			base.OnPreRender (e);
 429
 430			Page.ClientScript.RegisterClientScriptBlock (typeof (FileManagerController), "WebFileManager_DoCallback", GetDoCallbackScript (), true);
 431			Page.ClientScript.RegisterClientScriptResource (typeof (FileManagerController), "IZ.WebFileManager.resources.FileManagerController.js");
 432			Page.ClientScript.RegisterStartupScript (typeof (FileManagerController), ClientID, GetInitInstanceScript (), true);
 433
 434			RegisterResources ();
 435
 436			EnsureDefaults ();
 437
 438			// register hidden item style
 439			if (!String.IsNullOrEmpty (HiddenFilesAndFoldersPrefix) || !String.IsNullOrEmpty (HiddenFiles))
 440				if (Page.Header != null)
 441					Page.Header.StyleSheet.RegisterStyle (HiddenItemStyle, this);
 442		}
 443
 444		internal void EnsureToolbarImages () {
 445			if (_toolbarImagesInitialized)
 446				return;
 447			_toolbarImagesInitialized = true;
 448			InitToolbarImages ();
 449		}
 450
 451		private void InitToolbarImages () {
 452			bool isImagesFolder = ImagesFolder.Length > 0;
 453			string imagesFolder = EnsureEndsWithSlash (ImagesFolder);
 454			_toolbarImages = new Dictionary<ToolbarImages, string> ();
 455
 456			// Copy icon
 457			if (CopyImageUrl.Length > 0)
 458				_toolbarImages [ToolbarImages.Copy] = ResolveUrl (CopyImageUrl);
 459			else if (isImagesFolder)
 460				_toolbarImages [ToolbarImages.Copy] = ResolveUrl (imagesFolder + "Copy.gif");
 461			else
 462				_toolbarImages [ToolbarImages.Copy] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.Copy.gif");
 463
 464			// Delete icon
 465			if (DeleteImageUrl.Length > 0)
 466				_toolbarImages [ToolbarImages.Delete] = ResolveUrl (DeleteImageUrl);
 467			else if (isImagesFolder)
 468				_toolbarImages [ToolbarImages.Delete] = ResolveUrl (imagesFolder + "Delete.gif");
 469			else
 470				_toolbarImages [ToolbarImages.Delete] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.Delete.gif");
 471
 472			// Move icon
 473			if (MoveImageUrl.Length > 0)
 474				_toolbarImages [ToolbarImages.Move] = ResolveUrl (MoveImageUrl);
 475			else if (isImagesFolder)
 476				_toolbarImages [ToolbarImages.Move] = ResolveUrl (imagesFolder + "Move.gif");
 477			else
 478				_toolbarImages [ToolbarImages.Move] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.Move.gif");
 479
 480			// Rename icon
 481			if (RenameImageUrl.Length > 0)
 482				_toolbarImages [ToolbarImages.Rename] = ResolveUrl (RenameImageUrl);
 483			else if (isImagesFolder)
 484				_toolbarImages [ToolbarImages.Rename] = ResolveUrl (imagesFolder + "Rename.gif");
 485			else
 486				_toolbarImages [ToolbarImages.Rename] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.Rename.gif");
 487
 488			// Rename icon
 489			if (NewFolderImageUrl.Length > 0)
 490				_toolbarImages [ToolbarImages.NewFolder] = ResolveUrl (NewFolderImageUrl);
 491			else if (isImagesFolder)
 492				_toolbarImages [ToolbarImages.NewFolder] = ResolveUrl (imagesFolder + "NewFolder.gif");
 493			else
 494				_toolbarImages [ToolbarImages.NewFolder] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.NewFolder.gif");
 495
 496			// View icon
 497			if (ViewImageUrl.Length > 0)
 498				_toolbarImages [ToolbarImages.View] = ResolveUrl (ViewImageUrl);
 499			else if (isImagesFolder)
 500				_toolbarImages [ToolbarImages.View] = ResolveUrl (imagesFolder + "View.gif");
 501			else
 502				_toolbarImages [ToolbarImages.View] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.View.gif");
 503
 504			// FolderUp icon
 505			if (FolderUpImageUrl.Length > 0)
 506				_toolbarImages [ToolbarImages.FolderUp] = ResolveUrl (FolderUpImageUrl);
 507			else if (isImagesFolder)
 508				_toolbarImages [ToolbarImages.FolderUp] = ResolveUrl (imagesFolder + "FolderUp.gif");
 509			else
 510				_toolbarImages [ToolbarImages.FolderUp] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.FolderUp.gif");
 511
 512			// Process icon
 513			if (ProcessImageUrl.Length > 0)
 514				_toolbarImages [ToolbarImages.Process] = ResolveUrl (ProcessImageUrl);
 515			else if (isImagesFolder)
 516				_toolbarImages [ToolbarImages.Process] = ResolveUrl (imagesFolder + "Process.gif");
 517			else
 518				_toolbarImages [ToolbarImages.Process] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.Process.gif");
 519
 520			// Refresh icon
 521			if (RefreshImageUrl.Length > 0)
 522				_toolbarImages [ToolbarImages.Refresh] = ResolveUrl (RefreshImageUrl);
 523			else if (isImagesFolder)
 524				_toolbarImages [ToolbarImages.Refresh] = ResolveUrl (imagesFolder + "Refresh.gif");
 525			else
 526				_toolbarImages [ToolbarImages.Refresh] = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.Refresh.gif");
 527		}
 528
 529		internal void EnsureDefaults () {
 530			if (_defailtsInitialized)
 531				return;
 532			_defailtsInitialized = true;
 533			InitDefaults ();
 534		}
 535
 536		private void InitDefaults () {
 537			bool isImagesFolder = ImagesFolder.Length > 0;
 538			string imagesFolder = EnsureEndsWithSlash (ImagesFolder);
 539
 540			if (RootFolderSmallImageUrl.Length > 0)
 541				_defaultRootFolderSmallImage = ResolveUrl (RootFolderSmallImageUrl);
 542			else if (isImagesFolder)
 543				_defaultRootFolderSmallImage = ResolveUrl (imagesFolder + "RootFolderSmall.gif");
 544			else
 545				_defaultRootFolderSmallImage = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.RootFolderSmall.gif");
 546
 547			if (RootFolderLargeImageUrl.Length > 0)
 548				_defaultRootFolderLargeImage = ResolveUrl (RootFolderLargeImageUrl);
 549			else if (isImagesFolder)
 550				_defaultRootFolderLargeImage = ResolveUrl (imagesFolder + "RootFolderLarge.gif");
 551			else
 552				_defaultRootFolderLargeImage = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.RootFolderLarge.gif");
 553
 554			if (FolderSmallImageUrl.Length > 0)
 555				_defaultFolderSmallImage = ResolveUrl (FolderSmallImageUrl);
 556			else if (isImagesFolder)
 557				_defaultFolderSmallImage = ResolveUrl (imagesFolder + "FolderSmall.gif");
 558			else
 559				_defaultFolderSmallImage = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.FolderSmall.gif");
 560
 561			if (FolderLargeImageUrl.Length > 0)
 562				_defaultFolderLargeImage = ResolveUrl (FolderLargeImageUrl);
 563			else if (isImagesFolder)
 564				_defaultFolderLargeImage = ResolveUrl (imagesFolder + "FolderLarge.gif");
 565			else
 566				_defaultFolderLargeImage = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.FolderLarge.gif");
 567
 568			if (FileSmallImageUrl.Length > 0)
 569				_defaultFileSmallImage = ResolveUrl (FileSmallImageUrl);
 570			else if (isImagesFolder)
 571				_defaultFileSmallImage = ResolveUrl (imagesFolder + "FileSmall.gif");
 572			else
 573				_defaultFileSmallImage = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.FileSmall.gif");
 574
 575			if (FileLargeImageUrl.Length > 0)
 576				_defaultFileLargeImage = ResolveUrl (FileLargeImageUrl);
 577			else if (isImagesFolder)
 578				_defaultFileLargeImage = ResolveUrl (imagesFolder + "FileLarge.gif");
 579			else
 580				_defaultFileLargeImage = Page.ClientScript.GetWebResourceUrl (typeof (FileManagerController), "IZ.WebFileManager.resources.FileLarge.gif");
 581		}
 582
 583		string EnsureEndsWithSlash (string path) {
 584			if (!path.EndsWith ("/"))
 585				return path + "/";
 586			return path;
 587		}
 588
 589		private void InitFileTypes () {
 590			_fileExtensions = new Hashtable (StringComparer.OrdinalIgnoreCase);
 591
 592			foreach (FileType fileType in FileTypes) {
 593				string [] exts = fileType.Extensions.Split (new char [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
 594				foreach (string ext in exts) {
 595					_fileExtensions [ext.Trim ().Trim ('.')] = fileType;
 596				}
 597			}
 598		}
 599
 600		private void InitSpecialFolders () {
 601			_specialFolderPaths = new Hashtable (StringComparer.OrdinalIgnoreCase);
 602
 603			foreach (SpecialFolder folder in SpecialFolders) {
 604				if (folder.DirectoryPath.Length > 0) {
 605					string path = Page.MapPath (folder.DirectoryPath);
 606					_specialFolderPaths [path] = folder;
 607					if (path.EndsWith ("/"))
 608						path.TrimEnd ('/');
 609					else
 610						path = path + "/";
 611					_specialFolderPaths [path] = folder;
 612				}
 613			}
 614		}
 615
 616		private static ArrayList InitExtensionsArray (string extList) {
 617			string [] exts = extList.Split (',');
 618			ArrayList array = new ArrayList ();
 619			foreach (string ext in exts) {
 620				array.Add (ext.Trim ().ToLower (CultureInfo.InvariantCulture).TrimStart ('.'));
 621			}
 622			return array;
 623		}
 624
 625		internal FileType GetFileType (FileInfo file) {
 626			if (_fileExtensions == null)
 627				InitFileTypes ();
 628			string ext = file.Extension.TrimStart ('.');
 629			return (FileType) _fileExtensions [ext];
 630		}
 631
 632		public string GetResourceString (string name, string defaultValue) {
 633			try {
 634				string value = (string) HttpContext.GetGlobalResourceObject (ResourceClassKey, name, CurrentUICulture);
 635				if (value == null)
 636					return defaultValue;
 637				return value;
 638			}
 639			catch (MissingManifestResourceException) {
 640				return defaultValue;
 641			}
 642		}
 643
 644		internal void AddDirectionAttribute (HtmlTextWriter writer) {
 645			writer.AddAttribute (HtmlTextWriterAttribute.Dir, IsRightToLeft ? "rtl" : "ltr");
 646			writer.AddStyleAttribute (HtmlTextWriterStyle.Direction, IsRightToLeft ? "rtl" : "ltr");
 647		}
 648
 649		internal const string ClientScriptObjectNamePrefix = "WFM_";
 650		private string GetInitInstanceScript () {
 651			return "var " + ClientScriptObjectNamePrefix + ClientID + "=new FileManagerController('" + ClientID + "','" + UniqueID + "','" + EventArgumentSplitter + "');\r\n";
 652		}
 653
 654		private string GetDoCallbackScript () {
 655			StringBuilder sb = new StringBuilder ();
 656			sb.AppendLine ("function WebFileManager_DoCallback(target, argument, clientCallback, context, clientErrorCallback) {");
 657			sb.AppendLine (Page.ClientScript.GetCallbackEventReference ("target", "argument", "clientCallback", "context", "clientErrorCallback", false) + ";");
 658			sb.AppendLine ("}");
 659
 660			return sb.ToString ();
 661		}
 662
 663		internal string GetCommandEventReference (Control control, string command, string arg) {
 664			return ClientScriptObjectNamePrefix + ClientID + ".On" + command + "(" + ClientScriptObjectNamePrefix + control.ClientID + "," + arg + ")";
 665		}
 666
 667		#region ICallbackEventHandler Members
 668
 669		string GetCallbackResult () {
 670			EnsureDefaults ();
 671
 672			switch (_callbackCommand) {
 673			case FileManagerCommands.Refresh:
 674				_callbackResult = _callbackControl.RenderContents ();
 675				break;
 676			case FileManagerCommands.FileViewSort:
 677				_callbackResult = _callbackControl.RenderContents ();
 678				break;
 679			case FileManagerCommands.FileViewChangeView:
 680				_callbackResult = _callbackControl.RenderContents ();
 681				break;
 682			case FileManagerCommands.FileViewShowInGroups:
 683				_callbackResult = _callbackControl.RenderContents ();
 684				break;
 685			}
 686
 687			return _callbackResult;
 688		}
 689
 690		void RaiseCallbackEvent (string eventArgument) {
 691			if (eventArgument == null)
 692				return;
 693
 694			// Parse eventArgument
 695			string [] args = eventArgument.Split (new char [] { EventArgumentSplitter }, 3);
 696			_callbackControl = (FileManagerControlBase) fileMangerControls [args [0]];
 697			_callbackCommand = (FileManagerCommands) Enum.Parse (typeof (FileManagerCommands), args [1]);
 698			_callbackCommandArgument = null;
 699			if (args.Length > 2)
 700				_callbackCommandArgument = args [2];
 701
 702			switch (_callbackCommand) {
 703			case FileManagerCommands.Refresh:
 704				break;
 705			case FileManagerCommands.FileViewSort:
 706				break;
 707			case FileManagerCommands.FileViewChangeView:
 708				break;
 709			case FileManagerCommands.FileViewShowInGroups:
 710				break;
 711			case FileManagerCommands.FileViewNavigate:
 712				_callbackResult = ProcessFileViewNavigate (_callbackControl.ResolveFileManagerItemInfo (DecodeURIComponent (_callbackCommandArgument)));
 713				break;
 714			case FileManagerCommands.ExecuteCommand:
 715				string [] inds = _callbackCommandArgument.Split (':');
 716				_callbackResult = ProcessExecuteCommand (_callbackControl.SelectedItems, int.Parse (inds [0], null), int.Parse (inds [1], null));
 717				break;
 718			default:
 719				if (ReadOnly)
 720					_callbackResult = String.Empty;
 721				else {
 722					switch (_callbackCommand) {
 723					case FileManagerCommands.Rename:
 724						if (!AllowDelete) {
 725							_callbackResult = String.Empty;
 726							break;
 727						}
 728						string [] paths = _callbackCommandArgument.Split (EventArgumentSplitter);
 729						_callbackResult = ProcessRename (_callbackControl.ResolveFileManagerItemInfo (DecodeURIComponent (paths [0])), DecodeURIComponent (paths [1]));
 730						break;
 731					case FileManagerCommands.NewDocument:
 732						int index = int.Parse (_callbackCommandArgument, null);
 733						NewDocumentTemplate template = Templates [index];
 734						FileManagerItemInfo dir = _callbackControl.GetCurrentDirectory ();
 735						_callbackResult = ProcessNewDocument (dir, template);
 736						break;
 737					case FileManagerCommands.NewFolder:
 738						_callbackResult = ProcessNewFolder (_callbackControl.GetCurrentDirectory ());
 739						break;
 740					case FileManagerCommands.SelectedItemsDelete:
 741						if (!AllowDelete) {
 742							_callbackResult = String.Empty;
 743							break;
 744						}
 745						EvaluateSelectedItemsActionCommand (IZ.WebFileManager.SelectedItemsAction.Delete);
 746						break;
 747					case FileManagerCommands.SelectedItemsCopyTo:
 748						EvaluateSelectedItemsActionCommand (IZ.WebFileManager.SelectedItemsAction.Copy);
 749						break;
 750					case FileManagerCommands.SelectedItemsMoveTo:
 751						if (!AllowDelete) {
 752							_callbackResult = String.Empty;
 753							break;
 754						}
 755						EvaluateSelectedItemsActionCommand (IZ.WebFileManager.SelectedItemsAction.Move);
 756						break;
 757					default:
 758						String message = "Not Implemented Command \"{0}\"";
 759						throw new ArgumentException (String.Format (null, message, _callbackCommand));
 760					}
 761				}
 762				break;
 763			}
 764		}
 765
 766		private void EvaluateSelectedItemsActionCommand (SelectedItemsAction action) {
 767			_callbackResult = ProcessSelectedItemsAction (_callbackControl.CurrentDirectory, _callbackControl.ResolveFileManagerItemInfo (DecodeURIComponent (_callbackCommandArgument)), _callbackControl.SelectedItems, action);
 768		}
 769
 770		private string ProcessSelectedItemsAction (FileManagerItemInfo srcDir, FileManagerItemInfo destDir, FileManagerItemInfo [] items, SelectedItemsAction action) {
 771			if (items.Length == 0)
 772				return "";
 773
 774			SelectedItemsActionCancelEventArgs cancelArg = new SelectedItemsActionCancelEventArgs (action);
 775			foreach (FileManagerItemInfo item in items) {
 776				if (item.Directory.Exists && destDir.Directory.FullName.StartsWith (item.Directory.FullName, StringComparison.InvariantCultureIgnoreCase))
 777					return ClientMessageEventReference (GetResourceString ("DestinationFolderIsSubfolder", "The destination folder is a subfolder of the source folder."));
 778				cancelArg.SelectedItems.Add (item);
 779			}
 780			cancelArg.DestinationDirectory = destDir;
 781			OnSelectedItemsAction (cancelArg);
 782			if (cancelArg.Cancel) {
 783				return ClientMessageEventReference (cancelArg.ClientMessage);
 784			}
 785
 786			StringBuilder sb = new StringBuilder ();
 787
 788			switch (action) {
 789			case IZ.WebFileManager.SelectedItemsAction.Delete:
 790				if (ProcessSelectedItemsDelete (items))
 791					AddFolderTreeRefreshEventReference (sb, srcDir);
 792				break;
 793			case IZ.WebFileManager.SelectedItemsAction.Move:
 794				destDir.EnsureDirectoryExists ();
 795				if (ProcessSelectedItemsMoveTo (destDir, items)) {
 796					AddFolderTreeRequireRefreshEventReference (sb, srcDir, destDir);
 797					AddFolderTreeNavigateEventReference (sb, srcDir);
 798				}
 799				break;
 800			case IZ.WebFileManager.SelectedItemsAction.Copy:
 801				destDir.EnsureDirectoryExists ();
 802				if (ProcessSelectedItemsCopyTo (destDir, items)) {
 803					AddFolderTreeRequireRefreshEventReference (sb, srcDir, destDir);
 804					AddFolderTreeNavigateEventReference (sb, srcDir);
 805				}
 806				break;
 807			}
 808
 809
 810			SelectedItemsActionEventArgs args = new SelectedItemsActionEventArgs ();
 811			OnSelectedItemsActionComplete (args);
 812
 813			sb.AppendLine (ClientRefreshEventReference);
 814			return sb.ToString ();
 815
 816		}
 817
 818		private bool ProcessSelectedItemsMoveTo (FileManagerItemInfo destDir, FileManagerItemInfo [] items) {
 819			bool b = false;
 820			foreach (FileManagerItemInfo item in items) {
 821				if (item.Directory.Exists) {
 822					b = true;
 823					DirectoryInfo dir = item.Directory;
 824					if (String.Compare (dir.Parent.FullName.TrimEnd (Path.DirectorySeparatorChar), destDir.PhysicalPath.TrimEnd (Path.DirectorySeparatorChar), true) == 0)
 825						continue;
 826
 827					string newName;
 828					if (AllowOverwrite)
 829						newName = dir.Name;
 830					else
 831						newName = GetNotDuplicatedFolderName (destDir, dir.Name);
 832					string newPath = Path.Combine (destDir.PhysicalPath, newName);
 833					xDirectory.Copy (dir.FullName, newPath, true);
 834					dir.Delete (true);
 835				}
 836				else if (item.File.Exists) {
 837					FileInfo file = item.File;
 838					if (String.Compare (file.Directory.FullName.TrimEnd (Path.DirectorySeparatorChar), destDir.PhysicalPath.TrimEnd (Path.DirectorySeparatorChar), true) == 0)
 839						continue;
 840
 841					string newName;
 842					if (AllowOverwrite)
 843						newName = file.Name;
 844					else
 845						newName = GetNotDuplicatedFileName (destDir, Path.GetFileNameWithoutExtension (file.FullName), file.Extension);
 846					string newPath = Path.Combine (destDir.PhysicalPath, newName);
 847					file.CopyTo (newPath, AllowOverwrite);
 848					file.Delete ();
 849				}
 850			}
 851			return b;
 852		}
 853
 854		private bool ProcessSelectedItemsCopyTo (FileManagerItemInfo destDir, FileManagerItemInfo [] items) {
 855			bool b = false;
 856			foreach (FileManagerItemInfo item in items) {
 857				if (item.Directory.Exists) {
 858					b = true;
 859					DirectoryInfo dir = item.Directory;
 860					string newName;
 861					if (AllowOverwrite && String.Compare (dir.Parent.FullName.TrimEnd (Path.DirectorySeparatorChar), destDir.PhysicalPath.TrimEnd (Path.DirectorySeparatorChar), true) != 0)
 862						newName = dir.Name;
 863					else
 864						newName = GetNotDuplicatedFolderName (destDir, dir.Name);
 865					string newPath = Path.Combine (destDir.PhysicalPath, newName);
 866					xDirectory.Copy (dir.FullName, newPath, true);
 867				}
 868				else if (item.File.Exists) {
 869					FileInfo file = item.File;
 870					string newName;
 871					if (AllowOverwrite && String.Compare (file.Directory.FullName.TrimEnd (Path.DirectorySeparatorChar), destDir.PhysicalPath.TrimEnd (Path.DirectorySeparatorChar), true) != 0)
 872						newName = file.Name;
 873					else
 874						newName = GetNotDuplicatedFileName (destDir, Path.GetFileNameWithoutExtension (file.FullName), file.Extension);
 875					string newPath = Path.Combine (destDir.PhysicalPath, newName);
 876					file.CopyTo (newPath, AllowOverwrite);
 877				}
 878			}
 879			return b;
 880		}
 881
 882		private bool ProcessSelectedItemsDelete (FileManagerItemInfo [] items) {
 883			bool b = false;
 884			foreach (FileManagerItemInfo item in items) {
 885				if (item.File.Exists)
 886					item.File.Delete ();
 887				else if (item.Directory.Exists) {
 888					b = true;
 889					item.Directory.Delete (true);
 890				}
 891			}
 892			return b;
 893		}
 894
 895		private String ClientRefreshEventReference { get { return ClientScriptObjectNamePrefix + ClientID + ".OnRefresh(context,null)"; } }
 896		private string ClientMessageEventReference (string message) {
 897			string clientMessage = message;
 898			if (clientMessage == null || clientMessage.Length == 0)
 899				clientMessage = GetResourceString ("CannotCompleteOperation", "Cannot Complete Operation.");
 900
 901			return "alert(decodeURIComponent('" + EncodeURIComponent (clientMessage) + "'))";
 902		}
 903
 904		private string ProcessRename (FileManagerItemInfo fileManagerItemInfo, string newName) {
 905			RenameCancelEventArgs cancelArg = new RenameCancelEventArgs ();
 906			cancelArg.FileManagerItem = fileManagerItemInfo;
 907			cancelArg.NewName = newName;
 908			OnItemRenaming (cancelArg);
 909			if (cancelArg.Cancel) {
 910				return ClientMessageEventReference (cancelArg.ClientMessage);
 911			}
 912
 913			if (cancelArg.NewName == null || cancelArg.NewName.Length == 0 || cancelArg.NewName.StartsWith (".")) {
 914				return ClientMessageEventReference (GetResourceString ("MustTypeFileName", "You must type a file name."));
 915			}
 916
 917			if (!Validate (cancelArg.NewName)) {
 918				return ClientMessageEventReference (GetResourceString ("NotAllowedCharacters", "A file name cannot contain any of the following characters: \\/:*?\"<>|"));
 919			}
 920
 921			if (fileManagerItemInfo.File.Exists) {
 922				string newFileExt = Path.GetExtension (cancelArg.NewName).ToLower (CultureInfo.InvariantCulture).TrimStart ('.');
 923				if (newFileExt.Length == 0 ||
 924					HiddenFilesArray.Contains (newFileExt) ||
 925					ProhibitedFilesArray.Contains (newFileExt))
 926					cancelArg.NewName += fileManagerItemInfo.File.Extension;
 927			}
 928
 929			FileManagerItemInfo renamedItem = ResolveFileManagerItemInfo (fileManagerItemInfo.FileManagerPath.Substring (0, fileManagerItemInfo.FileManagerPath.LastIndexOf ('/')) + "/" + cancelArg.NewName);
 930			if (renamedItem.Directory.Exists || renamedItem.File.Exists) {
 931				string fileExistsMessage = GetResourceString ("CannotRenameFile", "Cannot rename file: A file with the name you specified already exists. Specify a different file name.");
 932				return ClientMessageEventReference (fileExistsMessage);
 933			}
 934
 935			bool b = false;
 936			if (fileManagerItemInfo.Directory.Exists) {
 937				b = true;
 938				fileManagerItemInfo.Directory.MoveTo (renamedItem.PhysicalPath);
 939			}
 940			else if (fileManagerItemInfo.File.Exists) {
 941				fileManagerItemInfo.File.MoveTo (renamedItem.PhysicalPath);
 942			}
 943
 944			RenameEventArgs arg = new RenameEventArgs ();
 945			arg.FileManagerItem = renamedItem;
 946			OnItemRenamed (arg);
 947
 948			StringBuilder sb = new StringBuilder ();
 949			if (b)
 950				AddFolderTreeRefreshEventReference (sb, _callbackControl.CurrentDirectory);
 951			sb.AppendLine (ClientRefreshEventReference);
 952			return sb.ToString ();
 953		}
 954
 955		internal static bool Validate (string name) {
 956			// \/:*?"<>|
 957			return !name.Contains ("\\") &&
 958				!name.Contains ("/") &&
 959				!name.Contains (":") &&
 960				!name.Contains ("*") &&
 961				!name.Contains ("?") &&
 962				!name.Contains ("\"") &&
 963				!name.Contains ("<") &&
 964				!name.Contains (">") &&
 965				!name.Contains ("|");
 966
 967		}
 968
 969		internal static string DecodeURIComponent (string str) {
 970			string res = HttpUtility.UrlDecode (str);
 971			return res;
 972		}
 973		internal static string EncodeURIComponent (string str) {
 974			string res = HttpUtility.UrlEncode (str);
 975			res = res.Replace ("+", "%20");
 976			res = res.Replace ("'", "%27");
 977			return res;
 978		}
 979		internal static string DecodeURI (string str) {
 980			string res = HttpUtility.UrlDecode (str);
 981			return res;
 982		}
 983		internal static string EncodeURI (string str) {
 984			string res = HttpUtility.UrlPathEncode (str);
 985			res = res.Replace ("+", "%20");
 986			return res;
 987		}
 988
 989		string ProcessFileViewNavigate (FileManagerItemInfo item) {
 990			if (item.Directory.Exists) {
 991				StringBuilder sb = new StringBuilder ();
 992				AddFolderTreeNavigateEventReference (sb, item);
 993				sb.AppendLine ("var dir = '" + EncodeURIComponent (item.FileManagerPath) + "'");
 994				sb.AppendLine ("WebForm_GetElementById(context.ClientID+'_Directory').value = dir;");
 995				sb.AppendLine ("var address = WebForm_GetElementById(context.ClientID+'_Address');");
 996				sb.AppendLine ("if(address) address.value = decodeURIComponent(dir);");
 997				sb.AppendLine (ClientRefreshEventReference);
 998				return sb.ToString ();
 999			}
1000			else if (item.File.Exists) {
1001				return ProcessExecuteCommand (new FileManagerItemInfo [] { item }, 0, 0);
1002			}
1003			else
1004				return ClientMessageEventReference (GetResourceString ("CannotFindFile", "Cannot find file, Make sure the path is correct."));
1005		}
1006
1007		internal string [] GetPathHashCodes (string fileManagerPath) {
1008			fileManagerPath = VirtualPathUtility.RemoveTrailingSlash (fileManagerPath);
1009			List<string> list = new List<string> ();
1010			int index = fileManagerPath.IndexOf ('/', 1);
1011			while (index > 0) {
1012				list.Add (GetPathHashCode (fileManagerPath.Substring (0, index)));
1013				index = fileManagerPath.IndexOf ('/', index + 1);
1014			}
1015			list.Add (GetPathHashCode (fileManagerPath));
1016			return list.ToArray ();
1017		}
1018
1019		internal string GetPathHashCode (string fileManagerPath) {
1020			return VirtualPathUtility.RemoveTrailingSlash (fileManagerPath).ToLower ().GetHashCode ().ToString ("x");
1021		}
1022
1023		private string ProcessExecuteCommand (FileManagerItemInfo [] items, int group, int index) {
1024			if (items.Length == 0)
1025				return "";
1026
1027			if (group == 0) {
1028				FileManagerItemInfo item = items [0];
1029
1030				if (item.Directory.Exists)
1031					return ProcessFileViewNavigate (item);
1032
1033				// Default Command
1034				if (index == -1)
1035					return ProcessOpenCommand (item);
1036				else if (index == -2)
1037					return ProcessDownloadCommand (item);
1038
1039				FileType ft = GetFileType (item.File);
1040				if (ft == null || ft.Commands.Count <= index)
1041					return DownloadOnDoubleClick ? ProcessDownloadCommand (item) : ProcessOpenCommand (item);
1042
1043				return ProcessCommand (item, ft.Commands [index]);
1044			}
1045			else {
1046			}
1047
1048
1049			return "";
1050		}
1051
1052		private string ProcessDownloadCommand (FileManagerItemInfo item) {
1053			String script = Page.ClientScript.GetPostBackEventReference (this, "Download:" + EncodeURIComponent (item.FileManagerPath)) + ";" +
1054				"theForm.__EVENTTARGET.value = '';theForm.__EVENTARGUMENT.value = '';";
1055			return script;
1056		}
1057
1058		private string ProcessCommand (FileManagerItemInfo item, FileManagerCommand fileManagerCommand) {
1059			ExecuteCommandEventArgs arg = new ExecuteCommandEventArgs (fileManagerCommand.CommandName, fileManagerCommand.CommandArgument);
1060			arg.Items.Add (item);
1061			//arg.Command = fileManagerCommand;
1062
1063			OnExecuteCommand (arg);
1064
1065			return arg.ClientScript;
1066		}
1067
1068		private string ProcessOpenCommand (FileManagerItemInfo item) {
1069			StringBuilder sb = new StringBuilder ();
1070
1071			if (ClientOpenItemFunction.Length > 0) {
1072				sb.AppendLine (ClientOpenItemFunction + "('" + EncodeURI (item.VirtualPath) + "')");
1073			}
1074			else {
1075				sb.AppendLine ("var href = '" + EncodeURIComponent (item.VirtualPath) + "';");
1076				sb.AppendLine ("window.open(decodeURIComponent(href),'_blank');");
1077			}
1078			return sb.ToString ();
1079		}
1080
1081		string ResolveAbsolutePath (ref string fileManagerPath) {
1082			Regex reg = new Regex (@"^((?<rootPath>/[^/]+)|(\[(?<rootIndex>\d+)\]))(?<dirPath>/.*)?");
1083			Match match = reg.Match (fileManagerPath);
1084			if (!match.Success)
1085				goto _throw;
1086			RootDirectory rootDirectory;
1087			string dirPath = match.Groups ["dirPath"].Value;
1088			string rootPath = match.Groups ["rootPath"].Value;
1089			string rootIndex = match.Groups ["rootIndex"].Value;
1090			if (!String.IsNullOrEmpty (rootPath))
1091				rootDirectory = RootDirectories [rootPath.Substring (1)];
1092			else {
1093				int index = Int32.Parse (rootIndex);
1094				if (index >= RootDirectories.Count)
1095					goto _throw;
1096				rootDirectory = RootDirectories [index];
1097				rootPath = "/" + rootDirectory.TextInternal;
1098			}
1099			if (rootDirectory == null)
1100				goto _throw;
1101			dirPath = Normalize (dirPath);
1102			fileManagerPath = rootPath + dirPath;
1103			string rootVPath = VirtualPathUtility.RemoveTrailingSlash (ResolveUrl (rootDirectory.DirectoryPath));
1104			string vPath = rootVPath + dirPath;
1105
1106			return vPath;
1107
1108		_throw:
1109			throw new ArgumentException ("Invalid file manager path.");
1110		}
1111
1112		public FileManagerItemInfo ResolveFileManagerItemInfo (string fileManagerPath) {
1113			string virtualPath = ResolveAbsolutePath (ref fileManagerPath);
1114			string phisicalPath = Page.MapPath (virtualPath);
1115			return new FileManagerItemInfo (fileManagerPath, virtualPath, phisicalPath);
1116		}
1117
1118		internal string ProcessFileUpload (FileManagerItemInfo destDir, System.Web.HttpPostedFile uploadedFile) {
1119			string script;
1120			if (AllowUpload)
1121				ProcessFileUpload (destDir, uploadedFile, out script);
1122			else
1123				script = ClientMessageEventReference (null);
1124			return script;
1125		}
1126
1127		internal bool ProcessFileUpload (FileManagerItemInfo destDir, System.Web.HttpPostedFile uploadedFile, out string script) {
1128			UploadFileCancelEventArgs cancelArg = new UploadFileCancelEventArgs ();
1129			cancelArg.DestinationDirectory = destDir;
1130			cancelArg.PostedFile = uploadedFile;
1131			OnFileUploading (cancelArg);
1132
1133			if (cancelArg.Cancel) {
1134				script = ClientMessageEventReference (cancelArg.ClientMessage);
1135				return false;
1136			}
1137
1138			string saveName = cancelArg.SaveName;
1139			string fileNameWithoutExtension = saveName.Substring (0, saveName.LastIndexOf ('.'));
1140			string extension = saveName.Substring (saveName.LastIndexOf ('.'));
1141
1142			string ext = extension.ToLower (CultureInfo.InvariantCulture).TrimStart ('.');
1143			if (HiddenFilesArray.Contains (ext) ||
1144					ProhibitedFilesArray.Contains (ext)) {
1145				script = ClientMessageEventReference (null);
1146				return false;
1147			}
1148
1149			string newFileName = GetNotDuplicatedFileName (destDir, fileNameWithoutExtension, extension);
1150			FileManagerItemInfo itemInfo = ResolveFileManagerItemInfo (VirtualPathUtility.AppendTrailingSlash (destDir.FileManagerPath) + newFileName);
1151
1152			uploadedFile.SaveAs (itemInfo.PhysicalPath);
1153
1154			UploadFileEventArgs arg = new UploadFileEventArgs ();
1155			arg.UploadedFile = itemInfo;
1156			OnFileUploaded (arg);
1157
1158			script = ClientRefreshEventReference;
1159			return true;
1160		}
1161
1162		private static string GetNotDuplicatedFileName (FileManagerItemInfo destDir, string fileNameWithoutExtension, string extension) {
1163			string fileName = fileNameWithoutExtension;
1164			int i = 1;
1165			string originNewFileName = fileNameWithoutExtension;
1166			while (File.Exists (Path.Combine (destDir.PhysicalPath, fileName + extension))) {
1167				fileName = originNewFileName + "(" + i + ")";
1168				i++;
1169			}
1170			return fileName + extension;
1171		}
1172
1173		private static string GetNotDuplicatedFolderName (FileManagerItemInfo destDir, string folderName) {
1174			string newFolderName = folderName;
1175			int i = 1;
1176			string originNewFolderName = folderName;
1177			while (Directory.Exists (Path.Combine (destDir.PhysicalPath, newFolderName))) {
1178				newFolderName = originNewFolderName + "(" + i + ")";
1179				i++;
1180			}
1181			return newFolderName;
1182		}
1183
1184		private string ProcessNewFolder (FileManagerItemInfo destDir) {
1185			NewFolderCancelEventArgs cancelArg = new NewFolderCancelEventArgs ();
1186			cancelArg.DestinationDirectory = destDir;
1187			OnNewFolderCreating (cancelArg);
1188
1189			if (cancelArg.Cancel) {
1190				return ClientMessageEventReference (cancelArg.ClientMessage);
1191			}
1192
1193			string folderName = GetResourceString ("New_Folder_Name", "New Folder");
1194
1195			string newFolderName = GetNotDuplicatedFolderName (destDir, folderName);
1196
1197			FileManagerItemInfo itemInfo = ResolveFileManagerItemInfo (VirtualPathUtility.AppendTrailingSlash (destDir.FileManagerPath) + newFolderName);
1198			itemInfo.EnsureDirectoryExists ();
1199
1200			NewFolderEventArgs arg = new NewFolderEventArgs ();
1201			arg.NewFolder = itemInfo;
1202			OnNewFolderCreated (arg);
1203
1204			StringBuilder sb = new StringBuilder ();
1205			AddFolderTreeRefreshEventReference (sb, destDir);
1206			sb.AppendLine (ClientRefreshEventReference);
1207			return sb.ToString ();
1208		}
1209
1210		void AddFolderTreeNavigateEventReference (StringBuilder sb, FileManagerItemInfo itemInfo) {
1211			sb.AppendLine ("var folderTree = window['WFM_' + context.ClientID + '_FolderTree'];");
1212			sb.AppendLine ("if(folderTree) {folderTree.Navigate (['" + String.Join ("','", GetPathHashCodes (itemInfo.FileManagerPath)) + "'],0);}");
1213		}
1214
1215		void AddFolderTreeRefreshEventReference (StringBuilder sb, FileManagerItemInfo itemInfo) {
1216			sb.AppendLine ("var folderTree = window['WFM_' + context.ClientID + '_FolderTree'];");
1217			sb.AppendLine ("if(folderTree) {folderTree.Refresh ('" + GetPathHashCode (itemInfo.FileManagerPath) + "');}");
1218		}
1219
1220		void AddFolderTreeRequireRefreshEventReference (StringBuilder sb, FileManagerItemInfo srcInfo, FileManagerItemInfo destInfo) {
1221			sb.AppendLine ("var folderTree = window['WFM_' + context.ClientID + '_FolderTree'];");
1222			sb.AppendLine ("if(folderTree) {folderTree.RequireRefresh (['" + GetPathHashCode (srcInfo.FileManagerPath) + "','" + String.Join ("','", GetPathHashCodes (destInfo.FileManagerPath)) + "']);}");
1223		}
1224
1225		private string ProcessNewDocument (FileManagerItemInfo destDir, NewDocumentTemplate template) {
1226			NewDocumentCancelEventArgs cancelArg = new NewDocumentCancelEventArgs ();
1227			cancelArg.DestinationDirectory = destDir;
1228			cancelArg.Template = template;
1229			OnNewDocumentCreating (cancelArg);
1230
1231			if (cancelArg.Cancel) {
1232				return ClientMessageEventReference (cancelArg.ClientMessage);
1233			}
1234
1235			if (template.MasterFileUrl.Length == 0)
1236				return "";
1237
1238			FileInfo masterFile = new FileInfo (Page.MapPath (template.MasterFileUrl));
1239			if (!masterFile.Exists)
1240				return "";
1241
1242			string newFileName = template.NewFileName;
1243			if (newFileName.Length == 0)
1244				newFileName = template.Name;
1245
1246			int i = 1;
1247			string originNewFileName = newFileName;
1248			while (File.Exists (Path.Combine (destDir.PhysicalPath, newFileName + masterFile.Extension))) {
1249				newFileName = originNewFileName + "(" + i + ")";
1250				i++;
1251			}
1252			newFileName = newFileName + masterFile.Extension;
1253			FileManagerItemInfo itemInfo = ResolveFileManagerItemInfo (VirtualPathUtility.AppendTrailingSlash (destDir.FileManagerPath) + newFileName);
1254			FileInfo newDocument = masterFile.CopyTo (itemInfo.PhysicalPath);
1255
1256			NewDocumentEventArgs arg = new NewDocumentEventArgs ();
1257			arg.Template = template;
1258			arg.NewDocument = itemInfo;
1259			OnNewDocumentCreated (arg);
1260
1261			return ClientRefreshEventReference;
1262		}
1263
1264		#endregion
1265
1266
1267		Hashtable fileMangerControls = new Hashtable ();
1268		internal void RegisterComponent (FileManagerControlBase cont

Large files files are truncated, but you can click here to view the full file