Beispiel #1
0
 /// <summary>
 /// Works like Visual Basic IsDBNull method.
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static bool IsDBNull(object value)
 {
     if (value != null)
     {
         return(VisualBasic.CBool(value == DBNull.Value));
     }
     else
     {
         return(false);
     }
 }
        private bool IsIniFile()
        {
            bool _isinifile = false;

            if (!String.IsNullOrEmpty(_filename.RLTrim()))
            {
                if (File.Exists(_filename))
                {
                    _isinifile = VisualBasic.CBool(Path.GetExtension(_filename).RLTrim().ToLower().Replace(".", "") == "ini");
                }
            }

            return(_isinifile);
        }
Beispiel #3
0
        /// <summary>
        /// Performs archive extraction using the chosen archiving tool.
        /// </summary>
        /// <param name="destination">Destination path for the extracted file(s)</param>
        /// <returns></returns>
        public bool Extract(string destination)
        {
            bool _extracted = false; string _error = "";
            //ExtractResourceApplications();
            string _archiverfile = "";

            switch (_archivingtool)
            {
            case ArchivingToolEnum.SevenZip:
                _archiverfile = Application.StartupPath + "\\7z.exe";

                if (File.Exists(_archiverfile))
                {
                    Process _process = new Process();
                    _process.StartInfo.Arguments             = "e \"" + _path + "\" -o\"" + destination + "\" *.* -r";
                    _process.StartInfo.FileName              = _archiverfile;
                    _process.StartInfo.CreateNoWindow        = VisualBasic.CBool(_processwindowstyle == ProcessWindowStyle.Hidden);
                    _process.StartInfo.WindowStyle           = _processwindowstyle;
                    _process.StartInfo.RedirectStandardError = true;
                    _process.StartInfo.UseShellExecute       = false;
                    _process.Start();

                    while (!_process.HasExited)
                    {
                        Application.DoEvents();
                    }

                    if (_process.StandardError != null)
                    {
                        try { _error = _process.StandardError.ReadToEnd(); }
                        catch { _error = ""; }
                    }
                    else
                    {
                        _error = "";
                    }
                    _process.Dispose(); Materia.RefreshAndManageCurrentProcess();
                }
                break;

            case ArchivingToolEnum.WinRar:
                _archiverfile = Application.StartupPath + "\\WinRar.exe";

                if (File.Exists(_archiverfile))
                {
                    Process _process = new Process();
                    _process.StartInfo.Arguments             = "e \"" + _path + "\" *.* \"" + destination + "\"";
                    _process.StartInfo.FileName              = _archiverfile;
                    _process.StartInfo.CreateNoWindow        = VisualBasic.CBool(_processwindowstyle == ProcessWindowStyle.Hidden);
                    _process.StartInfo.WindowStyle           = _processwindowstyle;
                    _process.StartInfo.RedirectStandardError = true;
                    _process.StartInfo.UseShellExecute       = false;
                    _process.Start();

                    while (!_process.HasExited)
                    {
                        Application.DoEvents();
                    }

                    if (_process.StandardError != null)
                    {
                        try { _error = _process.StandardError.ReadToEnd(); }
                        catch { _error = ""; }
                    }
                    else
                    {
                        _error = "";
                    }
                    _process.Dispose(); Materia.RefreshAndManageCurrentProcess();
                }
                break;

            default: break;
            }

            _extracted = String.IsNullOrEmpty(_error.Replace("\nThe handle is invalid.", "").Replace("The handle is invalid.", "").RLTrim());
            if (!_extracted)
            {
                Materia.LogError(_error.Replace("\nThe handle is invalid.", "").Replace("The handle is invalid.", "").RLTrim(), Enum.GetName(typeof(ArchivingToolEnum), _archivingtool) + " File Extraction Error");
            }
            //RemoveResourceApplications();

            return(_extracted);
        }
