Shares.cs :  » Game » Media-File-XStream » MediaStream » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » Media File XStream 
Media File XStream » MediaStream » Shares.cs
/* 
Media File XStream, Network file stream server supporting XBMSP
Copyright (C) 2004  j3g

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

This project can be found on SourceForge.
http://sourceforge.net/projects/mfxstream
*/
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Xml;

namespace MediaStream{
  /// <summary>
  /// Summary description for CShares.
  /// </summary>
  public class CShares
  {

    public const string ConstFolderString = "<DIRECTORYITEM><NAME>$NAME</NAME><ATTRIB>directory</ATTRIB></DIRECTORYITEM>";
    public const string ConstFileString = "<DIRECTORYITEM><NAME>$NAME</NAME><ATTRIB>file</ATTRIB><SIZE>$SIZE</SIZE></DIRECTORYITEM>";

    private Hashtable _Shares = new Hashtable(100);
    private Hashtable _Paths = new Hashtable(1000);
    private Hashtable _FSW = new Hashtable(20);
    private Hashtable _CDW = new Hashtable(20);
    private Hashtable _MountPoints = new Hashtable(100);
    private FileSystemWatcher _SharesFile;
    #region Constructors
    public CShares()
    {
      LoadShares(CSettings.LoadSharesXMLDom());
    }
    #endregion Constructors
    #region Public methods
    public void GetFileAndFolderCount(out int Files, out int Folders)
    {
      int nFiles = 0;
      int nFolders = 0;
      int nCnt;
      foreach (string sPath in this._Paths.Keys)
      {
        nCnt = ((Hashtable) this._Paths[sPath]).Count;
        if (IsFolder(sPath) || IsDrive(sPath))
          nFolders += nCnt;
        else
          nFiles += nCnt;
      }
      Files = nFiles;
      Folders = nFolders;
    }
    public string GetActualName(string Share, string Path)
    {
      if (IsDrive(Path))
      {
        return("Drive " + Path.Substring(0, 2).ToUpper());
      }
      else
      {
        string strRet = GetMountPointName(Share, Path);
        if (0 == strRet.Length)
        {
          string [] strName;
          DirectoryInfo di = new DirectoryInfo(Path);
          if (null == di.Parent)
          {
            strName = Path.TrimEnd('\\').Split('\\');
            strRet = strName[strName.Length - 1];
          }
          else if (!di.Parent.Exists)
          {
            strName = Path.TrimEnd('\\').Split('\\');
            strRet = strName[strName.Length - 1];
          }
          if (di.Exists)
          {
            di = new DirectoryInfo(System.IO.Directory.GetDirectories(di.Parent.FullName, di.Name)[0]);
            strRet = di.Name;
          }
          else
          {
            FileInfo fi = new FileInfo(System.IO.Directory.GetFiles(di.Parent.FullName, di.Name)[0]);
            strRet = fi.Name;
          }
        }
        return(strRet);
      }
    }
    public static string GetFilename(string sPath)
    {
      return(System.IO.Path.GetFileName(sPath));
    }
    public static string GetFolder(string sPath)
    {
      return(System.IO.Path.GetDirectoryName(sPath));
    }
    public static long GetFileSize(string sPath) 
    {
      FileInfo fi = new FileInfo(sPath);
      return (fi.Length);
    }
    public void Close()
    {
      ClearFileSystemWatchers();
      ClearDeviceWatchers();
      if (null != this._SharesFile)
      {
        this._SharesFile.EnableRaisingEvents = false;
        this._SharesFile = null;
      }
      foreach(Hashtable ht in this._Shares.Values)
      {
        ht.Clear();
      }
      foreach(Hashtable ht in this._Paths.Values)
      {
        ht.Clear();
      }
      this._Shares.Clear();
      this._Shares = null;
      this._Paths.Clear();
      this._Paths = null;
    }
    public static bool IsDrive(string Path)
    {
      string sPath = Path.ToLower();
      String [] strDrives = System.IO.Directory.GetLogicalDrives();
      foreach (String strDrive in strDrives)
      {
        if (sPath.Equals(strDrive.ToLower()))
          return (true);
      }
      return (false);
    }
    public static bool IsFolder(string Path)
    {
      if (IsDrive(Path)) return(true);
      DirectoryInfo di = new DirectoryInfo(Path);
      return (di.Exists);
    }
    private string GetMountPointName(string Share, string Path)
    {
      string strName = "";
      string [] arrShare = Share.Split('/');
      string strShare;
      if (1 < arrShare.Length)
        strShare = "/" + arrShare[1];
      else
        strShare = "/" + arrShare[0];
      if (this._MountPoints.Contains(Share))
      {
        Hashtable ht = (Hashtable) this._MountPoints[Share];
        if (ht.Contains(Path.ToLower()))
          strName = ((MountPoint) ht[Path.ToLower()]).MountPointName;
      }
      return(strName);
    }
    public static string GetXBMSPXml(ShareItem si)
    { 
      String strRet;
      if (si.IsFolder)
        strRet = CShares.ConstFolderString;
      else
      {
        strRet = CShares.ConstFileString;
        strRet = strRet.Replace("$SIZE", CShares.GetFileSize(si.Path).ToString());
      }
      strRet = strRet.Replace("$NAME", si.Name);

      return (strRet);
    }
    #endregion Public methods
    #region Load Shares
    public void LoadShares(XmlDocument xmlShares)
    {
      string strShareName;
      string strMountPointName;
      string strPath;
      bool blnMergeMountPoints;
      bool blnFlattenSubFolders;
      bool blnShowSubFolders;

      Close();
      this._Shares = new Hashtable(100);
      this._Paths = new Hashtable(1000);
      this._FSW = new Hashtable(20);

      Hashtable htRoot = GetShareTable("/");
      ShareItem si;
      // Load Shares
      XmlNodeList xmlShareList = xmlShares.SelectNodes("/Shares/Share[@name]");
      foreach(XmlNode xmlShare in xmlShareList)
      {
        strShareName = xmlShare.Attributes.GetNamedItem("name").InnerText;
        si = new ShareItem();
        si.Name = strShareName;
        si.IsFolder = true;
        htRoot.Add(si.Name, si);
        strShareName = "/" + strShareName;
        GetShareTable(strShareName);
        blnMergeMountPoints = ("1" == GetXMLValue(xmlShare, "MergeMountPoints", "0"));
        XmlNodeList xmlMountPointList = xmlShare.SelectNodes("MountPoint[@name]");
        foreach(XmlNode xmlMountPoint in xmlMountPointList)
        {
          strPath = xmlMountPoint.Attributes.GetNamedItem("name").InnerText;
          if (PathExists(strPath))
          {
            strMountPointName = GetXMLValue(xmlMountPoint, "Name", "");
            blnFlattenSubFolders = ("1" == GetXMLValue(xmlMountPoint, "FlattenSubFolders", "0"));
            blnShowSubFolders = ("1" == GetXMLValue(xmlMountPoint, "ShowSubFolders", "0"));
            MountPoint mp = AddMountPoint(strPath, strShareName, strMountPointName, blnMergeMountPoints, blnFlattenSubFolders, blnShowSubFolders);
            if (blnMergeMountPoints)
              LoadFilesAndSubFolders(mp.ParentShareName, mp.Path, mp.FlattenSubFolders, mp.ShowSubFolders);
            else
              LoadFilesAndSubFolders(mp.Share, mp.Path, mp.FlattenSubFolders, mp.ShowSubFolders);
            if (IsDrive(strPath))
              AddDeviceWatcher(strPath);
            else
              AddFileSystemWatcher(strPath, blnShowSubFolders);
          }
        }
      }
      WatchSharesFile();
    }
    public static bool PathExists(string Path)
    {
      try
      {
        if (IsDrive(Path)) return(true);

        if (System.IO.Directory.Exists(Path) || System.IO.File.Exists(Path)) 
          return(true);
        else
          return(false);
      }
      catch
      {
        return(false);
      }
    }
    private String GetXMLValue(XmlNode xmlNode, String Tag, String Default)
    {
      XmlNode xmlValue = xmlNode.SelectSingleNode(Tag);
      if (null == xmlValue)
        return (Default);
      else
        return (xmlValue.InnerText);
    }

