读书人

C#客户端WinForm如何自动(不许要用户

发布时间: 2013-11-16 23:15:33 作者: rapoo

C#客户端WinForm怎么自动(不许要用户选择文件)上传日志到Web服务器
C#客户端WinForm怎么自动(不许要用户选择文件)上传日志到Web服务器
1.WinForm客户端或者Web客户端能自动从某个设定的文件夹或文件上传到Web服务器;
2.上传的文件的大小在100kb之内;
3.最好是能简单点实现 WinForm自动上传 Web客户端自动上传 从固定文件夹上传文件 C#自动上传 客户端自动上传到服务器
[解决办法]
1、服务端的IIS里建立FTP,要设置目录(可写入权限),用户名与密码,
2、写个FTP的类,网上很多,http://blog.csdn.net/hejialin666/article/details/3522815
3、timer1定时器加载FTP
简单代码如下:
using System.IO;
using System.Net;

  public static int UploadFtp(string filePath, string filename, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(filePath + "\\" + filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
try
{
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
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.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
return 0;
}
catch (Exception ex)
{
reqFTP.Abort();
return -2;
}


}

private void timer1_Tick(object sender, EventArgs e)
{
int t = UploadFtp(@"d:\log", "log.txt", "192.168.0.2", "user", "password");
if (t == 0)
{
listBox1.Items.Add(DateTime.Now.ToString() + " 日志文件已上传");
File.Delete(@"d:\log\log.txt");
}
}


[解决办法]
那你做成B/S模式,通过网页来提交来得方便。如
asp.net实现ftp上传代码)http://www.cnblogs.com/LYunF/archive/2012/03/16/2399927.html,客户端不用安装程序,FTP的代码用户也看不到,

读书人网 >C#

热点推荐