| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Reflection;
 
- using System.Globalization;
 
- using System.Runtime.InteropServices;
 
- using System.Threading;
 
- using System.Xml;
 
- using Microsoft.Win32;
 
- using System.Diagnostics;
 
- using System.Security;
 
- using System.Text.RegularExpressions;
 
- namespace WinSCP
 
- {
 
-     [Guid("38649D44-B839-4F2C-A9DC-5D45EEA4B5E9")]
 
-     [ComVisible(true)]
 
-     public enum SynchronizationMode
 
-     {
 
-         Local = 0,
 
-         Remote = 1,
 
-         Both = 2,
 
-     }
 
-     [Guid("3F770EC1-35F5-4A7B-A000-46A2F7A213D8")]
 
-     [ComVisible(true)]
 
-     [Flags]
 
-     public enum SynchronizationCriteria
 
-     {
 
-         None = 0x00,
 
-         Time = 0x01,
 
-         Size = 0x02,
 
-         Either = Time | Size,
 
-     }
 
-     [Guid("6C441F60-26AA-44FC-9B93-08884768507B")]
 
-     [ComVisible(true)]
 
-     [Flags]
 
-     public enum EnumerationOptions
 
-     {
 
-         None = 0x00,
 
-         AllDirectories = 0x01,
 
-         MatchDirectories = 0x02,
 
-         EnumerateDirectories = 0x04,
 
-     }
 
-     [Guid("16B6D8F6-C0B4-487D-9546-A25BBF582ED6")]
 
-     [ComVisible(true)]
 
-     public enum ProgressSide
 
-     {
 
-         Local = 0,
 
-         Remote = 1,
 
-     }
 
-     public delegate void OutputDataReceivedEventHandler(object sender, OutputDataReceivedEventArgs e);
 
-     public delegate void FileTransferredEventHandler(object sender, TransferEventArgs e);
 
-     public delegate void FileTransferProgressEventHandler(object sender, FileTransferProgressEventArgs e);
 
-     public delegate void FailedEventHandler(object sender, FailedEventArgs e);
 
-     [Guid("56FFC5CE-3867-4EF0-A3B5-CFFBEB99EA35")]
 
-     [ClassInterface(Constants.ClassInterface)]
 
-     [ComVisible(true)]
 
-     [ComSourceInterfaces(typeof(ISessionEvents))]
 
-     public sealed class Session : IDisposable, IReflect
 
-     {
 
-         public string ExecutablePath { get { return _executablePath; } set { CheckNotOpened(); _executablePath = value; } }
 
-         public string ExecutableProcessUserName { get { return _executableProcessUserName; } set { CheckNotOpened(); _executableProcessUserName = value; } }
 
-         public SecureString ExecutableProcessPassword { get { return _executableProcessPassword; } set { CheckNotOpened(); _executableProcessPassword = value; } }
 
-         public string AdditionalExecutableArguments { get { return _additionalExecutableArguments; } set { CheckNotOpened(); _additionalExecutableArguments = value; } }
 
-         [Obsolete("Use AddRawConfiguration")]
 
-         public bool DefaultConfiguration { get { return _defaultConfiguration; } set { CheckNotOpened(); _defaultConfiguration = value; } }
 
-         public bool DisableVersionCheck { get { return GetDisableVersionCheck(); } set { CheckNotOpened(); _disableVersionCheck = value; } }
 
-         [Obsolete("Use AddRawConfiguration")]
 
-         public string IniFilePath { get { return _iniFilePath; } set { CheckNotOpened(); _iniFilePath = value; } }
 
-         public TimeSpan ReconnectTime { get { return _reconnectTime; } set { CheckNotOpened(); _reconnectTime = value; } }
 
-         public int ReconnectTimeInMilliseconds { get { return Tools.TimeSpanToMilliseconds(ReconnectTime); } set { ReconnectTime = Tools.MillisecondsToTimeSpan(value); } }
 
-         public string DebugLogPath { get { CheckNotDisposed(); return Logger.LogPath; } set { CheckNotDisposed(); Logger.LogPath = value; } }
 
-         public int DebugLogLevel { get { CheckNotDisposed(); return Logger.LogLevel; } set { CheckNotDisposed(); Logger.LogLevel = value; } }
 
-         public string SessionLogPath { get { return _sessionLogPath; } set { CheckNotOpened(); _sessionLogPath = value; } }
 
-         public string XmlLogPath { get { return _xmlLogPath; } set { CheckNotOpened(); _xmlLogPath = value; } }
 
-         #if DEBUG
 
-         public bool GuardProcessWithJob { get { return GuardProcessWithJobInternal; } set { GuardProcessWithJobInternal = value; } }
 
-         public bool TestHandlesClosed { get { return TestHandlesClosedInternal; } set { TestHandlesClosedInternal = value; } }
 
-         #endif
 
-         public string HomePath { get { CheckOpened(); return _homePath; } }
 
-         public TimeSpan Timeout { get; set; }
 
-         public StringCollection Output { get; private set; }
 
-         public bool Opened { get { CheckNotDisposed(); return (_process != null); } }
 
-         public event FileTransferredEventHandler FileTransferred;
 
-         public event FailedEventHandler Failed;
 
-         public event OutputDataReceivedEventHandler OutputDataReceived;
 
-         public event FileTransferProgressEventHandler FileTransferProgress
 
-         {
 
-             add
 
-             {
 
-                 using (Logger.CreateCallstackAndLock())
 
-                 {
 
-                     CheckNotOpened();
 
-                     _fileTransferProgress += value;
 
-                 }
 
-             }
 
-             remove
 
-             {
 
-                 using (Logger.CreateCallstackAndLock())
 
-                 {
 
-                     CheckNotOpened();
 
-                     _fileTransferProgress -= value;
 
-                 }
 
-             }
 
-         }
 
-         public Session()
 
-         {
 
-             Logger = new Logger();
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 Timeout = new TimeSpan(0, 1, 0);
 
-                 _reconnectTime = new TimeSpan(0, 2, 0); // keep in sync with TScript::OptionImpl
 
-                 ResetOutput();
 
-                 _operationResults = new List<OperationResultBase>();
 
-                 _events = new List<Action>();
 
-                 _eventsEvent = new AutoResetEvent(false);
 
-                 _disposed = false;
 
-                 _defaultConfiguration = true;
 
-                 _logUnique = 0;
 
-                 _guardProcessWithJob = true;
 
-                 RawConfiguration = new Dictionary<string, string>();
 
-             }
 
-         }
 
-         private void ResetOutput()
 
-         {
 
-             Output = new StringCollection();
 
-             _error = new StringCollection();
 
-         }
 
-         public void Dispose()
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 _disposed = true;
 
-                 Cleanup();
 
-                 Logger.Dispose();
 
-                 if (_eventsEvent != null)
 
-                 {
 
-                     _eventsEvent.Close();
 
-                     _eventsEvent = null;
 
-                 }
 
-                 GC.SuppressFinalize(this);
 
-             }
 
-         }
 
-         public void Abort()
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 CheckOpened();
 
-                 _aborted = true;
 
-                 // double-check
 
-                 if (_process != null)
 
-                 {
 
-                     _process.Abort();
 
-                 }
 
-             }
 
-         }
 
-         public void Open(SessionOptions sessionOptions)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckNotDisposed();
 
-                 if (Opened)
 
-                 {
 
-                     throw new InvalidOperationException("Session is already opened");
 
-                 }
 
-                 try
 
