使用C#Windows服务应用程序上载文件 [英] Upload file using C# Windows service application
本文介绍了使用C#Windows服务应用程序上载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
i希望使用简单的C#widows服务应用程序将文件上传到ftp服务器,这是我的代码
Hello guys,
i want to upload a file to ftp server using simple C# widows service application, and this is my code
protected void UploadToFTP(string path, string name)
{
Logger("Executing UploadToFTP Function . . .");
Logger("UploadToFTP Report :: " + path + "," + name);
String sourcefilepath = path;
String ftpurl = "ftp://cnetiran.com";
String ftpusername = "cnetiran.com";
String ftppassword = "************";
try
{
string filename = Path.GetFileName(sourcefilepath);
string ftpfullpath = ftpurl + "//rasool//" + name;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(sourcefilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (Exception ex)
{
Logger("UploadToFTP Function Says : " + ex.ToString());
}
Logger("End of UploadToFTP . . .");
}
i在C#Windows应用程序中测试此代码,这是有效的,但在C#Windows服务应用程序中不能正常工作。
如何解决这个问题?
i test this code in C# Windows application, and that''s work, but in C# Windows service app does not work properly.
how can i solve this problem???
推荐答案
我是假设无法正常工作意味着它根本不会转移任何东西。您记录的错误消息可能与此相关 - 您是否看过它?
如果不自行运行此代码作为服务,我不得不猜测,但是我的第一个想法是查看服务访问权限。因为当你将它作为应用程序运行它并且不作为服务时,执行应用程序的用户可能无法访问该文件,但是对于完全以不同用户身份运行的服务可能无法访问该文件。您可能必须更改权限,或以其他用户身份运行服务。这可能有所帮助: http://stackoverflow.com/questions/ 4453646 / windows-service-choose-user-or-system-on-install [ ^ ]
I''m assuming that "does not work properly" means it transfers nothing at all. The error message you have logged will probably be relevant here - have you looked at it?
Without running this code as a service myself, I would have to guess, but my first thought would be to look at the service access permissions. Since when you run it as an app it works, and doesn''t as a service, it is likely that the file is accessible to the user who executes the application, but not to the service, which runs as a different user altogether. You may have to either change teh permissions, or run teh service as a different user. This may help: http://stackoverflow.com/questions/4453646/windows-service-choose-user-or-system-account-on-install[^]
这篇关于使用C#Windows服务应用程序上载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文