Click here to Skip to main content
15,030,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys, as you can see my question I'm trying to use powershell using c# winform to send an ftp, this my be a bit odd because there is a much simple way, i did this because i can't connect in my client but when i'm using a powershell i can connect to them. Our client already tried to whitelist my IP in their side but still zero chance of connecting using my app that is why i create the code below. This my first time using this i really don't know what am i doing.

Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript("open 192.168.100.101");
            pipeline.Commands.AddScript("user1");
            pipeline.Commands.AddScript("demo");
            pipeline.Commands.AddScript("lcd c:\ftpuploads");
            pipeline.Commands.AddScript("put samplefile.txt");
            Collection <PSObject> results = pipeline.Invoke();
            runspace.Close();
            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject pSObject in results)
            {
                stringBuilder.AppendLine(pSObject.ToString());
            }
            return stringBuilder.ToString();


What I have tried:

i'd really like to my code below but still i can't connect to my client due to the passive server error i know this a firewall cause but they put me in a whitelist and the powershell is working

string FileName = filePath.Split(Path.DirectorySeparatorChar).Last();
            bool usePassive = Settings.Default.UsePassive;
            bool useBinary = Settings.Default.UseBinary;
            bool keepAlive = Settings.Default.KeepAlive;
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + FileName);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
                request.UseBinary = true;
                request.UsePassive = true;
                request.KeepAlive = false;
                request.Proxy = new WebProxy();
                using (FileStream stream = File.OpenRead(filePath))
                {
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    stream.Close();
                    using (Stream reqStream = request.GetRequestStream())
                    {
                        reqStream.Write(buffer, 0, buffer.Length);
                        reqStream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
Posted
Comments
Reden Rodriguez 1-Feb-21 20:39pm
   
Hi Richard care to explain the code below?

ftp.UploadFile(@"D:\Github\FluentFTP\README.md", "/public_html/temp/README.md");

Does the "/public_html/temp/README.md" is the directory of the client? is there a way to just dump all files in the ftp server without the directory like my code in the above or I really need to know their directory to dump my files?
Reden Rodriguez 2-Feb-21 1:55am
   
Hi Richard no need to explain I already get it and successfully tested the link that you gave to me. Thank you very much sir

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900