-                 {
 
-                     SetupTempPath();
 
-                     ResetOutput();
 
-                     _process = ExeSessionProcess.CreateForSession(this);
 
-                     _process.OutputDataReceived += ProcessOutputDataReceived;
 
-                     _process.Start();
 
-                     GotOutput();
 
-                     // setup batch mode
 
-                     WriteCommand("option batch on");
 
-                     WriteCommand("option confirm off");
 
-                     object reconnectTimeValue;
 
-                     if (ReconnectTime != TimeSpan.MaxValue)
 
-                     {
 
-                         reconnectTimeValue = (int)ReconnectTime.TotalSeconds;
 
-                     }
 
-                     else
 
-                     {
 
-                         reconnectTimeValue = "off";
 
-                     }
 
-                     string reconnectTimeCommand =
 
-                         string.Format(CultureInfo.InvariantCulture, "option reconnecttime {0}", reconnectTimeValue);
 
-                     WriteCommand(reconnectTimeCommand);
 
-                     string command;
 
-                     string log;
 
-                     SessionOptionsToUrlAndSwitches(sessionOptions, false, out command, out log);
 
-                     const string openCommand = "open ";
 
-                     command = openCommand + command;
 
-                     log = openCommand + log;
 
-                     WriteCommand(command, log);
 
-                     // Wait until the log file gets created or WinSCP terminates (in case of fatal error)
 
-                     do
 
-                     {
 
-                         string logExplanation;
 
-                         lock (Output)
 
-                         {
 
-                             if (_error.Count > 0)
 
-                             {
 
-                                 logExplanation = GetErrorOutputMessage();
 
-                             }
 
-                             else if (Output.Count > 0)
 
-                             {
 
-                                 logExplanation =
 
-                                     string.Format(
 
-                                         CultureInfo.CurrentCulture, "Output was \"{0}\". ", ListToString(Output));
 
-                             }
 
-                             else
 
-                             {
 
-                                 logExplanation = "There was no output. ";
 
-                             }
 
-                         }
 
-                         logExplanation +=
 
-                             string.Format(CultureInfo.CurrentCulture,
 
-                                 "Response log file {0} was not created. This could indicate lack of write permissions to the log folder or problems starting WinSCP itself.",
 
-                                 XmlLogPath);
 
-                         if (_process.HasExited && !File.Exists(XmlLogPath))
 
-                         {
 
-                             Logger.WriteCounters();
 
-                             Logger.WriteProcesses();
 
-                             _process.WriteStatus();
 
-                             string exitCode = string.Format(CultureInfo.CurrentCulture, "{0}", _process.ExitCode);
 
-                             if (_process.ExitCode < 0)
 
-                             {
 
-                                 exitCode = string.Format(CultureInfo.CurrentCulture, "{0} ({1:X})", exitCode, _process.ExitCode);
 
-                             }
 
-                             throw new SessionLocalException(this,
 
-                                 string.Format(CultureInfo.CurrentCulture, "WinSCP process terminated with exit code {0}. ", exitCode) +
 
-                                 logExplanation);
 
-                         }
 
-                         Thread.Sleep(50);
 
-                         CheckForTimeout(
 
-                             string.Format(CultureInfo.CurrentCulture, "WinSCP has not responded in time.") +
 
-                             logExplanation);
 
-                     } while (!File.Exists(XmlLogPath));
 
-                     _logReader = new SessionLogReader(this);
 
-                     _logReader.WaitForNonEmptyElement("session", LogReadFlags.ThrowFailures);
 
-                     // special variant of ElementLogReader that throws when closing element (</session>) is encountered
 
-                     _reader = new SessionElementLogReader(_logReader);
 
-                     // Skip "open" command <group>
 
-                     using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                     {
 
-                         ReadElement(groupReader, LogReadFlags.ThrowFailures);
 
-                     }
 
-                     WriteCommand("pwd");
 
-                     using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                     using (ElementLogReader cwdReader = groupReader.WaitForNonEmptyElementAndCreateLogReader("cwd", LogReadFlags.ThrowFailures))
 
-                     {
 
-                         while (cwdReader.Read(0))
 
-                         {
 
-                             string value;
 
-                             if (cwdReader.GetEmptyElementValue("cwd", out value))
 
-                             {
 
-                                 _homePath = value;
 
-                             }
 
-                         }
 
-                         groupReader.ReadToEnd(LogReadFlags.ThrowFailures);
 
-                     }
 
-                 }
 
-                 catch (Exception e)
 
-                 {
 
-                     Logger.WriteLine("Exception: {0}", e);
 
-                     Cleanup();
 
-                     throw;
 
-                 }
 
-             }
 
-         }
 
-         internal string GetErrorOutputMessage()
 
-         {
 
-             string result = null;
 
-             if (_error.Count > 0)
 
-             {
 
-                 result = string.Format(CultureInfo.CurrentCulture, "Error output was \"{0}\". ", ListToString(_error));
 
-             }
 
-             return result;
 
-         }
 
-         private static string ListToString(StringCollection list)
 
-         {
 
-             string[] error = new string[list.Count];
 
-             list.CopyTo(error, 0);
 
-             string s = string.Join(Environment.NewLine, error);
 
-             return s;
 
-         }
 
-         public string ScanFingerprint(SessionOptions sessionOptions)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 string result;
 
-                 CheckNotDisposed();
 
-                 if (Opened)
 
-                 {
 
-                     throw new InvalidOperationException("Session is already opened");
 
-                 }
 
-                 try
 
-                 {
 
-                     ResetOutput();
 
-                     string command;
 
-                     string log; // unused
 
-                     SessionOptionsToUrlAndSwitches(sessionOptions, true, out command, out log);
 
-                     string additionalArguments = "/fingerprintscan " + command;
 
-                     _process = ExeSessionProcess.CreateForConsole(this, additionalArguments);
 
-                     _process.OutputDataReceived += ProcessOutputDataReceived;
 
-                     _process.Start();
 
-                     GotOutput();
 
-                     while (!_process.HasExited)
 
-                     {
 
-                         Thread.Sleep(50);
 
-                         CheckForTimeout();
 
-                     }
 
-                     string output = string.Join(Environment.NewLine, new List<string>(Output).ToArray());
 
-                     if (_process.ExitCode == 0)
 
-                     {
 
-                         result = output;
 
-                     }
 
-                     else
 
-                     {
 
-                         throw new SessionRemoteException(this, output);
 
-                     }
 
-                 }
 
-                 catch (Exception e)
 
-                 {
 
-                     Logger.WriteLine("Exception: {0}", e);
 
-                     throw;
 
-                 }
 
-                 finally
 
-                 {
 
-                     Cleanup();
 
-                 }
 
-                 return result;
 
-             }
 
-         }
 
-         public void Close()
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 Cleanup();
 
-             }
 
-         }
 
-         public RemoteDirectoryInfo ListDirectory(string path)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 WriteCommand(string.Format(CultureInfo.InvariantCulture, "ls -- \"{0}\"", Tools.ArgumentEscape(IncludeTrailingSlash(path))));
 
-                 RemoteDirectoryInfo result = new RemoteDirectoryInfo();
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (ElementLogReader lsReader = groupReader.WaitForNonEmptyElementAndCreateLogReader("ls", LogReadFlags.ThrowFailures))
 
-                 {
 
-                     string destination = null;
 
-                     if (lsReader.TryWaitForEmptyElement("destination", 0))
 
-                     {
 
-                         lsReader.GetEmptyElementValue("destination", out destination);
 
-                     }
 
-                     if ((destination != null) && lsReader.TryWaitForNonEmptyElement("files", 0))
 
-                     {
 
-                         destination = IncludeTrailingSlash(destination);
 
-                         using (ElementLogReader filesReader = lsReader.CreateLogReader())
 
-                         {
 
-                             while (filesReader.TryWaitForNonEmptyElement("file", 0))
 
-                             {
 
-                                 RemoteFileInfo fileInfo = new RemoteFileInfo();
 
-                                 using (ElementLogReader fileReader = filesReader.CreateLogReader())
 
-                                 {
 
-                                     while (fileReader.Read(0))
 
-                                     {
 
-                                         string value;
 
-                                         if (fileReader.GetEmptyElementValue("filename", out value))
 
-                                         {
 
-                                             fileInfo.Name = value;
 
-                                             fileInfo.FullName = destination + value;
 
-                                         }
 
-                                         else
 
-                                         {
 
-                                             ReadFile(fileInfo, fileReader);
 
-                                         }
 
-                                     }
 
-                                     result.AddFile(fileInfo);
 
-                                 }
 
-                             }
 
-                         }
 
-                         groupReader.ReadToEnd(LogReadFlags.ThrowFailures);
 
-                     }
 
-                     else
 
-                     {
 
-                         // "files" not found, keep reading, we expect "failure"
 
-                         // This happens only in case of fatal errors,
 
-                         // in case of normal error (non existing folder),
 
-                         // the "failure" is caught in "group" already, before the "ls".
 
-                         groupReader.ReadToEnd(LogReadFlags.ThrowFailures);
 
-                         // only if not "failure", throw "files" not found
 
-                         throw SessionLocalException.CreateElementNotFound(this, "files");
 
-                     }
 
-                 }
 
-                 return result;
 
-             }
 
-         }
 
-         private IEnumerable<RemoteFileInfo> DoEnumerateRemoteFiles(string path, Regex regex, EnumerationOptions options, bool throwReadErrors)
 