    #endregion Load Shares
    #region Public properties
    public ArrayList this[string Share]
    {
      get 
      {
        lock(this._Shares)
        {
          if (null == this._Shares[Share]) return(null);
          ArrayList al = new ArrayList(GetShareTable(Share).Values);
          string strExtension = CSettings.GetValideFileExtensions();
          if (0 < strExtension.Length)
          {
            string strExt, strPath;
            int n;
            for(int i = al.Count - 1; i >= 0; i--)
            {
              strPath = ((ShareItem) al[i]).Path;
              n = strPath.LastIndexOf(".");
              if (n > 0)
              {
                strExt = strPath.Substring(n);
                if (-1 == strExtension.IndexOf(strExt))
                  al.RemoveAt(i);
              }
            }
          }
          CShareItemComparer sip = new CShareItemComparer();
          al.Sort(sip);
          return (al);
        }
      } 
    }
    public ShareItem GetShareItem(string sShare, string sItem)
    {
      lock(this._Shares)
      {
        Hashtable ht = GetShareTable(sShare);
        if (ht.Contains(sItem))
          return((ShareItem) ht[sItem]);
        else
          return(null);
      }
    }
    public ShareItem GetShareItem(string sShareItem)
    {
      int nPos = sShareItem.LastIndexOf("/");
      string sShare = sShareItem.Substring(0, nPos);
      string sItem = sShareItem.Substring(nPos);
      lock(this._Shares)
      {
        Hashtable ht = GetShareTable(sShare);
        if (ht.Contains(sItem))
          return((ShareItem) ht[sItem]);
        else
          return(null);
      }
    }
    #endregion Public properties
    /*
     * AddFile - Add to Paths Hashtabel, Add to Shares Hashtable
     * RemoveFile - Remove from Paths, Remove from Shares Hashtabel
     * AddFolder - Add to Paths Hashtable, Add to Shares Hashtable, Add Share Hashtabel entry
     * RemoveFolder - Remove Share Hashtable entry, Remove all Paths from Paths starting with foldername.
     * LoadFolder - Load files in folder and subfolders.
    */

