c# 通過文件夾共享復制文件到服務器


 
          
public static string[] GetFileNames(string directoryPath, string searchName)
        {
            return Directory.GetFiles(directoryPath, searchName, SearchOption.AllDirectories);
        }
 
          

 



public
class NetFileConntion { public static bool ConnectState(string path) { return ConnectState(path, "", ""); } public static bool ConnectState(string path, string userName, string passWord) { bool flag = false; Process proc = new Process(); try { proc.StartInfo.FileName = " cmd.exe "; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); string dosLine = @"net use * /del /y net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES"; proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine(" exit "); while (!proc.HasExited) { proc.WaitForExit(1000); } string errormsg = proc.StandardError.ReadToEnd(); proc.StandardError.Close(); if (string.IsNullOrEmpty(errormsg)) { flag = true; } else { throw new Exception(errormsg); } } finally { proc.Close(); proc.Dispose(); } return flag; } }

 

 /// <summary>
        /// 數據處理
        /// </summary>
        public override void Execute(JobExcutePara para)
        {
            if (ConfigHelper.Settings("RemoteCopyFileSwitch") != "1")
            {
                return;
            }

            //讀取所有文件
            var files = DirFileHelper.GetFileNames(ExcelRootPath, "*.*").Where(t => t.Contains("\\U_")).ToList();
            //遠程地址
            var remotepaths = ConfigHelper.Settings("RemoteRootPaths")
                .Split(new[] {''}, StringSplitOptions.RemoveEmptyEntries);
            var remoteusers = ConfigHelper.Settings("RemoteUserPass")
                .Split(new[] {''}, StringSplitOptions.RemoveEmptyEntries);
            var sr=new StringBuilder();
            foreach (var remotepath in remotepaths)
            {
                sr.Append(remotepath + ",");
                try
                {
                    bool status = NetFileConntion.ConnectState(remotepath, remoteusers[0], remoteusers[1]);
                    sr.Append(status + "," + files.Count + "," + remoteusers[0] + "," + remoteusers[1]);
                    if (status)
                    {
                        var startTime = new DateTime(DateTime.Now.Year,
                            DateTime.Now.Month,
                            DateTime.Now.Day,
                            0, 0, 0);
                        //讀取目標目錄文件
                        var theFiles = DirFileHelper.GetFileNames(remotepath, "*.*");
                        sr.Append(theFiles.Length + ",");

                        foreach (var file1 in files)
                        {
                            var fileinfo_1=new FileInfo(file1);
                            if (fileinfo_1.LastWriteTime < startTime)
                            {
                                continue;
                            }
                            var filepath = file1.Replace(ExcelRootPath, string.Empty);
                            var flag = false;
                            foreach (var file2 in theFiles)
                            {
                                //檢查文件是否存在
                                if (file2.Contains(filepath))
                                {
                                    flag = true;
                                    break;
                                }
                            }
                            if (!flag)
                            {
                                //沒有則進行寫入復制
                                var targetPath = remotepath + filepath;
                                if (!Directory.Exists(Path.GetDirectoryName(targetPath)))
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
                                }
                                File.Copy(file1, targetPath);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper._Error("JobExcute__CopyFile ERROR", ex);
                }
            }
            //LogHelper._Info(sr.ToString());
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM