使用Net Use在C#中读写局域网内其他服务器上的文件
在局域网内像处理本地磁盘上的文件一样进行文件的读写操作,有很多方案,
FTP :配置较麻烦,而且是异步传输,传输完成的结果响应比较复杂;
物理映射:需要在本地添加共享用户的账号
Net Use:可以直接使用指定的共享目录账号(不需要在本地创建账号)进行访问,使用此Dos命令建立连接后,就可以直接像本地磁盘文件一样进行操作了。
以下是实现的C#代码
代码
/// <summary>
/// Connet to remote share folder
/// </summary>
/// <param name="remoteHost">remote server IP or machinename.domain name</param>
/// <param name="shareName">share name</param>
/// <param name="userName">user name of remote share access account</param>
/// <param name="passWord">password of remote share access account</param>
/// <returns>connect result</returns>
public static bool Connect(string remoteHost, string shareName, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = @"net use \"
/// Connet to remote share folder
/// </summary>
/// <param name="remoteHost">remote server IP or machinename.domain name</param>
/// <param name="shareName">share name</param>
/// <param name="userName">user name of remote share access account</param>
/// <param name="passWord">password of remote share access account</param>
/// <returns>connect result</returns>
public static bool Connect(string remoteHost, string shareName, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = @"net use \"
补充:软件开发 , C# ,
上一个:C# webservice调用方法总结
下一个:C#XML操作类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,
部份技术文章来自网络,