    #region Add, Remove and Rename File and Folder methods
    private void AddFile(string Path, string Share)
    {
      ShareItem si = new ShareItem(GetActualName(Share, Path), Path);
      Hashtable hts = GetShareTable(Share);
      if (!hts.Contains(si.Name))
      {
        // Add to Shares Hashtable
        hts.Add(si.Name, si);

        // Add to Paths Hashtabel
        Hashtable htp = GetPathTable(si.Path);
        htp.Add(Share, si.Name);
      }
    }
    private void AddFolder(string Path, string Share, bool CreateShareFolder)
    {
      // Add to Paths Hashtabel
      ShareItem si = new ShareItem(GetActualName(Share, Path), Path);
      Hashtable ht = GetPathTable(si.Path);
      ht.Add(Share, si.Name);

      if (CreateShareFolder)
      {
        // Add to Shares Hashtable
        ht = GetShareTable(Share);
        if (!ht.Contains(si.Name))
        {
          ht.Add(si.Name, si);
          GetShareTable(Share + "/" + si.Name);
        }
      }
    }
    private void LoadFilesAndSubFolders(string Share, string Path, bool FlattenSubFolders, bool ShowSubFolders)
    {
      ShareItem si = new ShareItem(GetActualName(Share, Path), Path);
      if (!System.IO.Directory.Exists(si.Path)) return;

      string [] arrFiles = System.IO.Directory.GetFiles(si.Path); 
      for (int i = 0; i < arrFiles.Length; i++)
        AddFile(arrFiles[i], Share);

      if (ShowSubFolders)
      {
        string [] arrFolders = System.IO.Directory.GetDirectories(si.Path); 
        for (int i = 0; i < arrFolders.Length; i++)
        {
          if (FlattenSubFolders)
          {
            AddFolder(arrFolders[i], Share, false);
            LoadFilesAndSubFolders(Share, arrFolders[i], FlattenSubFolders, ShowSubFolders);
          }
          else
          {
            AddFolder(arrFolders[i], Share, true);
            LoadFilesAndSubFolders(Share + "/" + GetActualName(Share, arrFolders[i]), arrFolders[i], FlattenSubFolders, ShowSubFolders);
          }
        }
      }
    }
    private void AddShareItem(string Path)
    {
      AddShareItem(Path, false);
    }
    private void AddShareItem(string Path, bool LoadFolder)
    {
      if (this._Paths.Contains(Path.ToLower())) return;
      ShareItem si = new ShareItem(GetActualName("", Path), Path);
      ShareItem siParent = new ShareItem("", System.IO.Path.GetDirectoryName(Path));
      MountPoint mp;
      
      Hashtable htPath = GetPathTable(siParent.Path);
      foreach (string strShare in htPath.Keys)
      {
        siParent = new ShareItem(GetActualName(strShare, siParent.Path), siParent.Path);
        mp = GetMountPoint(siParent.Path, strShare);
        if (mp.FlattenSubFolders || (mp.Share.Equals(strShare) && mp.ShareName.Equals(strShare + "/" + siParent.Name)))
        {
          if (si.IsFolder)
          {
            if (mp.ShowSubFolders)
            {
              AddFolder(si.Path, strShare, false);
              if (LoadFolder)
                LoadFilesAndSubFolders(strShare, si.Path, mp.FlattenSubFolders, mp.ShowSubFolders);
            }
          }
          else
            AddFile(si.Path, strShare);
        }
        else
        {
          if (si.IsFolder)
          {
            if (mp.ShowSubFolders)
            {
              AddFolder(si.Path, strShare + "/" + siParent.Name, true);
              if (LoadFolder)
                LoadFilesAndSubFolders(strShare + "/" + siParent.Name + "/" + GetActualName(strShare, si.Path), si.Path, mp.FlattenSubFolders, mp.ShowSubFolders);
            }
          }
          else
            AddFile(si.Path, strShare + "/" + siParent.Name);
        }
      }
    }
    private void RemoveShareItem(string Path)
    {
      string strPath = Path.ToLower();
      string sShareName;
      Hashtable htPath;
      Hashtable htShare;
      ArrayList alPaths = new ArrayList(this._Paths.Keys);
      foreach(string sPath in alPaths)
      {
        if (sPath.StartsWith(strPath) && !(sPath.Equals(strPath) && IsDrive(strPath)))
        {
          htPath = GetPathTable(sPath);
          foreach(string sShare in htPath.Keys)
          {
            sShareName = (string) htPath[sShare];
            if (this._Shares.Contains(sShare))
            {
              htShare = GetShareTable(sShare);
              if (htShare.Contains(sShareName))
                htShare.Remove(sShareName);
            }
            sShareName = sShare + "/" + sShareName;
            if (this._Shares.Contains(sShareName))
            {
              htShare = GetShareTable(sShareName);
              htShare.Clear();
              this._Shares.Remove(sShareName);
            }
          }
          htPath.Clear();
          this._Paths.Remove(sPath);
        }
      }
    }
    private void RenameShareItem(string OldPath, string NewPath)
    {
      try
      {
        RemoveShareItem(OldPath);
        AddShareItem(NewPath, true);
      }
      catch (Exception ex)
      {
        string t = ex.Message;
      }

    }
    #endregion Add, Remove and Rename File and Folder methods
    #region Add and Remove Files and Folders from removable media
    private void AddDrive(string Path)
    {
      string sPath = Path.ToLower();
      MountPoint mp;
      foreach(Hashtable ht in this._MountPoints.Values)
      {
        if (ht.Contains(sPath))
        {
          mp = (MountPoint) ht[sPath];

          if (mp.MergeMountPoints)
            LoadFilesAndSubFolders(mp.ParentShareName, mp.Path, mp.FlattenSubFolders, mp.ShowSubFolders);
          else
            LoadFilesAndSubFolders(mp.Share, mp.Path, mp.FlattenSubFolders, mp.ShowSubFolders);
        }
      }
    }
    private void RemoveDrive(string Path)
    {
      string sPath = Path.ToLower();
      MountPoint mp;

      RemoveShareItem(Path);

      foreach(Hashtable ht in this._MountPoints.Values)
      {
        if (ht.Contains(sPath))
        {
          mp = (MountPoint) ht[sPath];

          if (mp.MergeMountPoints)
            LoadFilesAndSubFolders(mp.ParentShareName, mp.Path, mp.FlattenSubFolders, mp.ShowSubFolders);
          else
            LoadFilesAndSubFolders(mp.Share, mp.Path, mp.FlattenSubFolders, mp.ShowSubFolders);
        }
      }
    }
    #endregion Add and Remove Files and Folders from removable media
    #region Hashtable methods
    private Hashtable GetShareTable(string sShare)
    {
      if (this._Shares.Contains(sShare))
        return((Hashtable) this._Shares[sShare]);
      else
      {
        Hashtable ht = new Hashtable(150);
        this._Shares.Add(sShare, ht);
        return(ht);
      }
    }
    private Hashtable GetPathTable(string sPath)
    {
      if (this._Paths.Contains(sPath))
        return((Hashtable) this._Paths[sPath]);
      else
      {
        Hashtable ht = new Hashtable(20);
        this._Paths.Add(sPath, ht);
        return(ht);
      }
    }
    #endregion Hashtable methods
    #region Mountpoints
    private Hashtable GetMountPointTable(string sShare)
    {
      if (this._MountPoints.Contains(sShare))
        return((Hashtable) this._MountPoints[sShare]);
      else
      {
        Hashtable ht = new Hashtable(150);
        this._MountPoints.Add(sShare, ht);
        return(ht);
      }
    }
    private MountPoint AddMountPoint(string Path, string ShareName, string MountPointName, bool MergeMountPoints, bool FlattenSubFolders, bool ShowSubFolders)
    {
      MountPoint mp = new MountPoint(Path, ShareName, MountPointName, MergeMountPoints, FlattenSubFolders, ShowSubFolders);
      Hashtable ht = GetMountPointTable(mp.ParentShareName);
      if (!ht.Contains(mp.Path))
        ht.Add(mp.Path, mp);

      // Add to Paths Hashtable
      ht = GetPathTable(mp.Path);
      ht.Add(ShareName, mp.MountPointName);

      if (!MergeMountPoints)
      {
        // Add to Shares Hashtable
        ht = GetShareTable(ShareName);
        if (!ht.Contains(MountPointName))
        {
          ShareItem si = new ShareItem(ShareName, Path);
          si.Name = MountPointName;
          ht.Add(si.Name, si);
        }
      }
      return(mp);
    }
    private MountPoint GetMountPoint(string Path, string Share)
    {
      string sPath = Path.ToLower();
      int n = Share.IndexOf("/", 1);
      string sShare = Share;
      if (-1 < n)
        sShare = sShare.Substring(0, n);
      Hashtable ht = GetMountPointTable(sShare);
      foreach(string strMpPath in ht.Keys)
      {
        if (sPath.StartsWith(strMpPath))
          return ((MountPoint) ht[strMpPath]);
      }
      return(null);
    }
    #endregion Mountpoints
    #region FileSystemWatcher EventFunctions and Handeling
    private void AddFileSystemWatcher(string Path, string Filter)
    {
      AddFileSystemWatcher(Path, Filter, false);
    }
    private void AddFileSystemWatcher(string Path, bool IncludeSubdirectories)
    {
      AddFileSystemWatcher(Path, "*.*", IncludeSubdirectories);
    }
    private void AddFileSystemWatcher(string Path, string Filter, bool IncludeSubdirectories)
    {
      try
      {
        string sPath = Path.ToLower();
        FileSystemWatcher fsw = new FileSystemWatcher(sPath);
        fsw.BeginInit();
        fsw.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        fsw.Filter = Filter;
        fsw.IncludeSubdirectories = IncludeSubdirectories;
        fsw.InternalBufferSize = 32768;

        // Add event handlers.
        fsw.Changed += new FileSystemEventHandler(OnChanged);
        fsw.Created += new FileSystemEventHandler(OnCreated);
        fsw.Deleted += new FileSystemEventHandler(OnDeleted);
        fsw.Renamed += new RenamedEventHandler(OnRenamed);

        fsw.EnableRaisingEvents = true;
        this._FSW.Add(sPath, fsw);
        fsw.EndInit();
      }
      catch {}
    }
    private void WatchSharesFile()
    {
      try
      {
        string sPath = CSettings.GetSharesFilePath.ToLower();
        int n = sPath.LastIndexOf("\\");
        string sFile = sPath.Substring(n + 1);
        sPath = sPath.Substring(0, n);
        FileSystemWatcher fsw = new FileSystemWatcher(sPath);
        fsw.BeginInit();
        fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
        fsw.Filter = sFile;
        fsw.IncludeSubdirectories = false;
        fsw.InternalBufferSize = 32768;

        // Add event handlers.
        fsw.Changed += new FileSystemEventHandler(OnChanged);

        fsw.EnableRaisingEvents = true;
        this._SharesFile = fsw;
        fsw.EndInit();
      }
      catch {}
    }
    private void ClearFileSystemWatchers()
    {
      if (null == this._FSW) return;
      foreach(FileSystemWatcher fsw in this._FSW.Values)
      {
        fsw.EnableRaisingEvents = false;
      }
      this._FSW.Clear();
      this._FSW = null;
    }
    private void OnChanged(object source, FileSystemEventArgs e)
    {
      if (e.FullPath.ToLower() == CSettings.GetSharesFilePath.ToLower())
      {
        this.LoadShares(CSettings.LoadSharesXMLDom());
      }
    }

