解决方案:

1. 在目标服务器上发布webservice,实现文件下载的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Data; namespace TRNWebService
{
/// <summary>
/// FileHelperService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class FileHelperService : System.Web.Services.WebService
{ /// <summary>
/// 从指定文件夹下获取文件
/// </summary>
/// <param name="dicpath"></param>
/// <returns></returns>
[WebMethod(EnableSession = true, Description = "获取文件夹下面的文件")]
public DataTable GetFileList(string dicpath)
{
DataTable dt = new DataTable();
dt.TableName = "AEFiles";
dt.Columns.Add("fileName", typeof(string));
dt.Columns.Add("filePath", typeof(string)); DirectoryInfo dir = System.IO.Directory.CreateDirectory(dicpath);
if (dir == null)
{
return null;
}
FileInfo[] files = dir.GetFiles();
for (int i = ; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
if (file != null)
{
dt.Rows.Add(file.Name, file.FullName);
}
} return dt; } [WebMethod(Description = "下载服务器站点文件,传递文件相对路径")]
public byte[] DownloadFile(string strFilePath)
{
FileStream fs = null;
//string CurrentUploadFolderPath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["UploadFileFolder"]);
//string CurrentUploadFilePath = CurrentUploadFolderPath + strFilePath;
string CurrentUploadFilePath = strFilePath;
if (File.Exists(CurrentUploadFilePath))
{
try
{
///打开现有文件以进行读取。
fs = File.OpenRead(CurrentUploadFilePath);
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = fs.ReadByte()) != -)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
catch (Exception ex)
{
return new byte[];
}
finally
{
fs.Close();
}
}
else
{
return new byte[];
}
}
}
}

webservice 下载文件

2. 客户端利用反射实现webservice,把文件下载到本地的安装目录

3. 客户端打开已经存放在本地的文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom.Compiler;
using System.Net;
using System.IO;
using Microsoft.CSharp;
using System.CodeDom;
using System.Web.Services.Description; namespace Frm.Common
{
public static class WebServiceInvoke
{
#region InvokeWebService
//动态调用web服务
public static object InvokeWebService(string url, string methodname, object[] args)
{
return InvokeWebService(url, null, methodname, args);
} public static object InvokeWebService(string url, string classname, string methodname, object[] args)
{
string @namespace = "TRNWebService";
if ((classname == null) || (classname == ""))
{
classname = GetWsClassName(url);
} try
{
//获取WSDL
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = ServiceDescription.Read(stream);
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
ICodeCompiler icc = csc.CreateCompiler(); //设定编译参数
CompilerParameters cplist = new CompilerParameters();
cplist.GenerateExecutable = false;
cplist.GenerateInMemory = true;
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.XML.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll"); //编译代理类
CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
} //生成代理实例,并调用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + classname, true, true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod(methodname); return mi.Invoke(obj, args);
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
}
} private static string GetWsClassName(string wsUrl)
{
string[] parts = wsUrl.Split('/');
string[] pps = parts[parts.Length - ].Split('.'); return pps[];
}
#endregion
}
}

c# 利用反射实现动态webservice

 /// <summary>
/// 通过WebService下载文件
/// </summary>
/// <param name="ServiceFilePath">服务器图片路径</param>
/// <param name="DownloadFolderPath">本地图片路径</param>
private string DownloadFile(string ServiceFilePath, string DownloadFolderPath)
{
try
{
string DownloadFileName = "";
if (ServiceFilePath.Contains("/"))
{
DownloadFileName = ServiceFilePath.Substring(ServiceFilePath.LastIndexOf("/"));
}
else if (ServiceFilePath.Contains("\\"))
{
DownloadFileName = ServiceFilePath.Substring(ServiceFilePath.LastIndexOf("\\")+);
}
else
{
DownloadFileName = ServiceFilePath;
} string DownloadFilePath = DownloadFolderPath + "\\" + DownloadFileName; Dictionary<string, string> dicSystem = Common.AppConfigSys.AppConfigDic;
string strUrl = dicSystem["webserviceURL"];//webservice的发布位置(http://192.168.10.3:8084/FileHelperService.asmx)
object[] objPara = new object[];
objPara[] = ServiceFilePath;
object objFile = Frm.Common.WebServiceInvoke.InvokeWebService(strUrl, "DownloadFile", objPara); byte[] bytes = (byte[])objFile;
if (bytes != null)
{
if (!Directory.Exists(DownloadFolderPath))
{
Directory.CreateDirectory(DownloadFolderPath);
}
if (!File.Exists(DownloadFilePath))
{
File.Create(DownloadFilePath).Dispose();
}
//如果不存在完整的上传路径就创建
FileInfo downloadInfo = new FileInfo(DownloadFilePath);
if (downloadInfo.IsReadOnly) { downloadInfo.IsReadOnly = false; }
//定义并实例化一个内存流,以存放提交上来的字节数组。
MemoryStream ms = new MemoryStream(bytes);
//定义实际文件对象,保存上载的文件。
FileStream fs = new FileStream(DownloadFilePath, FileMode.Create);
///把内内存里的数据写入物理文件
ms.WriteTo(fs);
fs.Flush();
ms.Flush();
ms.Close();
fs.Close();
fs = null;
ms = null;
}
return DownloadFilePath;
}
catch (Exception ex)
{
return "";
}
}

