Click here to Skip to main content
14,639,661 members
Rate this:
Please Sign up or sign in to vote.
See more:
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 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???
Posted
Updated 17-Apr-13 21:10pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^]
   
Comments
shajarian_lover 18-Apr-13 5:01am
   
when i use this code as a service app, it gets this error:

System.Net.WebException: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Stream.Dispose()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetRequestStream()
at Windows_Service_App.ScheduledService.UploadToFTP(String path, String name) in c:\Users\Rasool Ghafari\Desktop\Windows Service App\ScheduledService.cs:line 181
OriginalGriff 18-Apr-13 5:08am
   
Look at the message:
...(e.g., file not found, no access)...
If your user the service is running under does not have permission, you will get "no access"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100