    private void OnCreated(object source, FileSystemEventArgs e)
    {
      try
      {
        CLogFile.Log("Shared file/folder '" + e.FullPath + "' created.", "CShareItem", LogLevel.Information);
        AddShareItem(e.FullPath.ToLower(), true);
      }
      catch(Exception ex)
      {
        CLogFile.Log("Error occured in OnCreated: " + ex.Message, "CShareItem", LogLevel.Error);
      }
    }

    private void OnDeleted(object source, FileSystemEventArgs e)
    {
      try
      {
        CLogFile.Log("Shared file/folder '" + e.FullPath + "' deleted.", "CShareItem", LogLevel.Information);
        String strPath = e.FullPath.ToLower();

        RemoveShareItem(strPath);
      }
      catch(Exception ex)
      {
        CLogFile.Log("Error occured in OnDeleted: " + ex.Message, "CShareItem", LogLevel.Error);
      }
    }
    private void OnRenamed(object source, RenamedEventArgs e)
    {
      try
      {
        CLogFile.Log("Shared file/folder '" + e.OldFullPath + "' renamed to '" + e.FullPath + "'.", "CShareItem", LogLevel.Information);

        RenameShareItem(e.OldFullPath, e.FullPath);
      }
      catch(Exception ex)
      {
        CLogFile.Log("Error occured in OnRenamed: " + ex.Message, "CShareItem", LogLevel.Error);
      }
      
    }
    #endregion FileSystemWatcher EventFunctions and Handeling
    #region DeviceWacher EventFunctions and Handeling
    private void AddDeviceWatcher(string DrivePath)
    {
//      CDeviceWatcher cdw = new CDeviceWatcher();
//      cdw.DevicePath = DrivePath;
//      cdw.OnDeviceChanged += new CDeviceEventHandler(OnDeviceChanged);
//      cdw.EnableRaisingEvents = true;
////      this._CDW.Add(DrivePath, cdw);
    }
    private void ClearDeviceWatchers()
    {
//      if (null == this._CDW) return;
//      foreach(CDeviceWatcher cdw in this._CDW.Values)
//      {
//        cdw.EnableRaisingEvents = false;
//      }
//      this._CDW.Clear();
//      this._CDW = null;
    }
//    private void OnDeviceChanged(CDeviceEventArgs e)
//    {
//      if (e.IsInserted)
//        AddDrive(e.Path);
//      else
//        RemoveDrive(e.Path);
//    }
    #endregion DeviceWacher EventFunctions and Handeling

  }
  public class CFileReader
  {
    private FileStream _fsFile;
    private long _nOffset;

