コード例 #1
0
ファイル: WebUtils.cs プロジェクト: Robin--/RMLib
        static public bool FtpUpload(bool useSsl, string hostName, string userName, RMSecureString password, string remoteDirectory, string fileName, EventHandler <FtpUploadProgressEventArgs> progressEventHandler)
        {
            FtpWebRequest ftpRequest = null;

            try
            {
                ftpRequest             = (FtpWebRequest)WebRequest.Create("ftp://" + hostName + remoteDirectory + Path.GetFileName(fileName));
                ftpRequest.Method      = WebRequestMethods.Ftp.UploadFile;
                ftpRequest.Proxy       = null;
                ftpRequest.UseBinary   = true;
                ftpRequest.UsePassive  = true;
                ftpRequest.Credentials = new NetworkCredential(userName, password.GetPlainText());
                ftpRequest.KeepAlive   = false;
                //ftpRequest.EnableSsl = ASSL;
                //if (ASSL) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(FtpUploadCertificateValidation);

                FileInfo FI = new FileInfo(fileName);
                using (Stream Reader = FI.OpenRead())
                {
                    using (Stream Writer = ftpRequest.GetRequestStream())
                    {
                        FtpUploadProgressEventArgs e = new FtpUploadProgressEventArgs(FI.Length);
                        byte[] Buffer         = new byte[8192];
                        long   TotalBytesRead = 0;

                        progressEventHandler(null, e);
                        int BytesRead = Reader.Read(Buffer, 0, Buffer.Length);
                        while (BytesRead > 0)
                        {
                            TotalBytesRead += BytesRead;

                            Writer.Write(Buffer, 0, BytesRead);
                            e.BytesSent = TotalBytesRead;
                            progressEventHandler(null, e);

                            BytesRead = Reader.Read(Buffer, 0, Buffer.Length);
                        }
                    }
                }
                ftpRequest.GetResponse().Close();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                ftpRequest = null;
            }
        }
コード例 #2
0
ファイル: WebUtils.cs プロジェクト: Robin--/RMLib
        public static void Email(string smtpHostname, int smtpPort, MailAddress fromAddress, MailAddress toAddress, MailAddress senderAddress, string subject, string body, bool isBodyHtml, string smtpUsername, RMSecureString smtpPassword, bool ssl)
        {
            MailMessage Msg = new MailMessage(fromAddress, toAddress);
            Msg.Sender = senderAddress;
            Msg.Subject = subject;
            Msg.Body = body;
            Msg.IsBodyHtml = isBodyHtml;

            SmtpClient Smtp = new SmtpClient(smtpHostname);
            if ((smtpUsername.Length > 0) && (smtpPassword.Length > 0))
            {
                Smtp.UseDefaultCredentials = false;
                Smtp.Credentials = new NetworkCredential(smtpUsername, smtpPassword.GetPlainText());
            }
            Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            Smtp.EnableSsl = ssl;
            Smtp.Port = smtpPort;
            Smtp.Timeout = 10000;
            Smtp.Send(Msg);
        }
コード例 #3
0
ファイル: WebUtils.cs プロジェクト: Robin--/RMLib
        public static void Email(string smtpHostname, int smtpPort, MailAddress fromAddress, MailAddress toAddress, MailAddress senderAddress, string subject, string body, bool isBodyHtml, string smtpUsername, RMSecureString smtpPassword, bool ssl)
        {
            MailMessage Msg = new MailMessage(fromAddress, toAddress);

            Msg.Sender     = senderAddress;
            Msg.Subject    = subject;
            Msg.Body       = body;
            Msg.IsBodyHtml = isBodyHtml;

            SmtpClient Smtp = new SmtpClient(smtpHostname);

            if ((smtpUsername.Length > 0) && (smtpPassword.Length > 0))
            {
                Smtp.UseDefaultCredentials = false;
                Smtp.Credentials           = new NetworkCredential(smtpUsername, smtpPassword.GetPlainText());
            }
            Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            Smtp.EnableSsl      = ssl;
            Smtp.Port           = smtpPort;
            Smtp.Timeout        = 10000;
            Smtp.Send(Msg);
        }
