• C# 客户端读取共享目录文件


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.IO;
      6 using System.Diagnostics;
      7  
      8 namespace test3
      9 {
     10     class Program
     11     {
     12         static void Main(string[] args)
     13         {
     14             bool status = false;
     15  
     16             //连接共享目录
     17             status = connectState(@"\10.0.xx.xxxctec2", "waveaccess", "waveaccessxxx");
     18             if (status)
     19             {
     20                 //共享文件夹的目录
     21                 DirectoryInfo theFolder = new DirectoryInfo(@"\10.0.xx.xxxctec2"); 
     22                 string filename = theFolder.ToString();
     23                 //执行方法
     24                 TransportLocalToRemote(@"D:
    eadme1.txt", filename, "readme1.txt");  //实现将本地文件写入到远程服务器
     25                 TransportRemoteToLocal(@"D:
    eadme.txt", filename, "readme.txt");    //实现将远程服务器文件写入到本地
     26             }
     27             else
     28             {
     29                 Console.WriteLine("未能连接!");
     30             }
     31             Console.WriteLine("成功");
     32             Console.ReadKey();
     33         }
     34  
     35         public static bool connectState(string path)
     36         {
     37             return connectState(path, "", "");
     38         }
     39         /// <summary>
     40         /// 连接远程共享文件夹
     41         /// </summary>
     42         /// <param name="path">远程共享文件夹的路径</param>
     43         /// <param name="userName">用户名</param>
     44         /// <param name="passWord">密码</param>
     45         /// <returns></returns>
     46         public static bool connectState(string path, string userName, string passWord)
     47         {
     48             bool Flag = false;
     49             Process proc = new Process();
     50             try
     51             {
     52                 proc.StartInfo.FileName = "cmd.exe";
     53                 proc.StartInfo.UseShellExecute = false;
     54                 proc.StartInfo.RedirectStandardInput = true;
     55                 proc.StartInfo.RedirectStandardOutput = true;
     56                 proc.StartInfo.RedirectStandardError = true;
     57                 proc.StartInfo.CreateNoWindow = true;
     58                 proc.Start();
     59                 string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
     60                 proc.StandardInput.WriteLine(dosLine);
     61                 proc.StandardInput.WriteLine("exit");
     62                 while (!proc.HasExited)
     63                 {
     64                     proc.WaitForExit(1000);
     65                 }
     66                 string errormsg = proc.StandardError.ReadToEnd();
     67                 proc.StandardError.Close();
     68                 if (string.IsNullOrEmpty(errormsg))
     69                 {
     70                     Flag = true;
     71                 }
     72                 else
     73                 {
     74                     throw new Exception(errormsg);
     75                 }
     76             }
     77             catch (Exception ex)
     78             {
     79                 throw ex;
     80             }
     81             finally
     82             {
     83                 proc.Close();
     84                 proc.Dispose();
     85             }
     86             return Flag;
     87         }
     88  
     89         /// <summary>
     90         /// 从远程服务器下载文件到本地
     91         /// </summary>
     92         /// <param name="src">下载到本地后的文件路径,包含文件的扩展名</param>
     93         /// <param name="dst">远程服务器路径(共享文件夹路径)</param>
     94         /// <param name="fileName">远程服务器(共享文件夹)中的文件名称,包含扩展名</param>
     95         public static void TransportRemoteToLocal(string src, string dst, string fileName)  //src:下载到本地后的文件路径     dst:远程服务器路径    fileName:远程服务器dst路径下的文件名
     96         {
     97             if (!Directory.Exists(dst))
     98             {
     99                 Directory.CreateDirectory(dst);
    100             }
    101             dst = dst + fileName;
    102             FileStream inFileStream = new FileStream(dst, FileMode.Open);    //远程服务器文件  此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错
    103  
    104             FileStream outFileStream = new FileStream(src, FileMode.OpenOrCreate);   //从远程服务器下载到本地的文件
    105  
    106             byte[] buf = new byte[inFileStream.Length];
    107  
    108             int byteCount;
    109  
    110             while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
    111             {
    112  
    113                 outFileStream.Write(buf, 0, byteCount);
    114  
    115             }
    116  
    117             inFileStream.Flush();
    118  
    119             inFileStream.Close();
    120  
    121             outFileStream.Flush();
    122  
    123             outFileStream.Close();
    124             
    125         }
    126  
    127         /// <summary>
    128         /// 将本地文件上传到远程服务器共享目录
    129         /// </summary>
    130         /// <param name="src">本地文件的绝对路径,包含扩展名</param>
    131         /// <param name="dst">远程服务器共享文件路径,不包含文件扩展名</param>
    132         /// <param name="fileName">上传到远程服务器后的文件扩展名</param>
    133         public static void TransportLocalToRemote(string src, string dst, string fileName)    //src
    134         {
    135             FileStream inFileStream = new FileStream(src, FileMode.Open);    //此处假定本地文件存在,不然程序会报错   
    136  
    137             if (!Directory.Exists(dst))        //判断上传到的远程服务器路径是否存在
    138             {
    139                 Directory.CreateDirectory(dst);
    140             }
    141             dst = dst + fileName;             //上传到远程服务器共享文件夹后文件的绝对路径
    142  
    143             FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
    144  
    145             byte[] buf = new byte[inFileStream.Length];
    146  
    147             int byteCount;
    148  
    149             while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
    150             {
    151  
    152                 outFileStream.Write(buf, 0, byteCount);
    153  
    154             }
    155  
    156             inFileStream.Flush();
    157  
    158             inFileStream.Close();
    159  
    160             outFileStream.Flush();
    161  
    162             outFileStream.Close();
    163         }
    164     }
    165 }
  • 相关阅读:
    豆瓣api-简单跨域演示
    MVC4 验证用户登录一个特性搞定
    IIS7.5 不能加载外部引用的第三方JS,CSS,img等一系列静态文件
    三合一网站后台密码重置
    anaconda安装 (开源的Python发行版本)
    通达信指标函数说明大全
    织梦CMS如何修改中英文模板的当前位置
    微信小程序上架需要增值电信业务经营许可证ICP?
    PS抠图玻璃杯227
    PS用通道工具人物头发抠图246
  • 原文地址:https://www.cnblogs.com/singov/p/11850876.html
Copyright © 2020-2023  润新知