C# 与命令行 FTP 帮助

发布于 2024-10-29 19:07:14 字数 2099 浏览 6 评论 0原文

我已经尝试了一段时间通过 C# 与我们的大型机建立 ftp 连接。到目前为止,我已经能够连接文件并将其上传到主机,这很棒,但是该文件的记录长度错误,并且设置为可变记录长度,而不是固定记录长度。经过相当多的研究后,我得出了与大多数人相同的结论,即 ftpwebresponse 功能将不允许向大型机发出“qoute SITE”命令。如果我错了,请毫不犹豫地纠正我,这是我正在使用的代码:

private void ftpButton_Click(object sender, EventArgs e)
{
    string ftphost = "ftpSite";  
    string user = "myUser";  
    string pwd= "myPass";
    string ftpfileName = "test.file2";
    string inputfilePath="d:\\documents and settings\\gheff\\My Documents\\Visual Studio 2005\\Projects\\biWeeklyJob\\BiWeekly\\bin\\Debug\\file2.TXT";

    string ftpfullpath = String.Format("ftp://{0}//'{1}'", ftphost,ftpfileName);             

    try  
    {                
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);  
        ftp.Credentials = new NetworkCredential(user,pwd);                 

        ftp.KeepAlive = true;  
        ftp.UseBinary = false;  //Use ascii. 

        ftp.Method = WebRequestMethods.Ftp.UploadFile;

        FileStream fs = File.OpenRead(inputfilePath);  
        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 (WebException ex)  
    {  
       String status = ((FtpWebResponse)ex.Response).StatusDescription;  
       throw new Exception(status);  
    }  
} 

所以,我没有被吓倒,我试图想出另一种方法,我已经成功地使用命令行上传文件,更改文件属性并还更改了文件类型,以便我可以将 JCL 作业发送到大型机。我现在正在努力将其实现到我现有的 C# 应用程序中,但事实证明比我想象的要困难。

我绝不是 C# 方面的专家,但可以使用答案进行更多研究以获得更好的理解。

我看过这段代码 C# cmd excute 命令,但是看起来周围似乎只能从这里发出一个命令。

所以我的问题是,我知道需要一段时间才能到达那里,是否可以在命令提示符下运行以下命令而不使用批处理文件,因为我在进程运行时正在查看一些反馈?

open "cmd.exe"
type "ftp"
type "FTPserver"
Type "USERNAME"
type "PASSWORD"

然后,一旦建立连接

,就运行预定义的命令,即上传文件、上传并运行 JCL 作业。 我想我正在寻找的是能够将文本写入 cmd.exe 但保持会话的东西?

我希望这是有道理的。

任何帮助将不胜感激。

谢谢

I've been trying for a while now to set up an ftp connection with our mainframe via c#.  So far I've been able to connect and upload a file to the mainframe, which is great, but the file has the wrong record length and is set up as variable, instead of fixed record length.  After quite a bit a research I have come to the same conclusion as most people, that the ftpwebresponse function will not allow "qoute SITE" commands to be issued to the mainframe. If i'm wrong please don't hesitate to correct me, heres the code i'm using:

private void ftpButton_Click(object sender, EventArgs e)
{
    string ftphost = "ftpSite";  
    string user = "myUser";  
    string pwd= "myPass";
    string ftpfileName = "test.file2";
    string inputfilePath="d:\\documents and settings\\gheff\\My Documents\\Visual Studio 2005\\Projects\\biWeeklyJob\\BiWeekly\\bin\\Debug\\file2.TXT";

    string ftpfullpath = String.Format("ftp://{0}//'{1}'", ftphost,ftpfileName);             

    try  
    {                
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);  
        ftp.Credentials = new NetworkCredential(user,pwd);                 

        ftp.KeepAlive = true;  
        ftp.UseBinary = false;  //Use ascii. 

        ftp.Method = WebRequestMethods.Ftp.UploadFile;

        FileStream fs = File.OpenRead(inputfilePath);  
        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 (WebException ex)  
    {  
       String status = ((FtpWebResponse)ex.Response).StatusDescription;  
       throw new Exception(status);  
    }  
} 

So undeterred, I tried to think of another approach, I have successfully used the command line to upload a file, change the file properties and also changed the filetype so I can send JCL jobs to the mainframe.  I am now tyring to implement this into my existing c# application, but proving harder than I thought.

