向远程目录写数据

首先得要将远程目录设置为共享,然后需要设置权限,具体操作为:在文件夹右键-》属性 弹出的面板中,“共享“项中设置共享此文件夹,然后点权限按钮设置权限,添加Administrator(连接是用的用户名),ASPNET,NETWORK SERVICES用户,并将它们附上“完全控制“权限,同样在安全项中也添加上述三个用户,设置“完全控制“权限。

以下为具体代码:

private RemoteCopy m_remoteCopy; 

public void WriteData()

{

    FileStream stream = null;

   StreamWriter streamwriter = null;

  try

   {

            String m_RemoteHost = ConfigurationManager.AppSettings["RemoteHost"]; //"10.150.39.188"远程主机

            String m_RemoteUser = ConfigurationManager.AppSettings["RemoteUser"]; //"Administrator"用户名

            String m_RemotePwd = ConfigurationManager.AppSettings["RemotePwd"];   //"123456"密码

           // m_remoteCopy = new RemoteCopy(m_RemoteHost, m_RemoteUser, m_RemotePwd);//初使化远程主机、用户名、密码

           // int n = m_remoteCopy.Initialize();//初始化与远程主机的连接,在已经连接过的情况下将不再连接
                  
    (以上两句不要也是可行的,为了程序严谨和程序健壮,还是要加上的)

            string FileName = @"\\" + "10.150.39.26" + "\\RemoteCopy\\resultaa.txt";

             stream = File.Open(FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

          streamwriter = new StreamWriter(stream);         
           streamwriter.WriteLine(0.123555 + "," +0.256885 + "," + 4568 + "," + 54555);//随便写一行数据

            streamwriter.Flush();

   }

   catch (Exception e)

   {

        //此处还需添加异常处理

   }

  finally

  {

     streamwriter.Close();

     stream.Close();

    //  m_remoteCopy.UnInitialize();//断开与远程主机的连接(与前面连接初始化两句一样,为了严谨和程序健壮,还是需要加上)

  }

  //连接与断开远程主机类

public class RemoteCopy

    {

        private bool m_bConnected;

        private string m_remoteHost;

        private string m_userName;

        private string m_passWord;

        //在构造函数中初使化远程主机、用户名、密码

        public RemoteCopy(string remoteHost, string userName, string passWord)

        {

            this.m_remoteHost = remoteHost;

            this.m_userName = userName;

            this.m_passWord = passWord;

            this.m_bConnected = false;

        }

 

       

        /// <summary>

        /// 初始化与远程主机的连接,在已经连接过的情况下将不再连接

        /// </summary>

        /// <returns>

        /// 0:连接成功

        /// -1000:远程主机不存在

        /// -1001:连接失败,用户名或密码不正确

        /// </returns>

        public int Initialize()

        {

            if (m_bConnected)

                return 0;

            //Ping()执行ping命令行,查看远程主机是否能够连通

            if (Ping(m_remoteHost))//如果能ping通主机

            {

                if (Connect(m_remoteHost, m_userName, m_passWord))//Connect通过net use命令连接并登录远程主机

                {

                    m_bConnected = true;

                    return 0;

                }

                else

                {

                    m_bConnected = false;

                    return -1001;//连接失败,用户名或密码不正确

                }

            }

            else

            {

                m_bConnected = false;

                return -1000;//远程主机不存在

            }           

        }

 

        /// <summary>

        /// 断开与远程主机的连接

        /// </summary>

        public void UnInitialize()

        {

            string dosLine = @"net use "" " + m_remoteHost + "/del";

            string output, error;

            RunDosLine(dosLine, out output, out error);//执行Dos命令,并返回Dos输出和Dos错误

        }

        /// <summary>

        /// 通过net use命令连接并登录远程主机

        /// </summary>

        /// <param name="remoteHost">远程主机名或者ip地址</param>

        /// <param name="userName">登录用户名</param>

        /// <param name="passWord">登录密码</param>

        /// <returns></returns>

        private bool Connect(string remoteHost, string userName, string passWord)

        {

            string dosLine = @"net use """ + remoteHost + " " + passWord + " " + " /user:" + userName + ">NUL";

            string output, error;

            if (RunDosLine(dosLine, out output, out error))//执行Dos命令,并返回Dos输出和Dos错误

            {

                if (error.Length > 0)

                    return false;

                else

                    return true;

            }

            else

                return false;           

        }

 

        /// <summary>

        /// 执行ping命令行,查看远程主机是否能够连通

        /// </summary>

        /// <param name="remoteHost">远程主机名称或者ip地址</param>

        /// <returns>

        /// true:能够连通

        /// false:不能够连通

        /// </returns>

        private bool Ping(string remoteHost)

        {

            string dosLine = @"ping -n 1 " + remoteHost;

            string output = string.Empty;

            string error = string.Empty;

            //执行Dos命令,并返回Dos输出和Dos错误

            if (RunDosLine(dosLine, out output, out error))

            {

                if (output.IndexOf("(0% loss)") != -1)//存在,网络状态良好

                    return true;

                else

                    return false;

            }

            else

                return false;         

        }

 

        /// <summary>

        /// 执行Dos命令,并返回Dos输出和Dos错误

        /// </summary>

        /// <param name="dosLine">要执行的Dos命令</param>

        /// <param name="DosOutput">执行命令后的输出</param>

        /// <param name="DosError">执行命令后Dos返回的错误信息</param>

        /// <returns>

        /// true:命令已经执行

        /// false:命令未能执行或者执行失败

        /// </returns>

        private bool RunDosLine(string dosLine, out string DosOutput,out string DosError)

        {

            bool flag = true;

            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();               

                proc.StandardInput.WriteLine(dosLine);

                proc.StandardInput.WriteLine("exit");

                while (proc.HasExited == false)

                {

                    proc.WaitForExit(500);

                }

                DosOutput = proc.StandardOutput.ReadToEnd();              

                proc.StandardOutput.Close();

                DosError = proc.StandardError.ReadToEnd();

                proc.StandardError.Close();

            }

            catch (Exception)

            {

                flag = false;

                DosOutput = string.Empty;

                DosError = string.Empty;

            }

            finally

            {               

                proc.Close();

                proc.Dispose();

            }

            return flag;

        }

}

posted @ 2009-07-28 19:35  周文  阅读(348)  评论(0编辑  收藏  举报