客户端文件下载方法

  void AElink_Click(object sender, EventArgs e)
{ LinkLabel link = sender as LinkLabel;
string serverfilePath = link.Tag.ToString();//服务器上的文件地址 string clientfilePath = DownloadFile(serverfilePath, Application.StartupPath + "\\UploadFileFolder");//客户端的文件地址
System.Diagnostics.Process.Start(clientfilePath);//打开本地文件 }

文件下载方法调用

winform 查看远程服务器上的文件的更多相关文章

  1. xshell终端向远程服务器上传文件方法

    centos-7下在本地终端里向远程服务器上传文件,在命令行中执行的软件. 安装命令如下: 在终端里输入如下命令: 会弹出如下窗口 选择你要上传的文件即可上传成功.

  2. Python: 如何判断远程服务器上Excel文件是否被人打开

    最近工作中需要去判断远程服务器上的某个Excel文件是否被打开,如果被人打开,则等待,如果没人打开使用,则去填写数据进Excel文件. 开始想的很简单,和其他语言一样,比如C#,打开文件,如果报错说明 ...

  3. php curl向远程服务器上传文件

    <?php /** * test.php: */ header('content-type:text/html;charset=utf8'); $ch = curl_init(); //加@符号 ...

  4. 模拟php curl向远程服务器上传文件

    test.php <?php header('content-type:text/html;charset=utf8'); $file = dirname(__FILE__).'/1.jpg'; ...

  5. Mac安装SSHFS挂载远程服务器上的文件夹到本地

    一.安装SSHFUS sshfs依赖于fuse,所以需要先安装fuse,这两个软件都可以在https://osxfuse.github.io/下载到. 注意安装顺序. 二.挂载文件夹到本地 输入一下命 ...

  6. php访问远程服务器上的文件

    test.php <?php $fp=fopen('http://www.baidu.com', 'r'); while (!feof($fp)) { $chunk=fgets($fp); ec ...

  7. 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器

    引入: 在我们昨天架设好了Samba服务器上并且创建了一个 Samba 账户后,我们就迫不及待的想用JAVA去操作Samba服务器了,我们找到了一个框架叫 jcifs,可以高效的完成我们工作. 实践: ...

  8. 利用SSH在本机和远程服务器之间传输文件或文件夹

    1.从远程服务器上下载文件到本机 scp <服务器用户名>@<服务器地址>:<服务器中要下载的文件路径> <下载到本机的绝对路径> 2.从本机上传本地文 ...

  9. scp 从远程服务器上一下载文件

    scp -P202 xx3.x6.xx.xx:/usr/local/zookeeper-.zip /tmp #指定远程服务器的端口和远程服务器的目标文件 ,最后指定要下载到本的地目录 也可以从远程服务 ...

随机推荐

  1. @查看MySQL版本的方法

    1.在终端下:mysql -V. [root@localhost bin]# mysql -V; mysql Ver 14.14 Distrib 5.6.21, for Linux (x86_64) ...

  2. xss payload

    xss payload可以使用富客户端文本书写,大多数用javascript,少部分用actionscript等等. 1.盗取cookie,发起cookie劫持 使用xss漏洞插入cookie.js ...

  3. transparent shadow caster unity

    https://forum.unity.com/threads/semitransparent-shadows.276490/ semitransparent shadows dither 类似alp ...

  4. Vmware 14.0 版本中安装Ubuntu 17.10版本无法调整分辨率的问题

    装完ubuntu后发现在vmware中选择了查看-自动调整大小-自适应客户机,虚拟机也无法随着窗口大小来切换分辨率,其实是因为WAYLAND限制了. 1. 先安装vim sudo apt-get in ...

  5. Android模糊演示样例-RenderScript-附效果图与代码

    本文链接    http://blog.csdn.net/xiaodongrush/article/details/31031411 參考链接    Android高级模糊技术    http://s ...

  6. nginx安装说明

    下载地址:http://nginx.org/en/download.html 安装版本:1.10.0 安装配置如下: /etc/nginx 目录 /home/nginx目录 --prefix=/hom ...

  7. Unity3d 录像

    flashtd1: 回复 tqfa :我觉得是有方法可以实现的,之前使用高通的增强显示开发包时发现其实它是添加了一个类似movietexture的东西,叠加在它的摄像机上 如果文档里有操作moviet ...

  8. 算法笔记_075:蓝桥杯练习 最短路(Java)

    目录 1 问题描述 2 解决方案 2.1 floyd算法解决 2.2 spfa算法解决   1 问题描述 问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从 ...

  9. Struts2的国际化入门

    Struts2的国际化入门 Struts2国际化是建立在Java国际化的基础上的,一样是通过提供不同国家/语言环境的消息资源,然后通过ResourceBundle加载指定Locale对应的资源文件,再 ...

  10. Java的内存机制(转)

    0.参考资料: http://www.j2megame.org/index.php/content/view/2246/125.html 1.Java的内存机制 Java 把内存划分成两种:一种是栈内 ...