-         {
 
-             bool allDirectories = ((options & EnumerationOptions.AllDirectories) == EnumerationOptions.AllDirectories);
 
-             bool matchDirectories = ((options & EnumerationOptions.MatchDirectories) == EnumerationOptions.MatchDirectories);
 
-             bool enumerateDirectories = ((options & EnumerationOptions.EnumerateDirectories) == EnumerationOptions.EnumerateDirectories);
 
-             if (enumerateDirectories && !allDirectories)
 
-             {
 
-                 throw new ArgumentException("Cannot use enumeration option EnumerateDirectories without AllDirectories");
 
-             }
 
-             if (enumerateDirectories && matchDirectories)
 
-             {
 
-                 throw new ArgumentException("Cannot combine enumeration option EnumerateDirectories with MatchDirectories");
 
-             }
 
-             RemoteDirectoryInfo directoryInfo;
 
-             try
 
-             {
 
-                 // Need to use guarded method for the listing, see a comment in EnumerateRemoteFiles
 
-                 directoryInfo = ListDirectory(path);
 
-             }
 
-             catch (SessionRemoteException)
 
-             {
 
-                 if (throwReadErrors)
 
-                 {
 
-                     throw;
 
-                 }
 
-                 else
 
-                 {
 
-                     directoryInfo = null;
 
-                 }
 
-             }
 
-             if (directoryInfo != null)
 
-             {
 
-                 foreach (RemoteFileInfo fileInfo in directoryInfo.Files)
 
-                 {
 
-                     if (!fileInfo.IsThisDirectory && !fileInfo.IsParentDirectory)
 
-                     {
 
-                         bool matches = regex.IsMatch(fileInfo.Name);
 
-                         bool enumerate;
 
-                         if (!fileInfo.IsDirectory)
 
-                         {
 
-                             enumerate = matches;
 
-                         }
 
-                         else
 
-                         {
 
-                             if (enumerateDirectories)
 
-                             {
 
-                                 enumerate = true;
 
-                             }
 
-                             else if (matchDirectories)
 
-                             {
 
-                                 enumerate = matches;
 
-                             }
 
-                             else
 
-                             {
 
-                                 enumerate = false;
 
-                             }
 
-                         }
 
-                         if (enumerate)
 
-                         {
 
-                             yield return fileInfo;
 
-                         }
 
-                         if (fileInfo.IsDirectory && allDirectories)
 
-                         {
 
-                             foreach (RemoteFileInfo fileInfo2 in DoEnumerateRemoteFiles(CombinePaths(path, fileInfo.Name), regex, options, false))
 
-                             {
 
-                                 yield return fileInfo2;
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         public IEnumerable<RemoteFileInfo> EnumerateRemoteFiles(string path, string mask, EnumerationOptions options)
 
-         {
 
-             // Note that this method exits as soon as DoEnumerateRemoteFiles is entered,
 
-             // so the Session object is not guarded during the whole enumeration.
 
-             // Though it should not matter as it uses only guarded methods (ListDirectory)
 
-             // for the actual work on the session
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 Regex regex = MaskToRegex(mask);
 
-                 return DoEnumerateRemoteFiles(path, regex, options, true);
 
-             }
 
-         }
 
-         private static Regex MaskToRegex(string mask)
 
-         {
 
-             if (string.IsNullOrEmpty(mask) ||
 
-                 // *.* has to match even filename without dot
 
-                 (mask == "*.*"))
 
-             {
 
-                 mask = "*";
 
-             }
 
-             return
 
-                 new Regex(
 
-                     '^' +
 
-                     mask
 
-                         .Replace(".", "[.]")
 
-                         .Replace("*", ".*")
 
-                         .Replace("?", ".") +
 
-                     '$',
 
-                     RegexOptions.IgnoreCase);
 
-         }
 
-         public TransferOperationResult PutFiles(string localPath, string remotePath, bool remove = false, TransferOptions options = null)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 if (options == null)
 
-                 {
 
-                     options = new TransferOptions();
 
-                 }
 
-                 CheckOpened();
 
-                 WriteCommand(
 
-                     string.Format(CultureInfo.InvariantCulture,
 
-                         "put {0} {1} -- \"{2}\" \"{3}\"",
 
-                         BooleanSwitch(remove, "delete"), options.ToSwitches(),
 
-                         Tools.ArgumentEscape(localPath), Tools.ArgumentEscape(remotePath)));
 
-                 TransferOperationResult result = new TransferOperationResult();
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (RegisterOperationResult(result))
 
-                 using (CreateProgressHandler())
 
-                 {
 
-                     TransferEventArgs args = null;
 
-                     bool mkdir = false;
 
-                     while (groupReader.Read(0))
 
-                     {
 
-                         if (groupReader.IsNonEmptyElement(TransferEventArgs.UploadTag))
 
-                         {
 
-                             AddTransfer(result, args);
 
-                             args = TransferEventArgs.Read(ProgressSide.Local, groupReader);
 
-                             mkdir = false;
 
-                         }
 
-                         else if (groupReader.IsNonEmptyElement(TransferEventArgs.MkDirTag))
 
-                         {
 
-                             AddTransfer(result, args);
 
-                             args = null;
 
-                             mkdir = true;
 
-                             // For now, silently ignoring results (even errors)
 
-                             // of mkdir operation, including future chmod/touch
 
-                         }
 
-                         else if (groupReader.IsNonEmptyElement(ChmodEventArgs.Tag))
 
-                         {
 
-                             if (!mkdir)
 
-                             {
 
-                                 if (args == null)
 
-                                 {
 
-                                     throw new InvalidOperationException("Tag chmod before tag upload");
 
-                                 }
 
-                                 args.Chmod = ChmodEventArgs.Read(groupReader);
 
-                             }
 
-                         }
 
-                         else if (groupReader.IsNonEmptyElement(TouchEventArgs.Tag))
 
-                         {
 
-                             if (!mkdir)
 
-                             {
 
-                                 if (args == null)
 
-                                 {
 
-                                     throw new InvalidOperationException("Tag touch before tag upload");
 
-                                 }
 
-                                 args.Touch = TouchEventArgs.Read(groupReader);
 
-                             }
 
-                         }
 
-                     }
 
-                     AddTransfer(result, args);
 
-                 }
 
-                 return result;
 
-             }
 
-         }
 
-         private void AddTransfer(TransferOperationResult result, TransferEventArgs args)
 
-         {
 
-             if (args != null)
 
-             {
 
-                 result.AddTransfer(args);
 
-                 RaiseFileTransferredEvent(args);
 
-             }
 
-         }
 
-         public TransferOperationResult GetFiles(string remotePath, string localPath, bool remove = false, TransferOptions options = null)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 if (options == null)
 
-                 {
 
-                     options = new TransferOptions();
 
-                 }
 
-                 CheckOpened();
 
-                 WriteCommand(
 
-                     string.Format(CultureInfo.InvariantCulture, "get {0} {1} -- \"{2}\" \"{3}\"",
 
-                         BooleanSwitch(remove, "delete"), options.ToSwitches(),
 
-                         Tools.ArgumentEscape(remotePath), Tools.ArgumentEscape(localPath)));
 
-                 TransferOperationResult result = new TransferOperationResult();
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (RegisterOperationResult(result))
 
-                 using (CreateProgressHandler())
 
-                 {
 
-                     TransferEventArgs args = null;
 
-                     while (groupReader.Read(0))
 
-                     {
 
-                         if (groupReader.IsNonEmptyElement(TransferEventArgs.DownloadTag))
 
-                         {
 
-                             AddTransfer(result, args);
 
-                             args = TransferEventArgs.Read(ProgressSide.Remote, groupReader);
 
-                         }
 
-                         else if (groupReader.IsNonEmptyElement(RemovalEventArgs.Tag))
 
-                         {
 
-                             // When "downloading and deleting" a folder,
 
-                             // we get "rm" tag without preceeding "download" tag.
 
-                             // So we use only the first "rm" tag after preceeding "download" tag,
 
-                             // silently ignoring the others
 
-                             if ((args != null) && (args.Removal == null))
 
-                             {
 
-                                 args.Removal = RemovalEventArgs.Read(groupReader);
 
-                             }
 
-                         }
 
-                     }
 
-                     AddTransfer(result, args);
 
-                 }
 
-                 return result;
 
-             }
 
-         }
 
-         public RemovalOperationResult RemoveFiles(string path)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 WriteCommand(string.Format(CultureInfo.InvariantCulture, "rm -- \"{0}\"", Tools.ArgumentEscape(path)));
 
-                 RemovalOperationResult result = new RemovalOperationResult();
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (RegisterOperationResult(result))
 
-                 {
 
-                     while (groupReader.Read(0))
 
-                     {
 
-                         if (groupReader.IsNonEmptyElement(RemovalEventArgs.Tag))
 
-                         {
 
-                             result.AddRemoval(RemovalEventArgs.Read(groupReader));
 
-                         }
 
-                     }
 
-                 }
 
-                 return result;
 
-             }
 
-         }
 
-         public SynchronizationResult SynchronizeDirectories(
 
-             SynchronizationMode mode, string localPath, string remotePath,
 
-             bool removeFiles, bool mirror = false, SynchronizationCriteria criteria = SynchronizationCriteria.Time,
 
-             TransferOptions options = null)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 if (options == null)
 
-                 {
 
-                     options = new TransferOptions();
 
-                 }
 
-                 CheckOpened();
 
-                 if (removeFiles && (mode == SynchronizationMode.Both))
 
-                 {
 
-                     throw new ArgumentException("Cannot delete files in synchronization mode Both");
 
-                 }
 
-                 if (mirror && (mode == SynchronizationMode.Both))
 
-                 {
 
-                     throw new ArgumentException("Cannot mirror files in synchronization mode Both");
 
-                 }
 
-                 if ((criteria != SynchronizationCriteria.Time) && (mode == SynchronizationMode.Both))
 
-                 {
 
-                     throw new ArgumentException("Only Time criteria is allowed in synchronization mode Both");
 
-                 }
 
-                 string modeName;
 
-                 switch (mode)
 
-                 {
 
-                     case SynchronizationMode.Local:
 
-                         modeName = "local";
 
-                         break;
 
-                     case SynchronizationMode.Remote:
 
-                         modeName = "remote";
 
-                         break;
 
-                     case SynchronizationMode.Both:
 
-                         modeName = "both";
 
-                         break;
 
-                     default:
 
-                         throw new ArgumentOutOfRangeException("mode");
 
-                 }
 
-                 string criteriaName;
 
-                 switch (criteria)
 
-                 {
 
-                     case SynchronizationCriteria.None:
 
-                         criteriaName = "none";
 
-                         break;
 
-                     case SynchronizationCriteria.Time:
 
-                         criteriaName = "time";
 
-                         break;
 
-                     case SynchronizationCriteria.Size:
 
-                         criteriaName = "size";
 
-                         break;
 
-                     case SynchronizationCriteria.Either:
 
-                         criteriaName = "either";
 
-                         break;
 
-                     default:
 
-                         throw new ArgumentOutOfRangeException("criteria");
 
-                 }
 
-                 WriteCommand(
 
-                     string.Format(CultureInfo.InvariantCulture,
 
-                         "synchronize {0} {1} {2} {3} -criteria=\"{4}\" -- \"{5}\" \"{6}\"",
 
-                         modeName,
 
-                         BooleanSwitch(removeFiles, "delete"),
 
-                         BooleanSwitch(mirror, "mirror"),
 
-                         options.ToSwitches(),
 
-                         criteriaName,
 
-                         Tools.ArgumentEscape(localPath), Tools.ArgumentEscape(remotePath)));
 
