Exemplo n.º 1
0
        /// <summary>
        /// Saves the specified FTP Options Connection File.
        /// </summary>
        /// <param name="ops">The FTP Options to save</param>
        /// <param name="conName">The file name to use</param>
        public void Save(FTPOptions ops, string conName)
        {
            string        rootPath = Path.Combine(Application.StartupPath, "FTP");
            XmlTextWriter xtw      = new XmlTextWriter(Path.Combine(rootPath, RemoveUnsupportedChars(conName) + ".xml"), Encoding.UTF8);

            xtw.WriteStartDocument();
            xtw.WriteStartElement("FTPOptions");
            xtw.WriteStartElement("UserName");
            xtw.WriteString(ops.UserName);
            xtw.WriteEndElement();
            xtw.WriteStartElement("UserPassword");
            xtw.WriteString(ops.UserPassword);
            xtw.WriteEndElement();
            xtw.WriteStartElement("FTPAddress");
            xtw.WriteString(ops.FTPAddress);
            xtw.WriteEndElement();
            xtw.WriteStartElement("UsePassive");
            xtw.WriteString(ops.UsePassive.ToString());
            xtw.WriteEndElement();
            xtw.WriteStartElement("AnonymousLogin");
            xtw.WriteString(ops.AnonymousLogin.ToString());
            xtw.WriteEndElement();
            xtw.WriteStartElement("UseSSL");
            xtw.WriteString(ops.UseSSL.ToString());
            xtw.WriteEndElement();
            xtw.WriteEndElement();
            xtw.WriteEndDocument();
            xtw.Close();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Lists the available files in an FTP Remote path
        /// </summary>
        /// <param name="Options">The FTP Options to use</param>
        /// <param name="RemotePath">The remote path to list files for</param>
        /// <param name="Extensions">The file extension list</param>
        /// <returns>The list of files to display</returns>
        public static List <string> ListFiles(FTPOptions Options, string RemotePath, List <string> Extensions)
        {
            List <string> returnList = new List <string>();
            FtpWebRequest request    = SetupRequest(Options, RemotePath);

            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response       = (FtpWebResponse)request.GetResponse();
            Stream         responseStream = response.GetResponseStream();
            StreamReader   sr             = new StreamReader(responseStream);
            string         line           = sr.ReadLine();

            while (line != null)
            {
                if (line.Contains("."))
                {
                    foreach (string ext in Extensions)
                    {
                        if (line.Contains("." + ext))
                        {
                            returnList.Add(line);
                            break;
                        }
                    }
                }
                line = sr.ReadLine();
            }
            sr.Close();
            response.Close();
            return(returnList);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Downloads the selected file from the FTP site
        /// </summary>
        /// <param name="Options">The FTP Options to use</param>
        /// <param name="RemotePath">The remove file path to download</param>
        /// <returns>The file contents after download</returns>
        public static string Get(FTPOptions Options, string RemotePath)
        {
            FtpWebRequest request = SetupRequest(Options, RemotePath);

            request.Method = WebRequestMethods.Ftp.DownloadFile;
            FtpWebResponse response       = (FtpWebResponse)request.GetResponse();
            Stream         responseStream = response.GetResponseStream();
            StreamReader   sr             = new StreamReader(responseStream);
            string         s = sr.ReadToEnd();

            sr.Close();
            response.Close();
            return(s);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Uploads a file to the specified FTP site.
        /// </summary>
        /// <param name="Options">The FTP Options to use</param>
        /// <param name="fileContents">The contents of the file to upload</param>
        /// <param name="RemotePath">The remote path to upload file to</param>
        /// <param name="fileIndex">The index of the file to upload</param>
        /// <returns>The file name after upload</returns>
        public static string Send(FTPOptions Options, string fileContents, string RemotePath, int fileIndex)
        {
            byte[]        contents    = Encoding.UTF8.GetBytes(fileContents);
            string        returnFName = String.Format("HL7Analyst{0}{1}.hl7", DateTime.Now.ToString("MMddyyyyHHmmss"), fileIndex);
            FtpWebRequest request     = SetupRequest(Options, RemotePath + "/" + returnFName);

            request.Method        = WebRequestMethods.Ftp.UploadFile;
            request.ContentLength = contents.Length;
            Stream requestStream = request.GetRequestStream();

            requestStream.Write(contents, 0, contents.Length);
            requestStream.Close();
            return(returnFName);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads the specified FTP Connection file
        /// </summary>
        /// <param name="FTPFile">The FTP Connection file to open</param>
        /// <returns>The FTP Options pulled from the FTP Connection file</returns>
        public static FTPOptions Load(string FTPFile)
        {
            string        rootPath = Path.Combine(Application.StartupPath, "FTP");
            XmlTextReader xtr      = new XmlTextReader(Path.Combine(rootPath, FTPFile + ".xml"));

            xtr.Read();
            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(xtr);

            FTPOptions ftpo = new FTPOptions();

            if (xDoc.SelectSingleNode("FTPOptions/UserName") != null)
            {
                ftpo.UserName = xDoc.SelectSingleNode("FTPOptions/UserName").InnerText;
            }
            if (xDoc.SelectSingleNode("FTPOptions/UserPassword") != null)
            {
                ftpo.UserPassword = xDoc.SelectSingleNode("FTPOptions/UserPassword").InnerText;
            }
            if (xDoc.SelectSingleNode("FTPOptions/FTPAddress") != null)
            {
                ftpo.FTPAddress = xDoc.SelectSingleNode("FTPOptions/FTPAddress").InnerText;
            }
            if (xDoc.SelectSingleNode("FTPOptions/UsePassive") != null)
            {
                ftpo.UsePassive = Convert.ToBoolean(xDoc.SelectSingleNode("FTPOptions/UsePassive").InnerText);
            }
            if (xDoc.SelectSingleNode("FTPOptions/UserName") != null)
            {
                ftpo.AnonymousLogin = Convert.ToBoolean(xDoc.SelectSingleNode("FTPOptions/AnonymousLogin").InnerText);
            }
            if (xDoc.SelectSingleNode("FTPOptions/UseSSL") != null)
            {
                ftpo.UseSSL = Convert.ToBoolean(xDoc.SelectSingleNode("FTPOptions/UseSSL").InnerText);
            }
            xtr.Close();
            return(ftpo);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sets up the FTPWebRequest object to be used
        /// </summary>
        /// <param name="Options">The FTP Options to use</param>
        /// <param name="RemotePath">The remote path to use</param>
        /// <returns>The FTP Web Request to be used</returns>
        private static FtpWebRequest SetupRequest(FTPOptions Options, string RemotePath)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(RemotePath);

            request.UsePassive = Options.UsePassive;
            if (Options.AnonymousLogin)
            {
                request.Credentials = new NetworkCredential("Anonymous", "");
            }
            else
            {
                request.Credentials = new NetworkCredential(Options.UserName, Options.UserPassword);
            }
            if (Options.UseSSL)
            {
                request.EnableSsl = true;
            }
            else
            {
                request.EnableSsl = false;
            }
            return(request);
        }