★如何用c#实现 对远程机器上的文本文件的读取

home_town1 2007-07-11 06:11:54
各位大虾,本人c#经验不足,麻烦帮忙解决这个问题(如题),最好能提供思路和例子,感激不尽!!
...全文
752 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
home_town1 2007-07-13
  • 打赏
  • 举报
回复
不好意思,IP那个地方可以了,现在就是判断是否连接成功的问题了。
home_town1 2007-07-13
  • 打赏
  • 举报
回复
还有一个问题就是,如何判断是否连接成功与否。
home_town1 2007-07-13
  • 打赏
  • 举报
回复
现在我用process对象调用dos命令实现了读取远程文本文件,但是还有2个问题,
1:我原来想建立连接之后读取,然后再取消连接,能不能在一个函数中完成,我没做出来,不得不把建立连接和取消连接做成了2个函数来使用。看起来比较罗嗦一些。
2:使用了映射驱动器,可是要求是用ip直接连,
lovefootball(蟑螂)也提到可以实现,不过我实在水平有限,没弄出来,不知道应该如何修改。
请指教。多谢。

下边是我实现的代码。请参考一下。
private void button2_Click(object sender, System.EventArgs e)
{
string ip="172.17.3.70";
//open connection
string strRst1=ConnectOpen(ip);
//MessageBox.Show(strRst1.ToString());

//read txt
string s;
StreamReader srReadLine = new StreamReader(
(System.IO.Stream)File.OpenRead("m:\\sql.txt"),//"Application.StartupPath.ToString().Trim()+"\\Text.txt"),
System.Text.Encoding.Default);
srReadLine.BaseStream.Seek(0, SeekOrigin.Begin);
while(srReadLine.Peek() > -1)
{
s=srReadLine.ReadLine().Trim();
this.listBox1.Items.Add(s);
}
srReadLine.Close();

//close connection
string strRst2=ConnectClose(ip);

//MessageBox.Show(strRst2.ToString());


}

private string ConnectClose(string strIp)

{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;

string pingrst;
p.Start();
p.StandardInput.WriteLine("net use \\172.17.3.77 /del");
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
pingrst = strRst;
p.Close();
return pingrst;

}

private string ConnectOpen(string strIp)

{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string pingrst;
p.Start();

//ping
p.StandardInput.WriteLine("ping "+strIp);
p.StandardInput.WriteLine("net use "+strIp+" /user:uid pwd");
p.StandardInput.WriteLine("net use m: \\\\172.17.3.70\\share");
p.StandardInput.WriteLine("exit");

string strRst = p.StandardOutput.ReadToEnd();

pingrst = strRst;
p.Close();
return pingrst;

}
yuan731 2007-07-12
  • 打赏
  • 举报
回复
jf
lovefootball 2007-07-12
  • 打赏
  • 举报
回复
不用建bat文件
直接在程序中执行命令就可以了
如果你用net use能连上对方机器
就象访问本地一样了
具体的权限应该就是你所用的登录对方机器账户的权限
访问对方机器的时候加上IP
home_town1 2007-07-12
  • 打赏
  • 举报
回复
谢谢啦,(公司不让用ftp方式),我看了上面的2个地址的帖子现在我的理解是:用c#调用dos命令执行(利用process对象),可以把dos命令放在一个.bat文件中,执行这个文件,或者直接把命令放到程序中来执行,大体思路对吗?

如果上边思路没问题的话,那么得出下边比较详细些的处理过程:
.bat文件中建立远程机器的连接,把远程机上的共享文件夹建立为映射驱动器,然后作为本地资源来访问其中的内容。
(此处还有一个问题就是对映射驱动器的访问是不是和本地驱动器访问一样呢,有没有受限的地方呢)


对于以上的理解,请指教。(在线等)
yuzl32 2007-07-11
  • 打赏
  • 举报
回复
FTP,Socket,Remoting,WMI。。。
lovefootball 2007-07-11
  • 打赏
  • 举报
回复
http://www.chinahtm.cn/program/16974.html
lovefootball 2007-07-11
  • 打赏
  • 举报
回复
使用Process的例子
http://topic.csdn.net/t/20040624/15/3119792.html

lovefootball 2007-07-11
  • 打赏
  • 举报
回复
先用pint命令看看通不通
然后用net use命令连接对方机器
前提是知道用户名密码

110,538

社区成员

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

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

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