• 跨服务器存放文件(1)


     跨服务器保存文件:代码实现业务是将一个服务器上的文件保存到另一台服务器的共享目录下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 using System.IO;
    10 
    11 namespace 网络共享做本地映射
    12 {
    13     public partial class Form1 : Form
    14     {
    15         public Form1()
    16         {
    17             InitializeComponent();
    18         }
    19        
    20 
    21         string RemoteDirTo = @"\Netnetnet-pc站点发布"; //共享目录
    22         string FilePathTo = @"Test";//共享目录(子目录)
    23         string RemoteDirToUser = "admin2";//共享目录服务器用户
    24         string RemoteDirToPwd = "123";//共享目录服务器密码
    25         string RemoteDirFromFile = @"d:aaaa.txt";//本地文件
    26         string RemoteDirFromUser = "administrator";//本地用户
    27         string RemoteDirFromPwd = "zmkm";//本地密码
    28 
    29         private void button3_Click(object sender, EventArgs e)
    30         {
    31             try
    32             {
    33                 DataExtractor.SaveFileToService(RemoteDirTo, FilePathTo, RemoteDirToUser, RemoteDirToPwd, RemoteDirFromFile, RemoteDirFromUser, RemoteDirFromPwd);
    34             }
    35             catch (Exception ex)
    36             {
    37                 MessageBox.Show(ex.Message);
    38             }
    39         }
    40 
    41 
    42     }
    43 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.IO;
     5 
     6 namespace 网络共享做本地映射
     7 {
     8     public class DataExtractor
     9     {
    10         /// <summary>
    11         /// 将文件保存到文件服务器
    12         /// </summary>
    13         /// <param name="RemoteDirTo">共享目录</param>
    14         /// <param name="FilePathTo">共享目录(子目录)</param>
    15         /// <param name="RemoteDirToUser">共享目录服务器用户</param>
    16         /// <param name="RemoteDirToPwd">共享目录服务器密码</param>
    17         /// <param name="RemoteDirFromFile">本地文件</param>
    18         /// <param name="RemoteDirFromUser">本地用户</param>
    19         /// <param name="RemoteDirFromPwd">本地密码</param>
    20         public static void SaveFileToService(string RemoteDirTo, string FilePathTo, string RemoteDirToUser, string RemoteDirToPwd, string RemoteDirFromFile, string RemoteDirFromUser, string RemoteDirFromPwd)
    21         {
    22             FilePathTo = RemoteDirTo.TrimEnd('\') + "\" + FilePathTo.TrimEnd('\').TrimStart('\');
    23             try
    24             {
    25                 Reconnect(RemoteDirFromFile, RemoteDirFromUser, RemoteDirFromPwd);
    26                 Reconnect(RemoteDirTo, RemoteDirToUser, RemoteDirToPwd);
    27                 if (!Directory.Exists(FilePathTo))
    28                 {
    29                     try
    30                     {
    31                         Directory.CreateDirectory(FilePathTo);
    32                     }
    33                     catch (Exception ex)
    34                     {
    35                         throw new Exception("[创建目录[" + FilePathTo + "]失败:]" + ex.Message);
    36                     }
    37                 }
    38 
    39                 if (File.Exists(RemoteDirFromFile))
    40                 {
    41                     File.Copy(RemoteDirFromFile, FilePathTo + "\" + Path.GetFileName(RemoteDirFromFile), true);
    42                 }
    43             }
    44             catch (Exception ex)
    45             {
    46                 throw new Exception("保存xml文件到文件服务器失败:" + ex.Message);
    47             }
    48         }
    49 
    50         public static void Reconnect(string connectRemotePath, string user, string pwd)
    51         {
    52             try
    53             {
    54                 connectRemotePath.TrimEnd('\');
    55                 System.Diagnostics.Process p = new System.Diagnostics.Process();
    56                 p.StartInfo.FileName = "CMD.exe";
    57                 p.StartInfo.Arguments = "   /c   net   use   "" + connectRemotePath + ""   /user:"" + user + ""   " + pwd;
    58                 p.StartInfo.RedirectStandardOutput = true;
    59                 p.StartInfo.RedirectStandardInput = true;
    60                 p.StartInfo.UseShellExecute = false;
    61                 p.StartInfo.CreateNoWindow = true;
    62                 p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    63                 //p.StandardInput.Write(@"net   use   " + connectRemotePath + "/user:" + user + "   " + pwd + " /PERSISTENT:YES");
    64                 //p.StandardInput.WriteLine("exit");
    65                 p.Start();
    66                 p.WaitForExit(1000);
    67                 p.Close();
    68                 p.Dispose();
    69             }
    70             catch (Exception ex)
    71             {
    72                 throw new Exception("访问目录出错:" + ex.Message);
    73             }
    74         }
    75     }
    76 }
    View Code
  • 相关阅读:
    字段修改名称
    coercing to Unicode: need string or buffer, geoprocessing value object found
    为什么ArcGIS 10.3导出 Shapefile的字段名会被截断成3个汉字?解决方法如下
    arcgis python 使用光标和内存中的要素类将数据加载到要素集 学习:http://zhihu.esrichina.com.cn/article/634
    arcgis python 获得arcgis安装版本和安装位置
    arcgis python 不知道一个工具怎么用
    arcgis 地理坐标系 699个,投影坐标系是4767
    arcgis python 参数类型和含义
    win10 svn commit无响应
    新建网站与新建Asp.Net Web 应用程序的区别
  • 原文地址:https://www.cnblogs.com/yf2011/p/3989312.html
Copyright © 2020-2023  润新知