如何从此输出写入文本文件

Posted

技术标签:

【中文标题】如何从此输出写入文本文件【英文标题】:How to write a Text file from this Output 【发布时间】:2013-01-02 10:45:46 【问题描述】:

我有以下代码,以便我可以搜索目录以查找文件。现在我想为用户添加一种将输出保存到文本文件的方法?

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;

    namespace RecursiveSearchCS
    
        public class Form1 : System.Windows.Forms.Form
        
            internal System.Windows.Forms.Button btnSearch;
            internal System.Windows.Forms.TextBox txtFile;
            internal System.Windows.Forms.Label lblFile;
            internal System.Windows.Forms.Label lblDirectory;
            internal System.Windows.Forms.ListBox lstFilesFound;
            internal System.Windows.Forms.ComboBox cboDirectory;

            private System.ComponentModel.Container components = null;

            public Form1()
            

                InitializeComponent();

            

            protected override void Dispose(bool disposing)
            
                if (disposing)
                
                    if (components != null)
                    
                        components.Dispose();
                    
                
                base.Dispose(disposing);
            


            private void InitializeComponent()
            
                this.btnSearch = new System.Windows.Forms.Button();
                this.txtFile = new System.Windows.Forms.TextBox();
                this.lblFile = new System.Windows.Forms.Label();
                this.lblDirectory = new System.Windows.Forms.Label();
                this.lstFilesFound = new System.Windows.Forms.ListBox();
                this.cboDirectory = new System.Windows.Forms.ComboBox();
                this.SuspendLayout();
        // 
        // btnSearch
        // 
                this.btnSearch.Location = new System.Drawing.Point(608, 248);
                this.btnSearch.Name = "btnSearch";
                this.btnSearch.Size = new System.Drawing.Size(75, 23);
                this.btnSearch.TabIndex = 0;
                this.btnSearch.Text = "Search";
                this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
        // 
        // txtFile
        // 
                this.txtFile.Location = new System.Drawing.Point(8, 40);
                this.txtFile.Name = "txtFile";
                this.txtFile.Size = new System.Drawing.Size(120, 20);
                this.txtFile.TabIndex = 4;
                this.txtFile.Text = "*.*";
        // 
        // lblFile
        // 
                this.lblFile.Location = new System.Drawing.Point(8, 16);
                this.lblFile.Name = "lblFile";
                this.lblFile.Size = new System.Drawing.Size(144, 16);
                this.lblFile.TabIndex = 5;
                this.lblFile.Text = "Search for files containing:";
        // 
        // lblDirectory
        // 
                this.lblDirectory.Location = new System.Drawing.Point(8, 96);
                this.lblDirectory.Name = "lblDirectory";
                this.lblDirectory.Size = new System.Drawing.Size(120, 23);
                this.lblDirectory.TabIndex = 3;
                this.lblDirectory.Text = "Look In:";
        // 
        // lstFilesFound
        // 
                this.lstFilesFound.Location = new System.Drawing.Point(152, 8);
                this.lstFilesFound.Name = "lstFilesFound";
                this.lstFilesFound.Size = new System.Drawing.Size(528, 225);
                this.lstFilesFound.TabIndex = 1;
        // 
        // cboDirectory
        // 
                this.cboDirectory.DropDownWidth = 112;
                this.cboDirectory.Location = new System.Drawing.Point(8, 128);
                this.cboDirectory.Name = "cboDirectory";
                this.cboDirectory.Size = new System.Drawing.Size(120, 21);
                this.cboDirectory.TabIndex = 2;
                this.cboDirectory.Text = "ComboBox1";
        // 
        // Form1
        // 
                this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                this.ClientSize = new System.Drawing.Size(688, 277);
                this.Controls.Add(this.btnSearch);
                this.Controls.Add(this.txtFile);
                this.Controls.Add(this.lblFile);
                this.Controls.Add(this.lblDirectory);
                this.Controls.Add(this.lstFilesFound);
                this.Controls.Add(this.cboDirectory);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
                this.PerformLayout();

            
            #endregion

    /// <summary>
    /// The main entry point for the application
    /// </summary>
            [STAThread]
            static void Main()
            
                Application.Run(new Form1());
            

            private void btnSearch_Click(object sender, System.EventArgs e)
            
                lstFilesFound.Items.Clear();
                txtFile.Enabled = false;
                cboDirectory.Enabled = false;
                btnSearch.Text = "Searching...";
                this.Cursor = Cursors.WaitCursor;
                Application.DoEvents();
                DirSearch(cboDirectory.Text);
                btnSearch.Text = "Search";
                this.Cursor = Cursors.Default;
                txtFile.Enabled = true;
                cboDirectory.Enabled = true;
            

            private void Form1_Load(object sender, System.EventArgs e)
            
                cboDirectory.Items.Clear();
                foreach (string s in Directory.GetLogicalDrives())
                
                    cboDirectory.Items.Add(s);
                
                cboDirectory.Text = "C:\\";
            

            void DirSearch(string sDir)
            
                try
                
                    foreach (string d in Directory.GetDirectories(sDir))
                    
                        foreach (string f in Directory.GetFiles(d, txtFile.Text))
                        
                            lstFilesFound.Items.Add(f);
                        
                        DirSearch(d);
                    
                
                catch (System.Exception excpt)
                
                    Console.WriteLine(excpt.Message);
                
            
        
    