コード例 #4
0
ファイル: WebUtils.cs プロジェクト: Robin--/RMLib
        public static bool FtpUpload(bool useSsl, string hostName, string userName, RMSecureString password, string remoteDirectory, string fileName, EventHandler<FtpUploadProgressEventArgs> progressEventHandler)
        {
            FtpWebRequest ftpRequest = null;
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + hostName + remoteDirectory + Path.GetFileName(fileName));
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                ftpRequest.Proxy = null;
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.Credentials = new NetworkCredential(userName, password.GetPlainText());
                ftpRequest.KeepAlive = false;
                //ftpRequest.EnableSsl = ASSL;
                //if (ASSL) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(FtpUploadCertificateValidation);

                FileInfo FI = new FileInfo(fileName);
                using (Stream Reader = FI.OpenRead())
                {
                    using (Stream Writer = ftpRequest.GetRequestStream())
                    {
                        FtpUploadProgressEventArgs e = new FtpUploadProgressEventArgs(FI.Length);
                        byte[] Buffer = new byte[8192];
                        long TotalBytesRead = 0;

                        progressEventHandler(null, e);
                        int BytesRead = Reader.Read(Buffer, 0, Buffer.Length);
                        while (BytesRead > 0)
                        {
                            TotalBytesRead += BytesRead;

                            Writer.Write(Buffer, 0, BytesRead);
                            e.BytesSent = TotalBytesRead;
                            progressEventHandler(null, e);

                            BytesRead = Reader.Read(Buffer, 0, Buffer.Length);
                        }
                    }
                }
                ftpRequest.GetResponse().Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                ftpRequest = null;
            }
        }