    public String _strPath;
    public String Path { get { return(_strPath); } }

    public String _strName;
    public String Name { get { return(_strName); } }

    public CFileReader(String Path)
    {
      this._strPath = Path;
      this._strName = CShares.GetFilename(Path);
    }
    public Boolean Open()
    {
      try 
      {
        if (!System.IO.File.Exists(Path)) return (false);
        if (0 == CShares.GetFileSize(this._strPath)) return (false);
        this._fsFile = new FileStream(this._strPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
        this._nOffset = 0;
        return (true);
      }
      catch
      {
        return (false);
      }
    }
    public void Close()
    {
      try
      {
        this._fsFile.Close();
        this._fsFile = null;
      }
      catch
      {
      }
    }
    public Byte [] Read(Int32 nLength)
    {
      try
      {
        if (!this._fsFile.CanRead)
          return (null);
        if (this._nOffset + nLength > CShares.GetFileSize(this._strPath))
          nLength = (Int32) (CShares.GetFileSize(this._strPath) - this._nOffset);
        if (1 > nLength) return (null);
        Byte [] pbRead = new Byte[nLength];
        this._fsFile.Read(pbRead, 0, nLength);
        this._nOffset += nLength;
        return (pbRead);
      }
      catch 
      {
        return (null);
      }
    }
    public Boolean Seek(FileSeekType SeekType, long nLength)
    {
      long nOffset = 0;
      if (!this._fsFile.CanSeek)
        return (false);
      try
      {
        switch(SeekType)
        {
          case FileSeekType.Backward :
            nOffset = this._fsFile.Seek(0 - nLength, SeekOrigin.Current);
            break;
          case FileSeekType.BackwardEOF :
            nOffset = this._fsFile.Seek(0 - nLength, SeekOrigin.End);
            break;
          case FileSeekType.Forward :
            nOffset = this._fsFile.Seek(nLength, SeekOrigin.Current);
            break;
          case FileSeekType.ForwardBOF :
            nOffset = this._fsFile.Seek(nLength, SeekOrigin.Begin);
            break;
        }
        if (nOffset > CShares.GetFileSize(this._strPath) || nOffset < 0)
        {
          this._nOffset = this._fsFile.Seek(this._nOffset, SeekOrigin.Begin);
          return (false);
        }
        else
        {
          this._nOffset = nOffset;
          return (true);
        }
      }
      catch
      {
        return (false);
      }
    }
    public override int GetHashCode()
    {
      return (this._fsFile.Handle.ToInt32());
    }
  }  
  public enum FileSeekType
  {
    ForwardBOF = 0,
    BackwardEOF = 1,
    Forward = 2,
    Backward = 3
  }