-                 return ReadSynchronizeDirectories();
 
-             }
 
-         }
 
-         private SynchronizationResult ReadSynchronizeDirectories()
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 SynchronizationResult result = new SynchronizationResult();
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (RegisterOperationResult(result))
 
-                 using (CreateProgressHandler())
 
-                 {
 
-                     TransferEventArgs transfer = null;
 
-                     while (groupReader.Read(0))
 
-                     {
 
-                         ProgressSide? newSide = null;
 
-                         if (groupReader.IsNonEmptyElement(TransferEventArgs.UploadTag))
 
-                         {
 
-                             newSide = ProgressSide.Local;
 
-                         }
 
-                         else if (groupReader.IsNonEmptyElement(TransferEventArgs.DownloadTag))
 
-                         {
 
-                             newSide = ProgressSide.Remote;
 
-                         }
 
-                         if (newSide.HasValue)
 
-                         {
 
-                             AddSynchronizationTransfer(result, transfer);
 
-                             transfer = TransferEventArgs.Read(newSide.Value, groupReader);
 
-                         }
 
-                         else if (groupReader.IsNonEmptyElement(RemovalEventArgs.Tag))
 
-                         {
 
-                             result.AddRemoval(RemovalEventArgs.Read(groupReader));
 
-                         }
 
-                         else if (groupReader.IsNonEmptyElement(ChmodEventArgs.Tag))
 
-                         {
 
-                             if (transfer == null)
 
-                             {
 
-                                 throw new InvalidOperationException("Tag chmod before tag download");
 
-                             }
 
-                             transfer.Chmod = ChmodEventArgs.Read(groupReader);
 
-                         }
 
-                         else if (groupReader.IsNonEmptyElement(TouchEventArgs.Tag))
 
-                         {
 
-                             if (transfer == null)
 
-                             {
 
-                                 throw new InvalidOperationException("Tag touch before tag download");
 
-                             }
 
-                             transfer.Touch = TouchEventArgs.Read(groupReader);
 
-                         }
 
-                     }
 
-                     AddSynchronizationTransfer(result, transfer);
 
-                 }
 
-                 return result;
 
-             }
 
-         }
 
-         public CommandExecutionResult ExecuteCommand(string command)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 WriteCommand(string.Format(CultureInfo.InvariantCulture, "call {0}", command));
 
-                 CommandExecutionResult result = new CommandExecutionResult();
 
-                 // registering before creating group reader, so that
 
-                 // it is still registered, when group reader is read to the end in its .Dispose();
 
-                 using (RegisterOperationResult(result))
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (ElementLogReader callReader = groupReader.WaitForNonEmptyElementAndCreateLogReader("call", LogReadFlags.ThrowFailures))
 
-                 {
 
-                     while (callReader.Read(0))
 
-                     {
 
-                         string value;
 
-                         if (callReader.GetEmptyElementValue("output", out value))
 
-                         {
 
-                             result.Output = value;
 
-                         }
 
-                         if (callReader.GetEmptyElementValue("erroroutput", out value))
 
-                         {
 
-                             result.ErrorOutput = value;
 
-                         }
 
-                         if (callReader.GetEmptyElementValue("exitcode", out value))
 
-                         {
 
-                             result.ExitCode = int.Parse(value, CultureInfo.InvariantCulture);
 
-                         }
 
-                     }
 
-                 }
 
-                 return result;
 
-             }
 
-         }
 
-         public RemoteFileInfo GetFileInfo(string path)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 return DoGetFileInfo(path);
 
-             }
 
-         }
 
-         public bool FileExists(string path)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 try
 
-                 {
 
-                     DoGetFileInfo(path);
 
-                     return true;
 
-                 }
 
-                 catch (SessionRemoteException)
 
-                 {
 
-                     return false;
 
-                 }
 
-             }
 
-         }
 
-         public byte[] CalculateFileChecksum(string algorithm, string path)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 WriteCommand(string.Format(CultureInfo.InvariantCulture, "checksum -- \"{0}\" \"{1}\"", Tools.ArgumentEscape(algorithm), Tools.ArgumentEscape(path)));
 
-                 string hex = null;
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (ElementLogReader checksumReader = groupReader.WaitForNonEmptyElementAndCreateLogReader("checksum", LogReadFlags.ThrowFailures))
 
-                 {
 
-                     while (checksumReader.Read(0))
 
-                     {
 
-                         string value;
 
-                         if (checksumReader.GetEmptyElementValue("checksum", out value))
 
-                         {
 
-                             hex = value;
 
-                         }
 
-                     }
 
-                     groupReader.ReadToEnd(LogReadFlags.ThrowFailures);
 
-                 }
 
-                 int len = hex.Length;
 
-                 if ((len % 2) != 0)
 
-                 {
 
-                     string error = string.Format(CultureInfo.CurrentCulture, "Invalid string representation of checksum - {0}", hex);
 
-                     throw new SessionLocalException(this, error);
 
-                 }
 
-                 int count = len / 2;
 
-                 byte[] bytes = new byte[count];
 
-                 for (int i = 0; i < count; i++)
 
-                 {
 
-                     bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
 
-                 }
 
-                 return bytes;
 
-             }
 
-         }
 
-         public void CreateDirectory(string path)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 WriteCommand(string.Format(CultureInfo.InvariantCulture, "mkdir \"{0}\"", Tools.ArgumentEscape(path)));
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (ElementLogReader mkdirReader = groupReader.WaitForNonEmptyElementAndCreateLogReader(TransferEventArgs.MkDirTag, LogReadFlags.ThrowFailures))
 
-                 {
 
-                     ReadElement(mkdirReader, 0);
 
-                     groupReader.ReadToEnd(LogReadFlags.ThrowFailures);
 
-                 }
 
-             }
 
-         }
 
-         public void MoveFile(string sourcePath, string targetPath)
 
