C#读写远程文件

024h 2006-12-07 05:49:35
A and B两台服务器,从A服务器上将文件保存至B服务器,如将024h.txt 存至
\\10.d.d.d\File目录。请提供解决思路或参考代码,谢谢!
...全文
737 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
024h 2006-12-08
  • 打赏
  • 举报
回复
to silent_x(),你的代码OK在开发环境OK.多谢!
024h 2006-12-08
  • 打赏
  • 举报
回复
using System.Management;

......


//连接远程电脑
ConnectionOptions options = new ConnectionOptions();
options.Username = textBox1.Text;
options.Password = textBox2.Text;
ManagementScope scope = new ManagementScope(textBox3.Text,options);
try
{
scope.Connect();
ManagementObject disk = new ManagementObject(scope,new ManagementPath("Win32_logicaldisk='i:'"),null);
disk.Get();
textBox6.Text = disk.Path + textBox4.Text;
FileInfo fi= new FileInfo(disk.Path + textBox4.Text);
FileStream fs=fi.Create();
StreamWriter sw=new StreamWriter(fs);
sw.Write(textBox5.Text);
sw.Close();
}
catch(Exception ex)
{
textBox5.Text = ex.ToString();
}


line: scope.Connect();返回如下错误信息
System.Runtime.InteropServices.COMException (0x800706BA): RPC 服务器不可用。
at System.Management.ThreadDispatch.Start()
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementScope.Connect()
at WindowsApplication1.Form1.button1_Click(Object sender, EventArgs e) in c:\documents and settings\wjd\my documents\visual studio projects\windowsapplication1\windowsapplication1\form1.cs:line 215


再试试上面仁兄的方法
024h 2006-12-08
  • 打赏
  • 举报
回复
客户也是从数据安全的角度.
OK,试试上面的方法,我也了解到另外一个思路,正准备尝试.有结果了上来回复.
scow 2006-12-08
  • 打赏
  • 举报
回复
UNC, mappdive都要完全的权限, 连FTP和web站点都不让建,估计悬
silent_x 2006-12-08
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace IISWatcher
{

[StructLayout(LayoutKind.Sequential)]
public class NetMappingDrive
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;

//如果正确,返回值是0;否则错误。网络访问错误代码53。
[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
public static extern uint WNetAddConnection2([In] NetMappingDrive lpNetResource, string lpPassword, string lpUsername, uint dwFlags);

[DllImport("Mpr.dll")]
public static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);

public NetMappingDrive(string LocalDrive, string NetPath,string Username,string Password)
{
this.dwScope = 2;
this.dwType = 1;
this.dwDisplayType = 3;
this.dwUsage = 1;
this.LocalName = LocalDrive;
this.RemoteName = NetPath;
this.Provider = null;

}

public uint CreateDrive(string Username,string Password)
{
return WNetAddConnection2(this, Password, Username, 0);
}

public uint DeleteDrive()
{
return WNetCancelConnection2(this.LocalName, 1, true);
}
}



//添加映射网络驱动器调用的代码如下:

//NETRESOURCE myNetResource = new NETRESOURCE();

//myNetResource.dwScope = 2; //2:RESOURCE_GLOBALNET

//myNetResource.dwType = 1 ; //1:RESOURCETYPE_ANY

//myNetResource.dwDisplayType = 3; //3:RESOURCEDISPLAYTYPE_GENERIC

//myNetResource.dwUsage = 1; //1: RESOURCEUSAGE_CONNECTABLE

//myNetResource.LocalName = "T:";

//myNetResource.RemoteName = yourNetworkPath;

//myNetResource.Provider = null;



//uint nret = WNetAddConnection2( myNetResource, pwd, username, 0);

//



////删除映射网络驱动器调用的代码如下:

//uint nret = WNetCancelConnection2( yourNetDriveName, 1, true);

这个类可以帮你

先把远程目录转换为本地映射

然后写

写完后把映射删掉
024h 2006-12-08
  • 打赏
  • 举报
回复
这也是个办法,TCPClient,好像过于复杂了些.难道C#的System.IO只能操作本地文件?它的SDK上面提到UNC,但是没有提供例子.郁闷!
scow 2006-12-08
  • 打赏
  • 举报
回复
socket点对点传输, 不过服务端要写程序.
024h 2006-12-08
  • 打赏
  • 举报
回复
共享方式简单是简单,身份认证的问题怎么解决?实际上是个如何通过代码解决这个权限的问题.
cangwu_lee 2006-12-08
  • 打赏
  • 举报
回复
共享方式,最简单。
024h 2006-12-08
  • 打赏
  • 举报
回复
客户的服务器管理很严,建一个FTP站点和WEB站点都有现成的解决办法,出于管理上的原因通不过.还真不相信C#没有这样的类可用,难道走socket编程?还不熟悉,呵呵.
zhaochong12 2006-12-07
  • 打赏
  • 举报
回复
套接字传输, 或者A开端口做一个文件服务器. 模拟WEB端口开放文件.
teayear 2006-12-07
  • 打赏
  • 举报
回复

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