  public class CShareItemComparer : IComparer 
  {
    public int Compare(object x, object y)
    {
      return(new CaseInsensitiveComparer().Compare(((ShareItem) x).Name, ((ShareItem) y).Name));
    }
  }
  public class ShareItem
  {
    public bool IsFolder;
    public string Path;
    public string Name;
    public ShareItem()
    {
    }
    public ShareItem(string Name, string Path)
    {
      this.IsFolder = CShares.IsFolder(Path);
      this.Path = Path.ToLower();
      this.Name = Name;
    }
  }
  public class MountPoint
  {
    public string Path;
    public string ParentShareName;
    public string MountPointName;
    public string ShareName;
    public bool MergeMountPoints;
    public bool FlattenSubFolders;
    public bool ShowSubFolders;
    public bool IsDrive;
    public string Share
    {
      get
      {
        if (MergeMountPoints)
          return(this.ParentShareName);
        else
          return(this.ShareName);
      }
    }

    public MountPoint(string Path, string ShareName, string MountPointName, bool MergeMountPoints, bool FlattenSubFolders, bool ShowSubFolders)
    {
      this.Path = Path.ToLower();
      this.ParentShareName = ShareName;
      this.MountPointName = MountPointName;
      this.MergeMountPoints = MergeMountPoints;
      this.FlattenSubFolders = FlattenSubFolders;
      this.ShowSubFolders = ShowSubFolders;
      this.ShareName = ShareName + "/" + MountPointName;
      this.IsDrive = CShares.IsDrive(Path);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.