-         {
 
-             using (Logger.CreateCallstackAndLock())
 
-             {
 
-                 CheckOpened();
 
-                 WriteCommand(string.Format(CultureInfo.InvariantCulture, "mv \"{0}\" \"{1}\"", Tools.ArgumentEscape(sourcePath), Tools.ArgumentEscape(targetPath)));
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 {
 
-                     if (!groupReader.TryWaitForNonEmptyElement("mv", LogReadFlags.ThrowFailures))
 
-                     {
 
-                         throw new SessionRemoteException(this, string.Format(CultureInfo.CurrentCulture, "{0} not found.", sourcePath));
 
-                     }
 
-                     else
 
-                     {
 
-                         using (ElementLogReader mvReader = groupReader.CreateLogReader())
 
-                         {
 
-                             ReadElement(mvReader, 0);
 
-                             groupReader.ReadToEnd(LogReadFlags.ThrowFailures);
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         // This is not static method only to make it visible to COM
 
-         public string EscapeFileMask(string fileMask)
 
-         {
 
-             if (fileMask == null)
 
-             {
 
-                 throw new ArgumentNullException("fileMask");
 
-             }
 
-             int lastSlash = fileMask.LastIndexOf('/');
 
-             string path = lastSlash > 0 ? fileMask.Substring(0, lastSlash + 1) : string.Empty;
 
-             string mask = lastSlash > 0 ? fileMask.Substring(lastSlash + 1) : fileMask;
 
-             // Keep in sync with EscapeFileMask in GenerateUrl.cpp
 
-             mask = mask.Replace("[", "[[]").Replace("*", "[*]").Replace("?", "[?]");
 
-             return path + mask;
 
-         }
 
-         public string TranslateRemotePathToLocal(string remotePath, string remoteRoot, string localRoot)
 
-         {
 
-             if (remotePath == null)
 
-             {
 
-                 throw new ArgumentNullException("remotePath");
 
-             }
 
-             if (remoteRoot == null)
 
-             {
 
-                 throw new ArgumentNullException("remoteRoot");
 
-             }
 
-             if (localRoot == null)
 
-             {
 
-                 throw new ArgumentNullException("localRoot");
 
-             }
 
-             if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
 
-             {
 
-                 localRoot += "\\";
 
-             }
 
-             // not adding to empty root paths, because the path may not even start with slash
 
-             if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
 
-             {
 
-                 remoteRoot += "/";
 
-             }
 
-             string localPath;
 
-             // special case
 
-             if (remotePath == remoteRoot)
 
-             {
 
-                 localPath = localRoot;
 
-             }
 
-             else
 
-             {
 
-                 if (!remotePath.StartsWith(remoteRoot, StringComparison.Ordinal))
 
-                 {
 
-                     throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", remotePath, remoteRoot));
 
-                 }
 
-                 string subPath = remotePath.Substring(remoteRoot.Length);
 
-                 // can happen only when remoteRoot is empty
 
-                 if (subPath.StartsWith("/", StringComparison.Ordinal))
 
-                 {
 
-                     subPath = subPath.Substring(1);
 
-                 }
 
-                 subPath = subPath.Replace('/', '\\');
 
-                 localPath = localRoot + subPath;
 
-             }
 
-             return localPath;
 
-         }
 
-         public string TranslateLocalPathToRemote(string localPath, string localRoot, string remoteRoot)
 
-         {
 
-             if (localPath == null)
 
-             {
 
-                 throw new ArgumentNullException("localPath");
 
-             }
 
-             if (localRoot == null)
 
-             {
 
-                 throw new ArgumentNullException("localRoot");
 
-             }
 
-             if (remoteRoot == null)
 
-             {
 
-                 throw new ArgumentNullException("remoteRoot");
 
-             }
 
-             if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
 
-             {
 
-                 localRoot += "\\";
 
-             }
 
-             // not adding to empty root paths, because the path may not even start with slash
 
-             if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
 
-             {
 
-                 remoteRoot += "/";
 
-             }
 
-             string remotePath;
 
-             // special case
 
-             if (localPath == localRoot)
 
-             {
 
-                 remotePath = remoteRoot;
 
-             }
 
-             else
 
-             {
 
-                 if (!localPath.StartsWith(localRoot, StringComparison.Ordinal))
 
-                 {
 
-                     throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", localPath, localRoot));
 
-                 }
 
-                 string subPath = localPath.Substring(localRoot.Length);
 
-                 // can happen only when localRoot is empty
 
-                 if (subPath.StartsWith("\\", StringComparison.Ordinal))
 
-                 {
 
-                     subPath = subPath.Substring(1);
 
-                 }
 
-                 subPath = subPath.Replace('\\', '/');
 
-                 remotePath = remoteRoot + subPath;
 
-             }
 
-             return remotePath;
 
-         }
 
-         public string CombinePaths(string path1, string path2)
 
-         {
 
-             if (path1 == null)
 
-             {
 
-                 throw new ArgumentNullException("path1");
 
-             }
 
-             if (path2 == null)
 
-             {
 
-                 throw new ArgumentNullException("path2");
 
-             }
 
-             string result;
 
-             if (path2.StartsWith("/", StringComparison.Ordinal))
 
-             {
 
-                 result = path2;
 
-             }
 
-             else
 
-             {
 
-                 result =
 
-                     path1 +
 
-                     ((path1.Length == 0) || (path2.Length == 0) || path1.EndsWith("/", StringComparison.Ordinal) ? string.Empty : "/") +
 
-                     path2;
 
-             }
 
-             return result;
 
-         }
 
-         public void AddRawConfiguration(string setting, string value)
 
-         {
 
-             CheckNotOpened();
 
-             RawConfiguration.Add(setting, value);
 
-         }
 
-         [ComRegisterFunction]
 
-         private static void ComRegister(Type t)
 
-         {
 
-             string subKey = GetTypeLibKey(t);
 
-             Assembly assembly = Assembly.GetAssembly(t);
 
-             object[] attributes = assembly.GetCustomAttributes(typeof(GuidAttribute), false);
 
-             if (attributes.Length == 0)
 
-             {
 
-                 throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Cannot find {0} attribute for assembly {1}", typeof(GuidAttribute), assembly));
 
-             }
 
-             GuidAttribute guidAttribute = (GuidAttribute)attributes[0];
 
-             Registry.ClassesRoot.CreateSubKey(subKey).SetValue(null, guidAttribute.Value);
 
-         }
 
-         [ComUnregisterFunction]
 
-         private static void ComUnregister(Type t)
 
-         {
 
-             string subKey = GetTypeLibKey(t);
 
-             Registry.ClassesRoot.DeleteSubKey(subKey, false);
 
-         }
 
-         private void ReadFile(RemoteFileInfo fileInfo, CustomLogReader fileReader)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 string value;
 
-                 if (fileReader.GetEmptyElementValue("type", out value))
 
-                 {
 
-                     fileInfo.FileType = value[0];
 
-                 }
 
-                 else if (fileReader.GetEmptyElementValue("size", out value))
 
-                 {
 
-                     fileInfo.Length = long.Parse(value, CultureInfo.InvariantCulture);
 
-                 }
 
-                 else if (fileReader.GetEmptyElementValue("modification", out value))
 
-                 {
 
-                     fileInfo.LastWriteTime = XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Local);
 
-                 }
 
-                 else if (fileReader.GetEmptyElementValue("permissions", out value))
 
-                 {
 
-                     fileInfo.FilePermissions = FilePermissions.CreateReadOnlyFromText(value);
 
-                 }
 
-                 else if (fileReader.GetEmptyElementValue("owner", out value))
 
-                 {
 
-                     fileInfo.Owner = value;
 
-                 }
 
-                 else if (fileReader.GetEmptyElementValue("group", out value))
 
-                 {
 
-                     fileInfo.Group = value;
 
-                 }
 
-             }
 
-         }
 
-         internal static string BooleanSwitch(bool flag, string name)
 
-         {
 
-             return flag ? string.Format(CultureInfo.InvariantCulture, "-{0}", name) : null;
 
-         }
 
-         internal static string BooleanSwitch(bool flag, string onName, string offName)
 
-         {
 
-             return flag ? string.Format(CultureInfo.InvariantCulture, "-{0}", onName) : string.Format(CultureInfo.InvariantCulture, "-{0}", offName);
 
-         }
 
-         private void AddSynchronizationTransfer(SynchronizationResult result, TransferEventArgs transfer)
 
-         {
 
-             if (transfer != null)
 
-             {
 
-                 if (transfer.Side == ProgressSide.Local)
 
-                 {
 
-                     result.AddUpload(transfer);
 
-                 }
 
-                 else
 
-                 {
 
-                     result.AddDownload(transfer);
 
-                 }
 
-                 RaiseFileTransferredEvent(transfer);
 
-             }
 
-         }
 
-         private static string IncludeTrailingSlash(string path)
 
-         {
 
-             if (!string.IsNullOrEmpty(path) && !path.EndsWith("/", StringComparison.Ordinal))
 
-             {
 
-                 path += '/';
 
-             }
 
-             return path;
 
-         }
 
-         private void Cleanup()
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 if (_process != null)
 
-                 {
 
-                     Logger.WriteLine("Terminating process");
 
-                     try
 
-                     {
 
-                         try
 
-                         {
 
-                             WriteCommand("exit");
 
-                             _process.Close();
 
-                         }
 
-                         finally
 
-                         {
 
-                             _process.Dispose();
 
-                             _process = null;
 
-                         }
 
-                     }
 
-                     catch (Exception e)
 
-                     {
 
-                         Logger.WriteLine("Process cleanup Exception: {0}", e);
 
-                     }
 
-                 }
 
-                 Logger.WriteLine("Disposing log readers");
 
-                 if (_reader != null)
 
-                 {
 
-                     _reader.Dispose();
 
-                     _reader = null;
 
-                 }
 
-                 if (_logReader != null)
 
-                 {
 
-                     _logReader.Dispose();
 
-                     _logReader = null;
 
-                 }
 
-                 // Cleanup log file
 
-                 if ((XmlLogPath != null) && File.Exists(XmlLogPath))
 
-                 {
 
-                     Logger.WriteLine("Deleting XML log file [{0}]", XmlLogPath);
 
-                     try
 
-                     {
 
-                         File.Delete(XmlLogPath);
 
-                     }
 
-                     catch (DirectoryNotFoundException e)
 
-                     {
 
-                         Logger.WriteLine("XML log cleanup DirectoryNotFoundException: {0}", e);
 
-                     }
 
-                     catch (IOException e)
 
-                     {
 
-                         Logger.WriteLine("XML log cleanup IOException: {0}", e);
 
-                     }
 
-                     catch (UnauthorizedAccessException e)
 
-                     {
 
-                         Logger.WriteLine("XML log cleanup UnauthorizedAccessException: {0}", e);
 
-                     }
 
-                     _xmlLogPath = null;
 
-                 }
 
-             }
 
-         }
 
-         private void WriteCommand(string command)
 
-         {
 
-             WriteCommand(command, command);
 
-         }
 
-         private void WriteCommand(string command, string log)
 
-         {
 
-             Logger.WriteLine("Command: [{0}]", log);
 
-             _process.ExecuteCommand(command);
 
-             GotOutput();
 
-         }
 
-         private static void ReadElement(CustomLogReader reader, LogReadFlags flags)
 
-         {
 
-             while (reader.Read(flags))
 
-             {
 
-             }
 
-         }
 
-         private void SessionOptionsToUrlAndSwitches(SessionOptions sessionOptions, bool scanFingerprint, out string command, out string log)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 if (sessionOptions.WebdavSecure)
 
-                 {
 
-                     if (sessionOptions.Protocol != Protocol.Webdav)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.WebdavSecure is set, but SessionOptions.Protocol is not Protocol.Webdav.");
 
-                     }
 
-                 }
 
-                 string head;
 
-                 switch (sessionOptions.Protocol)
 
-                 {
 
-                     case Protocol.Sftp:
 
-                         head = "sftp://";
 
-                         break;
 
-                     case Protocol.Scp:
 
-                         head = "scp://";
 
-                         break;
 
-                     case Protocol.Ftp:
 
-                         head = "ftp://";
 
-                         break;
 
-                     case Protocol.Webdav:
 
-                         if (!sessionOptions.WebdavSecure)
 
-                         {
 
-                             head = "http://";
 
-                         }
 
-                         else
 
-                         {
 
-                             head = "https://";
 
-                         }
 
-                         break;
 
-                     default:
 
-                         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "{0} is not supported", sessionOptions.Protocol));
 
-                 }
 
-                 bool hasUsername;
 
-                 if (!scanFingerprint)
 
-                 {
 
-                     hasUsername = !string.IsNullOrEmpty(sessionOptions.UserName);
 
-                     if (hasUsername)
 
-                     {
 
-                         head += UriEscape(sessionOptions.UserName);
 
-                     }
 
-                 }
 
