iT邦幫忙

0

【求救】C# 本地資料夾與FTPSserver下的資料夾內的檔案動作

  • 分享至 

  • xImage

各位先進們,不好意思小弟又來求助了。

想要達成的目標是本機的資料夾下的檔案與FTP下的檔案互相比較差異後,達成資料複製、更名等動作。

目前在本機下實作成功但轉至FTP下就迷失了方向了,請各位先進們幫忙指點一下,感恩。

程式畫面:
https://ithelp.ithome.com.tw/upload/images/20190123/20114042Mr4jnBIwWb.png

程式碼:

namespace File_Check_Copy_Rename
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox2.Text = "";
            List<string> ListResult = new List<string>();
            ListResult = GetFtpList();

            int count = 0;
            foreach(string LR in ListResult)
            {
                count += 1;
                richTextBox2.Text += count + ":  " + LR + "\n";
            }





        }
        public List<string> GetFtpList()
        {
            List<string> strList = new List<string>();

            FtpWebRequest f = (FtpWebRequest)WebRequest.Create("ftp://localhost");
            f.Method = WebRequestMethods.Ftp.ListDirectory;
            f.UseBinary = true;
            f.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
            f.Credentials = new NetworkCredential("admin", "admin");

            StreamReader sr = new StreamReader(f.GetResponse().GetResponseStream());

            string str = sr.ReadLine();
            while (str != null)
            {
                strList.Add(str);
                str = sr.ReadLine();

            }

            sr.Close();
            sr.Dispose();
            f = null;

            return strList;


        }




        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox3.Text += GetDif((@"D:\test\Target"), (@"D:\test\Source"));
            FileCopy((@"D:\test\Target"), (@"D:\test\Source"));
        }

       
            public string GetDif(string folder1, string folder2)
            {

                string diff = "", filename;
                string[] fns1 = Directory.GetFiles(folder1), fns2 = Directory.GetFiles(folder2);
                Hashtable ht = new Hashtable();
                foreach (string fn in fns1)
                {

                    filename = Path.GetFileName(fn);
                    ht[filename] = 0;//標記資料夾1和資料夾2所有文件名不一样
                }
                foreach (string fn in fns2)
                { //找出資料夾2不再資料夾1中存在的文件名
                    filename = Path.GetFileName(fn);
                    if (ht.ContainsKey(filename)) ht[filename] = 1;//一样
                    else
                    {
                        diff += fn + "\n";
                    }
                }
                List<string> difs = new List<string>(fns1);
                difs.AddRange(fns2);
                foreach (string fn in difs.Distinct<string>())
                {
                    Console.WriteLine(fn);
                }
                foreach (string fn in ht.Keys) if (ht[fn].ToString() == "0") diff += folder1 + "\\" + fn + "\n";//得到資料夾1和資料夾2不一樣文件名
                return diff;
            }

            public void  FileCopy(string folder1, string folder2)
            {
                string diff = "";
                string filename;
                string[] fns1 = Directory.GetFiles(folder1,"*.txt"), fns2 = Directory.GetFiles(folder2, "*.txt");
                int count = 0;
                Hashtable ht = new Hashtable();
                foreach (string fn in fns1)
                {

                    filename = Path.GetFileName(fn);
                    ht[filename] = 0;//标记資料夾1和資料夾2所有文件名不一样
                    richTextBox1.Text += fn + "\n";

                }
                foreach (string fn in fns2)
                { //找出資料夾2不再資料夾1中存在的文件名
                
                    filename = Path.GetFileName(fn);

                    if (ht.ContainsKey(filename))
                    {
                        ht[filename] = 1;//一样

                    }
                    else
                    {
                       diff += fn + "\n";
                       count++;
                        //richTextBox3.Text += count + " : " + fn + "\n";

                        if (File.Exists(Path.Combine(Path.GetDirectoryName(fn), Path.ChangeExtension(Path.GetFileName(fn), "recv"))))
                        {
                            File.Copy(Path.Combine(Path.GetDirectoryName(fn), Path.ChangeExtension(Path.GetFileName(fn), "recv")), Path.Combine(folder1, Path.ChangeExtension(Path.GetFileName(fn), "recv")),true);
                            richTextBox3.Text += "Copy To Target" + "\n";
                        }
                        else
                        {
                            File.Copy(fn, Path.Combine(Path.GetDirectoryName(fn), Path.ChangeExtension(Path.GetFileName(fn), "recv")), true);
                            richTextBox3.Text += "Make--" + Path.Combine(Path.GetDirectoryName(fn), Path.ChangeExtension(Path.GetFileName(fn), "recv")) + "\n";
                        }

                           //richTextBox3.Text += Path.Combine(Path.GetDirectoryName(fn), Path.ChangeExtension(Path.GetFileName(fn), "recv")) + "\n";
                    }
                        richTextBox2.Text += fn + "\n";

                }

               

            }

            private void button3_Click(object sender, EventArgs e)
            {
                FileCheck((@"D:\test\Target"), (@"D:\test\Source"));
            }



            public void FileCheck(string folder1, string folder2)
            {
                //string diff = "";
                string filename;
                string[] fns1 = Directory.GetFiles(folder1, "*.recv"), fns2 = Directory.GetFiles(folder2, "*.recv");
                //int count = 0;
                Hashtable ht = new Hashtable();
            
                foreach (string fn in fns1)
                {

                    filename = Path.GetFileName(fn);
                    ht[filename] = 0;//標記資料夾1和資料夾2所有文件名不一樣
                    //richTextBox1.Text += fn + "\n";
                

                }
                foreach (string fn in fns2)
                { //找出資料夾下2不再資料夾下1中存在的文件名
                
                    filename = Path.GetFileName(fn);
                
                    if (ht.ContainsKey(filename))
                    {
                        ht[filename] = 1;//一样
                        FileStream Targetfile = File.OpenRead(Path.Combine(folder1, filename));
                        FileStream Sourcefile = File.OpenRead(fn);
                        if (Targetfile.Length == Sourcefile.Length)
                        {
                            MessageBox.Show("The Same");
                            Sourcefile.Close();
                            Targetfile.Close();

                            File.Move(fn,Path.Combine(Path.GetDirectoryName(fn), Path.ChangeExtension(Path.GetFileName(fn), "ok")));
                            File.Move(Path.Combine(folder1, filename), Path.Combine(Path.Combine(folder1, Path.ChangeExtension(Path.GetFileName(Path.Combine(folder1, filename)), "txt"))));
                        }
                        else
                        {                        
                            MessageBox.Show("Not Same");
                            Sourcefile.Close();
                            Targetfile.Close();
                            File.Move(fn, Path.Combine(Path.GetDirectoryName(fn), Path.ChangeExtension(Path.GetFileName(fn), "false")));
                        }
                    }
               
               

                }
            

            }
    }
}

程式流程:

1.捉出Source下所有 .txt檔案。
2.捉出Target下所有 .txt檔案。
3.輸出缺少檔案名稱。
4.Source與Target資料夾比較若Target資料夾下缺少Source資料夾裡的檔案的話。
5.Source資料夾產生Target缺少的檔案並將副檔案更名為.recv。
6.將Source資料夾下的.recv檔案COPY至Target資料夾下。
7.比對.recv檔案大小是否正確,若是:將Source資料夾下的.recv更改副檔名為.ok。若否則改成.fales
8.將Target資料夾下的.recv更改副檔名為.txt。


已上在本機實現完成,但轉成FTP後才發現,本人只可以取得到FTP的檔案LIST,但不能取得FTP的絕對路徑(EX:ftp://localhost/1.txt)。

求各位先進幫幫忙實現上方程式流程但是在FTP環境下,感恩!!

小魚 iT邦大師 1 級 ‧ 2019-01-23 20:58:03 檢舉
ftp不用絕對路徑, 只要相對路徑,
基本上根目錄的網址是已知的,
要不然根本連不到.
narumi iT邦新手 5 級 ‧ 2019-01-24 16:58:29 檢舉
感謝小魚大,路徑問題已經解決了。目前在把剩下的功能實作中。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答