Beispiel #4
0
        /// <summary>
        /// Perform file / directory compression.
        /// </summary>
        /// <returns></returns>
        public FileInfo Archive()
        {
            FileInfo _file = null; bool _pathexists = false; _archivedpath = "";

            if (System.IO.Path.HasExtension(_path))
            {
                _pathexists = File.Exists(_path);
            }
            else
            {
                _pathexists = Directory.Exists(_path);
            }

            if (_pathexists)
            {
                //ExtractResourceApplications();

                switch (_archivingtool)
                {
                case ArchivingToolEnum.SevenZip:
                    if (File.Exists(Application.StartupPath + "\\7z.exe"))
                    {
                        string _compressfilename = System.IO.Path.GetFileNameWithoutExtension(_path) + ".7z";
                        string _compressdir      = System.IO.Path.GetDirectoryName(_path);
                        string _compressfile     = _compressdir + (_compressdir.RLTrim().EndsWith("\\") ? "" : "\\") + _compressfilename;

                        if (File.Exists(_compressfile))
                        {
                            try { File.Delete(_compressfile); }
                            catch { }
                        }

                        string   _batfilename     = Application.StartupPath + "\\archiver.bat";
                        string   _batfilecontents = "7z a \"" + _compressfile + "\" \"" + _path + "\"";
                        FileInfo _batfile         = Materia.WriteToFile(_batfilename, _batfilecontents);
                        string   _error           = "";

                        if (_batfile != null)
                        {
                            Process _process = new Process();
                            _process.StartInfo.FileName              = _batfilename;
                            _process.StartInfo.CreateNoWindow        = VisualBasic.CBool(_processwindowstyle == ProcessWindowStyle.Hidden);
                            _process.StartInfo.WindowStyle           = _processwindowstyle;
                            _process.StartInfo.UseShellExecute       = false;
                            _process.StartInfo.RedirectStandardError = true;
                            _process.Start();

                            while (!_process.HasExited)
                            {
                                Application.DoEvents();
                            }

                            if (_process.StandardError != null)
                            {
                                try { _error = _process.StandardError.ReadToEnd(); }
                                catch { _error = ""; }
                            }
                            else
                            {
                                _error = "";
                            }

                            _process.Dispose(); Materia.RefreshAndManageCurrentProcess();

                            try { _batfile.Delete(); }
                            catch { }
                        }
                        else
                        {
                            _error = "Can't create executable archive batch file.";
                        }

                        _error = _error.Replace("\nThe handle is invalid.", "").Replace("The handle is invalid.", "");
                        if (!String.IsNullOrEmpty(_error.RLTrim()))
                        {
                            Materia.LogError(_error, "7Zip Archiving Error");
                        }

                        if (File.Exists(_compressfile))
                        {
                            _archivedpath = _compressfile;

                            if (_archivingmethod == ArchivingMethodEnum.Insert)
                            {
                                try
                                {
                                    for (int i = 0; i <= 30; i++)
                                    {
                                        Thread.Sleep(10); Application.DoEvents();
                                    }

                                    if (System.IO.Path.HasExtension(_path))
                                    {
                                        File.Delete(_path);
                                    }
                                    else
                                    {
                                        Directory.Delete(_path, true);
                                    }
                                }
                                catch { }
                            }

                            //RemoveResourceApplications();
                            _file = new FileInfo(_archivedpath);
                        }
                    }
                    break;

                case ArchivingToolEnum.WinRar:
                    if (File.Exists(Application.StartupPath + "\\WinRar.exe"))
                    {
                        string _compressfilename = System.IO.Path.GetFileNameWithoutExtension(_path) + ".rar";
                        string _compressdir      = System.IO.Path.GetDirectoryName(_path);
                        string _compressfile     = _compressdir + (_compressdir.RLTrim().EndsWith("\\") ? "" : "\\") + _compressfilename;

                        if (File.Exists(_compressfile))
                        {
                            try { File.Delete(_compressfile); }
                            catch { }
                        }

                        string   _batfilename     = Application.StartupPath + "\\archiver.bat";
                        string   _batfilecontents = "WinRar a -ep \"" + _compressfile + "\" \"" + _path + "\"";
                        FileInfo _batfile         = Materia.WriteToFile(_batfilename, _batfilecontents);
                        string   _error           = "";

                        if (_batfile != null)
                        {
                            Process _process = new Process();
                            _process.StartInfo.FileName              = _batfilename;
                            _process.StartInfo.CreateNoWindow        = VisualBasic.CBool(_processwindowstyle == ProcessWindowStyle.Hidden);
                            _process.StartInfo.WindowStyle           = _processwindowstyle;
                            _process.StartInfo.UseShellExecute       = false;
                            _process.StartInfo.RedirectStandardError = true;
                            _process.Start();

                            while (!_process.HasExited)
                            {
                                Application.DoEvents();
                            }

                            if (_process.StandardError != null)
                            {
                                try { _error = _process.StandardError.ReadToEnd(); }
                                catch { _error = ""; }
                            }
                            else
                            {
                                _error = "";
                            }

                            _process.Dispose(); Materia.RefreshAndManageCurrentProcess();

                            try { _batfile.Delete(); }
                            catch { }
                        }
                        else
                        {
                            _error = "Can't create executable archive batch file.";
                        }

                        _error = _error.Replace("\nThe handle is invalid.", "").Replace("The handle is invalid.", "");
                        if (!String.IsNullOrEmpty(_error.RLTrim()))
                        {
                            Materia.LogError(_error, "WinRar Archiving Error");
                        }

                        if (File.Exists(_compressfile))
                        {
                            _archivedpath = _compressfile;

                            if (_archivingmethod == ArchivingMethodEnum.Insert)
                            {
                                try
                                {
                                    for (int i = 0; i <= 30; i++)
                                    {
                                        Thread.Sleep(10); Application.DoEvents();
                                    }

                                    if (System.IO.Path.HasExtension(_path))
                                    {
                                        File.Delete(_path);
                                    }
                                    else
                                    {
                                        Directory.Delete(_path, true);
                                    }
                                }
                                catch { }
                            }

                            //RemoveResourceApplications();
                            _file = new FileInfo(_archivedpath);
                        }
                    }
                    break;

                default: break;
                }
            }

            return(_file);
        }