-                 else
 
-                 {
 
-                     hasUsername = false;
 
-                 }
 
-                 string url = head;
 
-                 string logUrl = head;
 
-                 if ((sessionOptions.SecurePassword != null) && !scanFingerprint)
 
-                 {
 
-                     if (!hasUsername)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.Password is set, but SessionOptions.UserName is not.");
 
-                     }
 
-                     url += ":" + UriEscape(sessionOptions.Password);
 
-                     logUrl += ":***";
 
-                 }
 
-                 string tail = string.Empty;
 
-                 if (hasUsername)
 
-                 {
 
-                     tail += "@";
 
-                 }
 
-                 if (string.IsNullOrEmpty(sessionOptions.HostName))
 
-                 {
 
-                     throw new ArgumentException("SessionOptions.HostName is not set.");
 
-                 }
 
-                 // We should wrap IPv6 literals to square brackets, instead of URL-encoding them,
 
-                 // but it works anyway...
 
-                 tail += UriEscape(sessionOptions.HostName);
 
-                 if (sessionOptions.PortNumber != 0)
 
-                 {
 
-                     tail += ":" + sessionOptions.PortNumber.ToString(CultureInfo.InvariantCulture);
 
-                 }
 
-                 if (!string.IsNullOrEmpty(sessionOptions.WebdavRoot) && !scanFingerprint)
 
-                 {
 
-                     if (sessionOptions.Protocol != Protocol.Webdav)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.WebdavRoot is set, but SessionOptions.Protocol is not Protocol.Webdav.");
 
-                     }
 
-                     tail += sessionOptions.WebdavRoot;
 
-                 }
 
-                 url += tail;
 
-                 logUrl += tail;
 
-                 string arguments = SessionOptionsToSwitches(sessionOptions, scanFingerprint);
 
-                 Tools.AddRawParameters(ref arguments, sessionOptions.RawSettings, "-rawsettings");
 
-                 if (!string.IsNullOrEmpty(arguments))
 
-                 {
 
-                     arguments = " " + arguments;
 
-                 }
 
-                 // Switches should (and particularly the -rawsettings MUST) come after the URL
 
-                 command = "\"" + Tools.ArgumentEscape(url) + "\"" + arguments;
 
-                 log = "\"" + Tools.ArgumentEscape(logUrl) + "\"" + arguments;
 
-             }
 
-         }
 
-         private string SessionOptionsToSwitches(SessionOptions sessionOptions, bool scanFingerprint)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 List<string> switches = new List<string>();
 
-                 if (!string.IsNullOrEmpty(sessionOptions.SshHostKeyFingerprint) ||
 
-                     (sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey && !scanFingerprint))
 
-                 {
 
-                     if (!sessionOptions.IsSsh)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.SshHostKeyFingerprint or SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey is set, but SessionOptions.Protocol is neither Protocol.Sftp nor Protocol.Scp.");
 
-                     }
 
-                     string sshHostKeyFingerprint = sessionOptions.SshHostKeyFingerprint;
 
-                     if (sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey)
 
-                     {
 
-                         Logger.WriteLine("WARNING! Giving up security and accepting any key as configured");
 
-                         sshHostKeyFingerprint = AddStarToList(sshHostKeyFingerprint);
 
-                     }
 
-                     switches.Add(FormatSwitch("hostkey", sshHostKeyFingerprint));
 
-                 }
 
-                 else
 
-                 {
 
-                     if (sessionOptions.IsSsh && DefaultConfigurationInternal && !scanFingerprint)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOptions.SshHostKeyFingerprint is not set.");
 
-                     }
 
-                 }
 
-                 if (!string.IsNullOrEmpty(sessionOptions.SshPrivateKeyPath) && !scanFingerprint)
 
-                 {
 
-                     if (!sessionOptions.IsSsh)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.SshPrivateKeyPath is set, but SessionOptions.Protocol is neither Protocol.Sftp nor Protocol.Scp.");
 
-                     }
 
-                     switches.Add(FormatSwitch("privatekey", sessionOptions.SshPrivateKeyPath));
 
-                 }
 
-                 if (!string.IsNullOrEmpty(sessionOptions.TlsClientCertificatePath) && !scanFingerprint)
 
-                 {
 
-                     if (!sessionOptions.IsTls)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.TlsClientCertificatePath is set, but neither SessionOptions.FtpSecure nor SessionOptions.WebdavSecure is enabled.");
 
-                     }
 
-                     switches.Add(FormatSwitch("clientcert", sessionOptions.TlsClientCertificatePath));
 
-                 }
 
-                 if (!string.IsNullOrEmpty(sessionOptions.PrivateKeyPassphrase) && !scanFingerprint)
 
-                 {
 
-                     if (string.IsNullOrEmpty(sessionOptions.SshPrivateKeyPath) && string.IsNullOrEmpty(sessionOptions.TlsClientCertificatePath))
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.PrivateKeyPassphrase is set, but neither SessionOptions.SshPrivateKeyPath nor SessionOptions.TlsClientCertificatePath is set.");
 
-                     }
 
-                     switches.Add(FormatSwitch("passphrase", sessionOptions.PrivateKeyPassphrase));
 
-                 }
 
-                 if (sessionOptions.FtpSecure != FtpSecure.None)
 
-                 {
 
-                     if (sessionOptions.Protocol != Protocol.Ftp)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.FtpSecure is not FtpSecure.None, but SessionOptions.Protocol is not Protocol.Ftp.");
 
-                     }
 
-                     switch (sessionOptions.FtpSecure)
 
-                     {
 
-                         case FtpSecure.Implicit:
 
-                             switches.Add(FormatSwitch("implicit"));
 
-                             break;
 
-                         case FtpSecure.Explicit:
 
-                             switches.Add(FormatSwitch("explicit"));
 
-                             break;
 
-                         default:
 
-                             throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "{0} is not supported", sessionOptions.FtpSecure));
 
-                     }
 
-                 }
 
-                 if ((!string.IsNullOrEmpty(sessionOptions.TlsHostCertificateFingerprint) ||
 
-                      sessionOptions.GiveUpSecurityAndAcceptAnyTlsHostCertificate) &&
 
-                     !scanFingerprint)
 
-                 {
 
-                     if (!sessionOptions.IsTls)
 
-                     {
 
-                         throw new ArgumentException("SessionOptions.TlsHostCertificateFingerprint or SessionOptions.GiveUpSecurityAndAcceptAnyTlsHostCertificate is set, but neither SessionOptions.FtpSecure nor SessionOptions.WebdavSecure is enabled.");
 
-                     }
 
-                     string tlsHostCertificateFingerprint = sessionOptions.TlsHostCertificateFingerprint;
 
-                     if (sessionOptions.GiveUpSecurityAndAcceptAnyTlsHostCertificate)
 
-                     {
 
-                         Logger.WriteLine("WARNING! Giving up security and accepting any certificate as configured");
 
-                         tlsHostCertificateFingerprint = AddStarToList(tlsHostCertificateFingerprint);
 
-                     }
 
-                     switches.Add(FormatSwitch("certificate", tlsHostCertificateFingerprint));
 
-                 }
 
-                 if ((sessionOptions.Protocol == Protocol.Ftp) && !scanFingerprint)
 
-                 {
 
-                     switches.Add(FormatSwitch("passive", (sessionOptions.FtpMode == FtpMode.Passive)));
 
-                 }
 
-                 switches.Add(FormatSwitch("timeout", (int)sessionOptions.Timeout.TotalSeconds));
 
-                 return string.Join(" ", switches.ToArray());
 
-             }
 
-         }
 
-         private static string AddStarToList(string list)
 
-         {
 
-             return (string.IsNullOrEmpty(list) ? string.Empty : list + ";") + "*";
 
-         }
 
-         private RemoteFileInfo DoGetFileInfo(string path)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 WriteCommand(string.Format(CultureInfo.InvariantCulture, "stat -- \"{0}\"", Tools.ArgumentEscape(path)));
 
-                 RemoteFileInfo fileInfo = new RemoteFileInfo();
 
-                 using (ElementLogReader groupReader = _reader.WaitForGroupAndCreateLogReader())
 
-                 using (ElementLogReader statReader = groupReader.WaitForNonEmptyElementAndCreateLogReader("stat", LogReadFlags.ThrowFailures))
 
-                 {
 
-                     while (statReader.Read(0))
 
-                     {
 
-                         string value;
 
-                         if (statReader.GetEmptyElementValue("filename", out value))
 
-                         {
 
-                             string name = value;
 
-                             int p = name.LastIndexOf('/');
 
-                             if (p >= 0)
 
-                             {
 
-                                 name = name.Substring(p + 1);
 
-                             }
 
-                             fileInfo.Name = name;
 
-                             fileInfo.FullName = value;
 
-                         }
 
-                         else if (statReader.IsNonEmptyElement("file"))
 
-                         {
 
-                             using (ElementLogReader fileReader = statReader.CreateLogReader())
 
-                             {
 
-                                 while (fileReader.Read(0))
 
-                                 {
 
-                                     ReadFile(fileInfo, fileReader);
 
-                                 }
 
-                             }
 
-                         }
 
-                     }
 
-                     groupReader.ReadToEnd(LogReadFlags.ThrowFailures);
 
-                 }
 
-                 return fileInfo;
 
-             }
 
-         }
 
-         internal static string FormatSwitch(string key)
 
