日期:2014-05-18  浏览次数:20922 次

求C# windows应用程序上传文件到指定IP的机器上。
例如:

窗体1 : 有个按钮,图片框,当点这个按钮的时候 就传送1.jpg 到IP为 192.168.1.2的机器的D:\images下。

传送成功后,图片框从192.168.1.2 的机器上 加载D:\images\1.jpg

怎么实现这个过程。 

说白了 就是 实现类似QQ头像上传的效果的。上传到服务器。并且下次登陆从服务器那边取出来。

最好有关键部分的代码。

------解决方案--------------------
首先必须上传的服务器支持文件共享或者Web共享,当然可以通过Web Service之类的方法来解决。
上传时将文件数据和文件名作为参数传过去,在目标服务器上保存这些数据;
获取的时候,将文件名作为参数穿过去,目标服务器将文件数据传过来。
------解决方案--------------------
通过使用FTPwebrequest上传文件
或web services
private bool FtpUpFile(string strFileName,string strFtpUrl,string strFtpUser,string strFtpPassword) 

try 

FileInfo fileInf = new FileInfo(strFileName); 
FtpWebRequest reqFTP; 
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFtpUrl + fileInf.Name)); 
reqFTP.Credentials = new NetworkCredential(strFtpUser, strFtpPassword); 
reqFTP.KeepAlive = false; 
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
reqFTP.UseBinary = true; 
reqFTP.ContentLength = fileInf.Length; 
int buffLength = 2048; 
byte[] buff = new byte[buffLength]; 
int contentLen; 
FileStream fs = fileInf.OpenRead(); 
Stream strm = reqFTP.GetRequestStream(); 
contentLen = fs.Read(buff, 0, buffLength); 
int allbye = (int)fileInf.Length; 
int startbye = 0; 
while (contentLen != 0) 

strm.Write(buff, 0, contentLen); 
contentLen = fs.Read(buff, 0, buffLength); 
startbye += contentLen; 

strm.Close(); 
fs.Close(); 
return true; 

catch 

return false; 



}
------解决方案--------------------
C# code
 
/// <summary>
  /// 建立DOS连接
  /// </summary>
  /// <param name="path">要建立连接的服务器IP以及路径 </param>
  static public void DOSConnected()
  {
    string path = GetServerSavePath();
    Process proc = new Process(); //实例化一个Process类
    proc.StartInfo.FileName = "cmd.exe";  //设定程序名
    proc.StartInfo.Arguments = "/c net use Z: \\\\" + path;  //设定程式执行参数,此命令意思是将参数path代表的服务器路径虚拟为本地的Z盘
    proc.StartInfo.CreateNoWindow = false;  //设置不显示窗口
    proc.StartInfo.UseShellExecute = false; //关闭Shell的使用
    proc.StartInfo.RedirectStandardInput = true;//重定向标准输入
    proc.StartInfo.RedirectStandardOutput = true; //重定向标准输出
    proc.StartInfo.RedirectStandardError = true; //重定向错误输出
    proc.Start();                //启动
    proc.WaitForExit();
  }
  /// <summary>
  /// 文件拷贝 从网站服务器拷贝到文件服务器
  /// </summary>
  /// <param name="FilePath">本地文件路径 </param>
  static public void DOSXCopy(string FilePath)
  {
    try
    {
      Process proc = new Process(); //实例化一个Proces