コード例 #5
0
        /// <summary>
        /// Custom constructor to load the given INI into memory, and indicate whether the changes should be buffered or immediately written to disk
        /// </summary>
        /// <param name="fileName">The INI to load</param>
        /// <param name="password">The password used to encrypt/decrypt the contents of the INI file</param>
        public IniFile(string fileName, RMSecureString password)
        {
            AutoSave = false;
            Password = new RMSecureString();

            _FileName = fileName;
            Password  = password;

            List <string> CurrentComment = new List <string>();
            string        CurrentSection = "";

            if (File.Exists(fileName))
            {
                // Read in the INI file
                string FileText = FileUtils.FileReadAllText(fileName, RMEncoding.Ansi);

                // Decrypt the INI file (if necessary)
                if (password.Length > 0)
                {
                    try
                    {
                        FileText = AES.Decrypt(FileText, password.GetPlainText(), INI_FILE_SALT);
                        Password = password;
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }

                // Split the INI file into separate lines
                string[] Lines = FileText.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');

                // Loop through each line
                foreach (string Line in Lines)
                {
                    // Make sure the line isn't empty
                    if ((!string.IsNullOrEmpty(Line.Trim())))
                    {
                        // Check if this is a comment
                        if (Line.TrimStart().StartsWith(";"))
                        {
                            if (string.IsNullOrEmpty(CurrentSection))
                            {
                                _HeaderComment.Add(Line.TrimStart().Substring(1));
                            }
                            else
                            {
                                CurrentComment.Add(Line.TrimStart().Substring(1));
                            }
                        }
                        else
                        {
                            // Check if this is a new section
                            if ((Line.TrimStart().StartsWith("[")) && (Line.TrimEnd().EndsWith("]")))
                            {
                                // It is, so add the new section
                                CurrentSection            = Line.Trim().Substring(1, Line.Trim().Length - 2);
                                _Sections[CurrentSection] = new IniFileSection(CurrentComment);
                                _SectionNames.Add(CurrentSection);
                                CurrentComment = new List <string>();
                            }
                            else
                            {
                                // It isn't so add the key/value to the current section
                                if (!string.IsNullOrEmpty(CurrentSection))
                                {
                                    // Make sure this line is in the KEY=VALUE form
                                    int EqualPos = Line.IndexOf('=');
                                    if (EqualPos >= 1)
                                    {
                                        // Get the key
                                        string Key = Line.Substring(0, EqualPos);

                                        // Get the value
                                        string Value = Line.Substring(EqualPos + 1);

                                        // Add the key/value pair to the dictionary
                                        _Sections[CurrentSection].WriteString(CurrentComment, Key, Value);
                                        CurrentComment = new List <string>();
                                    }
                                }
                            }
                        }
                    }
                }

                // Ensure the supplied password is valid
                if (Password.Length > 0)
                {
                    if ((_HeaderComment.Count == 0) || (_HeaderComment[0] != INI_FILE_ENCRYPTED_HEADER))
                    {
                        // Password did not properly decrypt the ini file
                        _Sections.Clear();
                        _SectionNames.Clear();
                        return;
                    }
                }
            }
        }
コード例 #6
0
ファイル: IniFile.cs プロジェクト: Robin--/RMLib
        /// <summary>
        /// Custom constructor to load the given INI into memory, and indicate whether the changes should be buffered or immediately written to disk
        /// </summary>
        /// <param name="fileName">The INI to load</param>
        /// <param name="password">The password used to encrypt/decrypt the contents of the INI file</param>
        public IniFile(string fileName, RMSecureString password)
        {
            AutoSave = false;
            Password = new RMSecureString();

            _FileName = fileName;
            Password = password;

            List<string> CurrentComment = new List<string>();
            string CurrentSection = "";
            if (File.Exists(fileName))
            {
                // Read in the INI file
                string FileText = FileUtils.FileReadAllText(fileName, RMEncoding.Ansi);

                // Decrypt the INI file (if necessary)
                if (password.Length > 0)
                {
                    try
                    {
                        FileText = AES.Decrypt(FileText, password.GetPlainText(), INI_FILE_SALT);
                        Password = password;
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }

                // Split the INI file into separate lines
                string[] Lines = FileText.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');

                // Loop through each line
                foreach (string Line in Lines)
                {
                    // Make sure the line isn't empty
                    if ((!string.IsNullOrEmpty(Line.Trim())))
                    {
                        // Check if this is a comment
                        if (Line.TrimStart().StartsWith(";"))
                        {
                            if (string.IsNullOrEmpty(CurrentSection))
                            {
                                _HeaderComment.Add(Line.TrimStart().Substring(1));
                            }
                            else
                            {
                                CurrentComment.Add(Line.TrimStart().Substring(1));
                            }
                        }
                        else
                        {
                            // Check if this is a new section
                            if ((Line.TrimStart().StartsWith("[")) && (Line.TrimEnd().EndsWith("]")))
                            {
                                // It is, so add the new section
                                CurrentSection = Line.Trim().Substring(1, Line.Trim().Length - 2);
                                _Sections[CurrentSection] = new IniFileSection(CurrentComment);
                                _SectionNames.Add(CurrentSection);
                                CurrentComment = new List<string>();
                            }
                            else
                            {
                                // It isn't so add the key/value to the current section
                                if (!string.IsNullOrEmpty(CurrentSection))
                                {
                                    // Make sure this line is in the KEY=VALUE form
                                    int EqualPos = Line.IndexOf('=');
                                    if (EqualPos >= 1)
                                    {
                                        // Get the key
                                        string Key = Line.Substring(0, EqualPos);

                                        // Get the value
                                        string Value = Line.Substring(EqualPos + 1);

                                        // Add the key/value pair to the dictionary
                                        _Sections[CurrentSection].WriteString(CurrentComment, Key, Value);
                                        CurrentComment = new List<string>();
                                    }
                                }
                            }
                        }
                    }
                }

                // Ensure the supplied password is valid
                if (Password.Length > 0)
                {
                    if ((_HeaderComment.Count == 0) || (_HeaderComment[0] != INI_FILE_ENCRYPTED_HEADER))
                    {
                        // Password did not properly decrypt the ini file
                        _Sections.Clear();
                        _SectionNames.Clear();
                        return;
                    }
                }
            }
        }