-         {
 
-             return string.Format(CultureInfo.InvariantCulture, "-{0}", key);
 
-         }
 
-         internal static string FormatSwitch(string key, string value)
 
-         {
 
-             return string.Format(CultureInfo.InvariantCulture, "-{0}=\"{1}\"", key, Tools.ArgumentEscape(value));
 
-         }
 
-         internal static string FormatSwitch(string key, int value)
 
-         {
 
-             return string.Format(CultureInfo.InvariantCulture, "-{0}={1}", key, value.ToString(CultureInfo.InvariantCulture));
 
-         }
 
-         internal static string FormatSwitch(string key, bool value)
 
-         {
 
-             return FormatSwitch(key, (value ? 1 : 0));
 
-         }
 
-         private static string UriEscape(string s)
 
-         {
 
-             return Uri.EscapeDataString(s);
 
-         }
 
-         internal void GotOutput()
 
-         {
 
-             _lastOutput = DateTime.Now;
 
-         }
 
-         private void ProcessOutputDataReceived(object sender, OutputDataReceivedEventArgs e)
 
-         {
 
-             if (e == null)
 
-             {
 
-                 Logger.WriteLine("Got incomplete progress output");
 
-             }
 
-             else
 
-             {
 
-                 Logger.WriteLine("Scheduling output: [{0}]", e.Data);
 
-                 string s = e.Data.TrimEnd(new[] { '\r' });
 
-                 lock (Output)
 
-                 {
 
-                     Output.InternalAdd(s);
 
-                     if (Output.Count > 1000)
 
-                     {
 
-                         Output.InternalRemoveFirst();
 
-                     }
 
-                     if (e.Error)
 
-                     {
 
-                         _error.InternalAdd(s);
 
-                         if (_error.Count > 1000)
 
-                         {
 
-                             _error.InternalRemoveFirst();
 
-                         }
 
-                     }
 
-                 }
 
-                 ScheduleEvent(() => RaiseOutputDataReceived(e.Data, e.Error));
 
-             }
 
-             GotOutput();
 
-         }
 
-         private void ScheduleEvent(Action action)
 
-         {
 
-             lock (_events)
 
-             {
 
-                 _events.Add(action);
 
-                 _eventsEvent.Set();
 
-             }
 
-         }
 
-         internal void CheckForTimeout(string additional = null)
 
-         {
 
-             if (DateTime.Now - _lastOutput > Timeout)
 
-             {
 
-                 string message = "Timeout waiting for WinSCP to respond";
 
-                 if (additional != null)
 
-                 {
 
-                     message += " - " + additional;
 
-                 }
 
-                 throw new TimeoutException(message);
 
-             }
 
-             if (_aborted)
 
-             {
 
-                 throw new SessionLocalException(this, "Aborted.");
 
-             }
 
-         }
 
-         private void RaiseFileTransferredEvent(TransferEventArgs args)
 
-         {
 
-             Logger.WriteLine("FileTransferredEvent: [{0}]", args.FileName);
 
-             if (FileTransferred != null)
 
-             {
 
-                 FileTransferred(this, args);
 
-             }
 
-         }
 
-         internal void RaiseFailed(SessionRemoteException e)
 
-         {
 
-             Logger.WriteLine("Failed: [{0}]", e);
 
-             if (Failed != null)
 
-             {
 
-                 Failed(this, new FailedEventArgs { Error = e });
 
-             }
 
-             foreach (OperationResultBase operationResult in _operationResults)
 
-             {
 
-                 operationResult.AddFailure(e);
 
-             }
 
-         }
 
-         private void CheckNotDisposed()
 
-         {
 
-             if (_disposed)
 
-             {
 
-                 throw new InvalidOperationException("Object is disposed");
 
-             }
 
-             if (_aborted)
 
-             {
 
-                 throw new InvalidOperationException("Session was aborted");
 
-             }
 
-         }
 
-         private void CheckOpened()
 
-         {
 
-             if (!Opened)
 
-             {
 
-                 throw new InvalidOperationException("Session is not opened");
 
-             }
 
-         }
 
-         private void CheckNotOpened()
 
-         {
 
-             if (Opened)
 
-             {
 
-                 throw new InvalidOperationException("Session is already opened");
 
-             }
 
-         }
 
-         private void RaiseOutputDataReceived(string data, bool error)
 
-         {
 
-             Logger.WriteLine("Output: [{0}]", data);
 
-             if (OutputDataReceived != null)
 
-             {
 
-                 OutputDataReceived(this, new OutputDataReceivedEventArgs(data, error));
 
-             }
 
-         }
 
-         internal void DispatchEvents(int interval)
 
-         {
 
-             DateTime start = DateTime.Now;
 
-             while (_eventsEvent.WaitOne(interval, false))
 
-             {
 
-                 lock (_events)
 
-                 {
 
-                     foreach (Action action in _events)
 
-                     {
 
-                         action();
 
-                     }
 
-                     _events.Clear();
 
-                 }
 
-                 interval -= (int) (DateTime.Now - start).TotalMilliseconds;
 
-                 if (interval < 0)
 
-                 {
 
-                     break;
 
-                 }
 
-                 start = DateTime.Now;
 
-             }
 
-         }
 
-         private IDisposable RegisterOperationResult(OperationResultBase operationResult)
 
-         {
 
-             _operationResults.Add(operationResult);
 
-             return new OperationResultGuard(this, operationResult);
 
-         }
 
-         internal void UnregisterOperationResult(OperationResultBase operationResult)
 
-         {
 
-             _operationResults.Remove(operationResult);
 
-         }
 
-         internal bool WantsProgress
 
-         {
 
-             get
 
-             {
 
-                 return (_fileTransferProgress != null);
 
-             }
 
-         }
 
-         private IDisposable CreateProgressHandler()
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 _progressHandling++;
 
-                 return new ProgressHandler(this);
 
-             }
 
-         }
 
-         internal void DisableProgressHandling()
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 if (_progressHandling <= 0)
 
-                 {
 
-                     throw new InvalidOperationException("Progress handling not enabled");
 
-                 }
 
-                 // make sure we process all pending progress events
 
-                 DispatchEvents(0);
 
-                 _progressHandling--;
 
-             }
 
-         }
 
-         internal void ProcessProgress(FileTransferProgressEventArgs args)
 
-         {
 
-             ScheduleEvent(() => Progress(args));
 
-         }
 
-         private void Progress(FileTransferProgressEventArgs args)
 
-         {
 
-             if ((_progressHandling >= 0) && WantsProgress)
 
-             {
 
-                 _fileTransferProgress(this, args);
 
-             }
 
-         }
 
-         private void SetupTempPath()
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 if (!string.IsNullOrEmpty(_xmlLogPath))
 
-                 {
 
-                     bool exists = File.Exists(_xmlLogPath);
 
-                     Logger.WriteLine("Configured temporary file: {0} - Exists [{1}]", _xmlLogPath, exists);
 
-                     if (exists)
 
-                     {
 
-                         throw new SessionLocalException(this, string.Format(CultureInfo.CurrentCulture, "Configured temporary file {0} already exists", _xmlLogPath));
 
-                     }
 
-                 }
 
-                 else
 
-                 {
 
-                     string path = Path.GetTempPath();
 
-                     Logger.WriteLine("Temporary folder: {0}", path);
 
-                     string process = Process.GetCurrentProcess().Id.ToString("X4", CultureInfo.InvariantCulture);
 
-                     string instance = GetHashCode().ToString("X8", CultureInfo.InvariantCulture);
 
-                     string filename;
 
-                     bool exists;
 
-                     do
 
-                     {
 
-                         string uniqueStr = (_logUnique > 0 ? "." + _logUnique.ToString(CultureInfo.InvariantCulture) : string.Empty);
 
-                         ++_logUnique;
 
-                         filename = Path.Combine(path, "wscp" + process + "." + instance + uniqueStr + ".tmp");
 
-                         exists = File.Exists(filename);
 
-                         Logger.WriteLine("Temporary file [{0}] - Exists [{1}]", filename, exists);
 
-                     }
 
-                     while (exists);
 
-                     _xmlLogPath = filename;
 
-                 }
 
-             }
 
-         }
 
-         private bool GetDisableVersionCheck()
 
-         {
 
-             bool result = _disableVersionCheck;
 
-             #if DEBUG
 
-             string env = Environment.GetEnvironmentVariable("WINSCPNET_DISABLE_VERSION_CHECK");
 
-             int envFlag;
 
-             if (!string.IsNullOrEmpty(env) && int.TryParse(env, out envFlag))
 
-             {
 
-                 result = (envFlag != 0);
 
-             }
 
-             #endif
 
-             return result;
 
-         }
 
-         private static string GetTypeLibKey(Type t)
 
-         {
 
-             return "CLSID\\{" + t.GUID.ToString().ToUpperInvariant() + "}\\TypeLib";
 
-         }
 
-         FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 Logger.WriteLine("Name [{0}]", name);
 
-                 FieldInfo result = GetType().GetField(name, bindingAttr);
 
-                 Logger.WriteLine("Result [{0}]", result != null ? result.Name : "null");
 
-                 return result;
 
-             }
 
-         }
 
-         FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 FieldInfo[] result = GetType().GetFields(bindingAttr);
 
-                 Logger.WriteLine("Fields [{0}]", result.Length);
 
-                 return result;
 
-             }
 
-         }
 
-         MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 Logger.WriteLine("Name [{0}]", name);
 
-                 MemberInfo[] result = GetType().GetMember(name, bindingAttr);
 