这里也是应用打开的截图:

Open Application Image

我要添加一个保存按钮,点击后会保存到特定位置,我该怎么做。

【问题讨论】:

【参考方案1】:

如果我理解正确,请使用简单的 I/O 操作。

   using(StreamWriter writer = new StreamWriter("debug.txt", true))
    
        foreach (string item in lstFilesFound.Items)
        
            writer.WriteLine(item.ToString());
        
    

一些额外的指针:

在 DirSearch 中,为 Directory.GetDirectories(sDir) 创建一个变量。您当前的代码导致这个东西在每个循环中都进行计算。在其他领域寻找一些更相似的重构代码。

var dirs = Directory.GetDirectories(sDir);
foreach (string d in dirs)

    var files = Directory.GetFiles(d, txtFile.Text);
    foreach (string f in files)
    
        lstFilesFound.Items.Add(f);
    
    DirSearch(d);

希望对你有帮助。

【讨论】:

我相信他的意思是 foreach 行(Directory.GetDirectories(sDir) 中的字符串 d)。如果这是正确的,我认为它没有区别,GetDirectories 不会为每个循环迭代执行 @BenSchwehn:我编辑了我的帖子以反映我所指的内容。我觉得它应该有所作为。请发表您的意见。谢谢 应该没什么区别。参见例如***.com/questions/3763933/… 太棒了...我在考虑for 循环,它应该有所作为(如果用于计算计数)。感谢您的澄清。很有帮助,肯定会在我的代码中节省几行......:) 好的,我明白你关于循环的意思,它有相同的文件显示超过 200 次,我将如何删除这个循环,我只想要每个文件中的一个供参考【参考方案2】:

我假设它是您想要保存到文本文件的已找到文件的列表,在您的保存事件中如何处理(粗略的代码,因此未经广泛测试)?

using (FileStream fs = new FileStream("c:\\files.txt", FileMode.Create, FileAccess.Write))

    using (StreamWriter sw = new StreamWriter(fs))
    
        foreach (string item in lstFilesFound.Items)
        
            sw.WriteLine(item);
        
    

【讨论】:

感谢您的快速回复,现在就去看看

以上是关于如何从此输出写入文本文件的主要内容,如果未能解决你的问题,请参考以下文章

如何将 WebSocket 输出写入文本文件?

如何将生成器的输出写入文本文件?

如何将 websocket 推送 api 输出写入文本文件?

如何将“读者友好”的 sessionInfo() 写入文本文件

PHP写入到文本文件乱码

使用 Apache Spark 将 RDD 写入文本文件