I am by no means an expert in c#, but can use answers to then go and do more research to gain a better understanding.

I have seen this piece of code C# cmd excute commands, but looking around it seems that only one command can be issued from this.

So my question is, I know it took a while to get there, is it possible to run the follow commands on the command prompt without the use of a batch file as i'm looking some feedback as the process runs?

open "cmd.exe"
type "ftp"
type "FTPserver"
Type "USERNAME"
type "PASSWORD"

Then once connection has been established

then run pre-defined commands i.e upload files, upload and run JCL jobs.
I think what i'm looking for is somthing that will write text to cmd.exe but keep the session?

I hope this makes sense.

Any help would be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呆° 2024-11-05 19:07:14

所以这是您要求的代码,但我怀疑它能否解决您的问题。

var cmd= new Process();

cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;

cmd.Start();

using (var stdin = cmd.StandardInput)
{
   stdin.WriteLine("ftp");
   stdin.WriteLine("FTPserver");
   stdin.WriteLine("USERNAME");
   stdin.WriteLine("PASSWORD");
} 

cmd.WaitForExit();
cmd.Close();

So here is code you are asking for nevertheless I doubt that it solve your problem.

var cmd= new Process();

cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;

cmd.Start();

using (var stdin = cmd.StandardInput)
{
   stdin.WriteLine("ftp");
   stdin.WriteLine("FTPserver");
   stdin.WriteLine("USERNAME");
   stdin.WriteLine("PASSWORD");
} 

cmd.WaitForExit();
cmd.Close();
欢烬 2024-11-05 19:07:14

试试这个cmd:

FTP -s:ftpcommandfilename url

感觉follow模式只能运行一个命令,你可以将ftp cmd写入一个文件并使用这个cmd来执行它们。

WriteLine("FTP -s:ftpcommandfilename url");

对了,我还找到了 multi-ftp-cmd-line-in-code 模式,如果你找到了,分享一下吗?

Try this cmd:

FTP -s:ftpcommandfilename url

sense the follow mode can run one command only, you can write the ftp cmd in a file and use this cmd to execute them.

WriteLine("FTP -s:ftpcommandfilename url");

By the way, I am also finding the multi-ftp-cmd-line-in-code mode, if you have got it, sharing?

山田美奈子 2024-11-05 19:07:14

几年前,我使用这个 CodeProject 项目 来创建一个需要发送的简单应用程序一些文件。可能对您有帮助的是它使用套接字与其他服务器通信。您可以发送原始 FTP 命令,这应该可以避免使用 FtpWebRequest 对象时遇到的问题,而不必“退出”并使用 ftp 命令行程序。

I used this CodeProject project a few years ago for a simple application that needed to send some files. The thing that may help you is that it uses a socket to talk to the other server. You can send raw FTP commands which should sidestep the problems you're having with the FtpWebRequest object, without having to "shell out" and use the ftp command line program.

再可℃爱ぅ一点好了 2024-11-05 19:07:14

您可以尝试的另一种方法是提交 jcl 作业来分配具有流程所需的记录长度、块大小等的数据集。

一旦完成工作,将数据放入数据集中,您才正确分配。 Z/OS FTP服务器应从您现有数据集中拾取属性。

Another approach you could try is to submit a jcl job to allocate the dataset with the record length, block size, et al, that your process needs.

Once that job complets, "put" your data into the dataset you just allocated correctly. The z/OS ftp server should pick up the attributes from your existing dataset.

顾忌 2024-11-05 19:07:14

您是否考虑过使用支持 COM 接口并能够发送原始协议命令的商业 FTP 客户端?如果您正在编写要重新分发的软件,这可能并不经济,但如果您只是尝试自动化内部 IT 项目的重复任务,那么就您作为开发人员的时间而言,这绝对是值得的。 Robo-FTP 可能是连接到大型机的不错选择,因为它内置了 ASCII 到 EBCDIC 的转换。

Have you considered using a commercial FTP client that supports a COM interface and is able to send raw protocol commands? This might not be economical if you are writing software to redistribute but if you are only trying to automate a recurring task for an internal IT project it could definately be worth it in terms of your time as a developer. Robo-FTP might be a good choice for connecting to a mainframe because it has built in ASCII to EBCDIC translation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文