-                 Logger.WriteLine("Result [{0}]", result.Length);
 
-                 return result;
 
-             }
 
-         }
 
-         MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 MemberInfo[] result = GetType().GetMembers(bindingAttr);
 
-                 Logger.WriteLine("Result [{0}]", result.Length);
 
-                 return result;
 
-             }
 
-         }
 
-         MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 Logger.WriteLine("Name [{0}]", name);
 
-                 MethodInfo result = GetType().GetMethod(name, bindingAttr);
 
-                 Logger.WriteLine("Result [{0}]", result != null ? result.Name : "null");
 
-                 return result;
 
-             }
 
-         }
 
-         MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 Logger.WriteLine("Name [{0}]", name);
 
-                 MethodInfo result = GetType().GetMethod(name, bindingAttr, binder, types, modifiers);
 
-                 Logger.WriteLine("Result [{0}]", result != null ? result.Name : "null");
 
-                 return result;
 
-             }
 
-         }
 
-         MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 MethodInfo[] result = GetType().GetMethods(bindingAttr);
 
-                 Logger.WriteLine("Result [{0}]", result.Length);
 
-                 return result;
 
-             }
 
-         }
 
-         PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 PropertyInfo[] result = GetType().GetProperties(bindingAttr);
 
-                 Logger.WriteLine("Result [{0}]", result.Length);
 
-                 return result;
 
-             }
 
-         }
 
-         PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 Logger.WriteLine("Name [{0}]", name);
 
-                 PropertyInfo result = GetType().GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
 
-                 Logger.WriteLine("Result [{0}]", result != null ? result.Name : "null");
 
-                 return result;
 
-             }
 
-         }
 
-         PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 Logger.WriteLine("Name [{0}]", name);
 
-                 PropertyInfo result = GetType().GetProperty(name, bindingAttr);
 
-                 Logger.WriteLine("Result [{0}]", result != null ? result.Name : "null");
 
-                 return result;
 
-             }
 
-         }
 
-         object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
 
-         {
 
-             using (Logger.CreateCallstack())
 
-             {
 
-                 object result;
 
-                 try
 
-                 {
 
-                     if (Logger.Logging)
 
-                     {
 
-                         Logger.WriteLine("Name [{0}]", name);
 
-                         Logger.WriteLine("BindingFlags [{0}]", invokeAttr);
 
-                         Logger.WriteLine("Binder [{0}]", binder);
 
-                         Logger.WriteLine("Target [{0}]", target);
 
-                         if (args != null)
 
-                         {
 
-                             Logger.WriteLine("Args [{0}] [{1}]", args.Length, modifiers != null ? modifiers.Length.ToString(CultureInfo.InvariantCulture) : "null");
 
-                             for (int i = 0; i < args.Length; ++i)
 
-                             {
 
-                                 Logger.WriteLine("Arg [{0}] [{1}] [{1}] [{2}]", i, args[i], (args[i] != null ? args[i].GetType().ToString() : "null"), (modifiers != null ? modifiers[i].ToString() : "null"));
 
-                             }
 
-                         }
 
-                         Logger.WriteLine("Culture [{0}]", culture);
 
-                         if (namedParameters != null)
 
-                         {
 
-                             foreach (string namedParameter in namedParameters)
 
-                             {
 
-                                 Logger.WriteLine("NamedParameter [{0}]", namedParameter);
 
-                             }
 
-                         }
 
-                     }
 
-                     if (target == null)
 
-                     {
 
-                         throw new ArgumentNullException("target");
 
-                     }
 
-                     Type type = target.GetType();
 
-                     // RuntimeType.InvokeMember below calls into Binder.BindToMethod (Binder is OleAutBinder)
 
-                     // that fails to match method, if integer value is provided to enum argument
 
-                     // (SynchronizeDirectories with its SynchronizationMode and SynchronizationCriteria).
 
-                     // This does not happen if we do not implement IReflect, though no idea why.
 
-                     // So as a workaround we check, if the method has no overloads (what is always true for Session),
 
-                     // and call the only instance directly.
 
-                     // Calling MethodInfo.Invoke with int values for enum arguments works.
 
-                     // Only as a fallback, we call InvokeMember (what is currently actually used only when
 
-                     // the method with given name does not exist at all)
 
-                     MethodInfo method = null;
 
-                     PropertyInfo property = null;
 
-                     // would be way too difficult to implement the below involving named arguments
 
-                     if (namedParameters == null)
 
-                     {
 
-                         try
 
-                         {
 
-                             BindingFlags bindingFlags = invokeAttr | BindingFlags.Instance | BindingFlags.Public;
 
-                             method = type.GetMethod(name, bindingFlags);
 
-                             if (args == null)
 
-                             {
 
-                                 throw new ArgumentNullException("args");
 
-                             }
 
-                             if (method != null)
 
-                             {
 
-                                 // MethodInfo.Invoke does not fill-in optional arguments (contrary to RuntimeType.InvokeMember)
 
-                                 ParameterInfo[] parameters = method.GetParameters();
 
-                                 if (args.Length < parameters.Length)
 
-                                 {
 
-                                     Logger.WriteLine("Provided less parameters [{0}] than defined [{1}]", args.Length, parameters.Length);
 
-                                     object[] args2 = new object[parameters.Length];
 
-                                     for (int i = 0; i < parameters.Length; i++)
 
-                                     {
 
-                                         if (i < args.Length)
 
-                                         {
 
-                                             args2[i] = args[i];
 
-                                         }
 
-                                         else
 
-                                         {
 
-                                             if (!parameters[i].IsOptional)
 
-                                             {
 
-                                                 Logger.WriteLine("Parameter [{0}] of [{1}] is not optional", i, method);
 
-                                                 args2 = null;
 
-                                                 break;
 
-                                             }
 
-                                             else
 
-                                             {
 
-                                                 Logger.WriteLine("Adding default value [{0}] for optional parameter [{1}]", parameters[i].DefaultValue, i);
 
-                                                 args2[i] = parameters[i].DefaultValue;
 
-                                             }
 
-                                         }
 
-                                     }
 
-                                     if (args2 != null)
 
-                                     {
 
-                                         args = args2;
 
-                                     }
 
-                                 }
 
-                             }
 
-                             else if (args.Length == 1) // sanity check
 
-                             {
 
-                                 property = type.GetProperty(name, bindingFlags);
 
-                             }
 
-                         }
 
-                         catch (AmbiguousMatchException e)
 
-                         {
 
-                             Logger.WriteLine("Unexpected ambiguous match [{0}]", e.Message);
 
-                         }
 
-                     }
 
-                     if (method != null)
 
-                     {
 
-                         Logger.WriteLine("Invoking unambiguous method [{0}]", method);
 
-                         result = method.Invoke(target, invokeAttr, binder, args, culture);
 
-                     }
 
-                     else if (property != null)
 
-                     {
 
-                         Logger.WriteLine("Setting unambiguous property [{0}]", property);
 
-                         property.SetValue(target, args[0], invokeAttr, binder, null, culture);
 
-                         result = null;
 
-                     }
 
-                     else
 
-                     {
 
-                         Logger.WriteLine("Invoking ambiguous/non-existing method 2 [{0}]", name);
 
-                         result = type.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
 
-                     }
 
-                     Logger.WriteLine("Result [{0}] [{1}]", result, (result != null ? result.GetType().ToString() : "null"));
 
-                 }
 
-                 catch (Exception e)
 
-                 {
 
-                     Logger.WriteLine("Error [{0}]", e);
 
-                     throw;
 
-                 }
 
-                 return result;
 
-             }
 
-         }
 
-         Type IReflect.UnderlyingSystemType
 
-         {
 
-             get { return GetType(); }
 
-         }
 
-         internal const string Namespace = "http://winscp.net/schema/session/1.0";
 
-         internal Logger Logger { get; private set; }
 
-         internal bool GuardProcessWithJobInternal { get { return _guardProcessWithJob; } set { CheckNotOpened(); _guardProcessWithJob = value; } }
 
-         internal bool TestHandlesClosedInternal { get; set; }
 
-         internal Dictionary<string, string> RawConfiguration { get; private set; }
 
-         internal bool DefaultConfigurationInternal { get { return _defaultConfiguration; } }
 
-         internal string IniFilePathInternal { get { return _iniFilePath; } }
 
-         private ExeSessionProcess _process;
 
-         private DateTime _lastOutput;
 
-         private ElementLogReader _reader;
 
-         private SessionLogReader _logReader;
 
-         private readonly IList<OperationResultBase> _operationResults;
 
-         private delegate void Action();
 
-         private readonly IList<Action> _events;
 
-         private AutoResetEvent _eventsEvent;
 
-         private bool _disposed;
 
-         private string _executablePath;
 
-         private string _additionalExecutableArguments;
 
-         private bool _defaultConfiguration;
 
-         private bool _disableVersionCheck;
 
-         private string _iniFilePath;
 
-         private TimeSpan _reconnectTime;
 
-         private string _sessionLogPath;
 
-         private bool _aborted;
 
-         private int _logUnique;
 
-         private string _xmlLogPath;
 
-         private FileTransferProgressEventHandler _fileTransferProgress;
 
-         private int _progressHandling;
 
-         private bool _guardProcessWithJob;
 
-         private string _homePath;
 
-         private string _executableProcessUserName;
 
-         private SecureString _executableProcessPassword;
 
-         private StringCollection _error;
 
-     }
 
- }
 
 
  |