c#将本地文件上传至服务器(内网)

2021-02-11 14:19

阅读:596

标签:远程服务器   共享   style   error   file   info   out   ice   created   

            //连接  
                    string serverFolder = @"\\192.168.1.26\Invoice";//服务器的共享文件夹路径
                    string PWD = "666";//远程服务器密码
                    status = connectState(serverFolder, "Administrator", PWD);
                    if (status)
                    {
                        //共享文件夹的目录  
                        DirectoryInfo theFolder = new DirectoryInfo(serverFolder);
                        string filename = theFolder.ToString();
                        //执行方法   
                        if (File.Exists(serverFolder + @"\" + str_InvoiceName))//远程服务器上存在次文件那么下载,否则会报系统错误
                        {
                            TransportRemoteToLocal(destinationFile, serverFolder + @"\", str_InvoiceName);    //实现将远程服务器文件写入到本地  
                        }
                        //string destinationFile = Path_Folder + "\\" + str_InvoiceName.Remove(0, 17);
                        //file.CopyTo(destinationFile, true);
                        ////File.Copy(sourceFile, "");
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("不在内网,下载Invoice失败!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

通过CMD连接远程服务器的方法

 ///   
        /// 连接远程共享文件夹  
        ///   
        /// 远程共享文件夹的路径  
        /// 用户名  
        /// 密码  
        ///   
        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();
                proc.StandardInput.WriteLine("net use * /del /y");
                string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
                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);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }
   
将远程服务器文件写入到本地的方法
        ///   
        /// 从远程服务器下载文件到本地  
        ///   
        /// 下载到本地后的文件路径,包含文件的扩展名  
        /// 远程服务器路径(共享文件夹路径)  
        /// 远程服务器(共享文件夹)中的文件名称,包含扩展名  
        public static void TransportRemoteToLocal(string src, string dst, string fileName)  //src:下载到本地后的文件路径     dst:远程服务器路径    fileName:远程服务器dst路径下的文件名  
        {
            if (!Directory.Exists(dst))
            {
                Directory.CreateDirectory(dst);
            }
            dst = dst + fileName;
            FileStream inFileStream = new FileStream(dst, FileMode.Open);    //远程服务器文件  此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错  

            FileStream outFileStream = new FileStream(src, FileMode.OpenOrCreate);   //从远程服务器下载到本地的文件  

            byte[] buf = new byte[inFileStream.Length];

            int byteCount;

            while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
            {

                outFileStream.Write(buf, 0, byteCount);

            }

            inFileStream.Flush();

            inFileStream.Close();

            outFileStream.Flush();

            outFileStream.Close();

        }

 

c#将本地文件上传至服务器(内网)

标签:远程服务器   共享   style   error   file   info   out   ice   created   

原文地址:https://www.cnblogs.com/evanmemo/p/8492441.html


评论


亲,登录后才可以留言!