本文介绍了腻子 - 动态地使用C#删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在一个Windows应用程序用C#作为编程langugage工作。 要求 要登录到动态腻子 从特定位置删除旧文件 我目前使用下面的代码登录到腻子,但我怎么跑delete命令?? 字符串主机名=主机名; 串登录=登录; 字符串密码=密码; 的ProcessStartInfo的StartInfo =新的ProcessStartInfo(); startInfo.FileName = @C:\Putty\putty.exe startInfo.Arguments =的String.Format({0} @ {1} -pw {2},登录,主机名,密码); startInfo.Start(); 我想通过这样的事情 RM MYFILE * .TXT 但在此之前,我必须通过 CD / DIR1 / DIR2 / NewFolder 我想删除NewFolder ?? 下的所有 .TXT 文件解决方案 腻子有命令行选项的 -m {script_file} 它允许您指定要对远程服务器上运行一个脚本文件。所有的运行命令后,腻子将退出。 您可以保存命令脚本文件运行,调用腻子,并删除脚本文件,当你 。再完成 下面的代码对我的作品: 字符串主机=主机名; 串登录=登录; 字符串密码=密码; 字符串命令=RM〜/ DIR1 / DIR2 / NewFolder / * .TXT; //这个修改,以满足您的需求 常量字符串scriptFileName = @remote_commands.sh File.WriteAllText(scriptFileName,命令); 变种的StartInfo =新的ProcessStartInfo(); startInfo.FileName = @C:\Putty\putty.exe startInfo.Arguments =的String.Format({0} @ {1} -pw {2} -m {3},登录,主机名,密码,scriptFileName); VAR过程=新的Process {StartInfo的=的StartInfo}; 的Process.Start(); process.WaitForExit(); File.Delete(scriptFileName); 如果你想要做的就是发送一个命令到服务器,该解决方案就行了。如果您需要更多先进的功能,如读取服务器响应,你应该检查出托马斯的回答。 编辑: 下面是如何使用砰砰运行命令,并得到它们的输出: 字符串主机=主机名; 串登录=登录; 字符串密码=密码; 变种的StartInfo =新的ProcessStartInfo(); startInfo.FileName = @C:\Putty\plink.exe startInfo.RedirectStandardInput = TRUE; startInfo.RedirectStandardOutput = TRUE; startInfo.UseShellExecute = FALSE; startInfo.Arguments =的String.Format({0} @ {1} -pw {2},登录,主机名,密码);使用 (VAR过程=新的Process {StartInfo的=的StartInfo}) {的Process.Start(); process.StandardInput.WriteLine(LS); process.StandardInput.WriteLine(回声运行多个命令在这里......'); process.StandardInput.WriteLine(退出); //确保我们的出口在年底 字符串输出= process.StandardOutput.ReadToEnd(); Console.WriteLine(输出); } I am working on a Windows app with C# as a programming langugage.Requirement isto login to putty dynamicallydelete old files from specific locationI am currently using below code to login to putty, but how do i ran the delete command ??string hostname = "hostname";string login = "login";string password = "password";ProcessStartInfo startInfo = new ProcessStartInfo();startInfo.FileName = @"C:\Putty\putty.exe";startInfo.Arguments = string.Format("{0}@{1} -pw {2}", login, hostname, password);startInfo.Start();I want to pass something like this rm myFile*.txtBut before that I have to navigate to particular location bycd /dir1/dir2/NewFolderI wanted to delete all the .txt files under NewFolder?? 解决方案 Putty has the command line option -m {script_file} which allows you to specify a script file to be run against the remote server. After all the commands are run, putty will exit.You could save the command to be run in a script file, call Putty, and delete the script file when you're done.The following code works for me:string hostname = "hostname";string login = "login";string password = "password";string command = "rm ~/dir1/dir2/NewFolder/*.txt"; // modify this to suit your needsconst string scriptFileName = @"remote_commands.sh";File.WriteAllText(scriptFileName, command);var startInfo = new ProcessStartInfo();startInfo.FileName = @"C:\Putty\putty.exe";startInfo.Arguments = string.Format("{0}@{1} -pw {2} -m {3}", login, hostname, password, scriptFileName);var process = new Process {StartInfo = startInfo};process.Start();process.WaitForExit();File.Delete(scriptFileName);If all you want to do is send a single command to the server, this solution will do. If you need more advanced functionality, like reading the server response, you should check out Thomas' answer.Edit:Here's how to use plink to run commands and get their output:string hostname = "hostname";string login = "login";string password = "password";var startInfo = new ProcessStartInfo();startInfo.FileName = @"C:\Putty\plink.exe";startInfo.RedirectStandardInput = true;startInfo.RedirectStandardOutput = true;startInfo.UseShellExecute = false;startInfo.Arguments = string.Format("{0}@{1} -pw {2}", login, hostname, password);using (var process = new Process {StartInfo = startInfo}){ process.Start(); process.StandardInput.WriteLine("ls"); process.StandardInput.WriteLine("echo 'run more commands here...'"); process.StandardInput.WriteLine("exit"); // make sure we exit at the end string output = process.StandardOutput.ReadToEnd(); Console.WriteLine(output);} 这篇关于腻子 - 动态地使用C#删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 09:36