예제 #1
0
        public int ReadFraSFtp()
        {
            string homedir = m_sftp.RealPath(".", "");
            //  Open a directory on the server...
            string handle = m_sftp.OpenDir(m_rec_sftp.Outbound);

            if (handle == null)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            //  Download the directory listing:
            Chilkat.SFtpDir dirListing = null;
            dirListing = m_sftp.ReadDir(handle);
            if (dirListing == null)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            Program.memPbsnetdir = null; //opret ny memPbsnetdir

            //  Iterate over the files.
            int i;
            int n = dirListing.NumFilesAndDirs;

            if (n > 0)
            {
                for (i = 0; i <= n - 1; i++)
                {
                    Chilkat.SFtpFile fileObj = null;
                    fileObj = dirListing.GetFileObject(i);
                    if (!fileObj.IsDirectory)
                    {
                        recPbsnetdir rec = new recPbsnetdir
                        {
                            Type     = 8,
                            Path     = dirListing.OriginalPath,
                            Filename = fileObj.Filename,
                            Size     = (int)fileObj.Size32,
                            Atime    = fileObj.LastAccessTime,
                            Mtime    = fileObj.LastModifiedTime,
                            Gid      = fileObj.Gid,
                            Uid      = fileObj.Uid,
                            Perm     = fileObj.Permissions.ToString()
                        };
                        Program.memPbsnetdir.Add(rec);
                    }
                }
            }

            //  Close the directory
            bool success = m_sftp.CloseHandle(handle);

            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            var leftqry_pbsnetdir =
                from h in Program.memPbsnetdir
                //join d1 in Program.dbData3060.Tblpbsfiles on new { h.Path, h.Filename } equals new { d1.Path, d1.Filename } into details
                //from d1 in details.DefaultIfEmpty(new Tblpbsfiles { Id = -1, Type = (int?)null, Path = null, Filename = null, Size = (int?)null, Atime = (DateTime?)null, Mtime = (DateTime?)null, Perm = null, Uid = (int?)null, Gid = (int?)null })
                //where d1.Path == null && d1.Filename == null
                select h;

            int AntalFiler = leftqry_pbsnetdir.Count();

            if (leftqry_pbsnetdir.Count() > 0)
            {
                foreach (var rec_pbsnetdir in leftqry_pbsnetdir)
                {
                    Tblpbsfiles m_rec_pbsfiles = new Tblpbsfiles
                    {
                        Type     = rec_pbsnetdir.Type,
                        Path     = rec_pbsnetdir.Path,
                        Filename = rec_pbsnetdir.Filename,
                        Size     = rec_pbsnetdir.Size,
                        Atime    = rec_pbsnetdir.Atime,
                        Mtime    = rec_pbsnetdir.Mtime,
                        Perm     = rec_pbsnetdir.Perm,
                        Uid      = rec_pbsnetdir.Uid,
                        Gid      = rec_pbsnetdir.Gid
                    };
                    Program.dbData3060.Tblpbsfiles.InsertOnSubmit(m_rec_pbsfiles);

                    //***********************************************************************
                    //  Open a file on the server:
                    string fullpath   = rec_pbsnetdir.Path + "/" + rec_pbsnetdir.Filename;
                    string filehandle = m_sftp.OpenFile(fullpath, "readOnly", "openExisting");
                    if (filehandle == null)
                    {
                        throw new Exception(m_sftp.LastErrorText);
                    }

                    int numBytes = (int)rec_pbsnetdir.Size;
                    if (numBytes < 0)
                    {
                        throw new Exception(m_sftp.LastErrorText);
                    }

                    //---------------------------------------------------------------------
                    byte[] b_data        = null;
                    bool   bEof          = false;
                    int    chunkSizeGet  = 10240;
                    int    chunkSizeRead = 0;
                    m_sftp.ClearAccumulateBuffer();
                    while (bEof == false)
                    {
                        chunkSizeRead = m_sftp.AccumulateBytes(handle, chunkSizeGet);
                        if (chunkSizeRead == -1)
                        {
                            throw new Exception(m_sftp.LastErrorText);
                        }
                        bEof = m_sftp.Eof(handle);
                    }
                    b_data = m_sftp.AccumulateBuffer;
                    //---------------------------------------------------------------------
                    if (b_data == null)
                    {
                        throw new Exception(m_sftp.LastErrorText);
                    }
                    sendAttachedFile(rec_pbsnetdir.Filename, b_data, false);
                    char[] c_data      = System.Text.Encoding.GetEncoding("windows-1252").GetString(b_data).ToCharArray();
                    string filecontens = new string(c_data);

                    string filecontens2 = filecontens.TrimEnd('\n');
                    string filecontens3 = filecontens2.TrimEnd('\r');
                    string filecontens4 = filecontens3.TrimEnd('\n');

                    string[] lines = filecontens4.Split('\n');
                    string   ln    = null;
                    int      seqnr = 0;
                    string   ln0_6;
                    for (int idx = 0; idx < lines.Count(); idx++)
                    {
                        ln = lines[idx].TrimEnd('\r');
                        try { ln0_6 = ln.Substring(0, 6); } catch { ln0_6 = ""; }
                        if (((seqnr == 0) && !(ln0_6 == "PBCNET")) || (seqnr > 0))
                        {
                            seqnr++;
                        }
                        if (ln.Length > 0)
                        {
                            m_rec_pbsfile = new Tblpbsfile
                            {
                                Seqnr = seqnr,
                                Data  = ln
                            };
                            m_rec_pbsfiles.Tblpbsfile.Add(m_rec_pbsfile);
                        }
                    }

                    m_rec_pbsfiles.Transmittime = DateTime.Now;
                    Program.dbData3060.SubmitChanges();

                    //  Close the file.
                    success = m_sftp.CloseHandle(filehandle);
                    if (success != true)
                    {
                        throw new Exception(m_sftp.LastErrorText);
                    }
                    //***********************************************************************************
                }
            }
            Program.dbData3060.SubmitChanges();
            return(AntalFiler);
        }