locked
write to table RRS feed

  • Question

  • User181930479 posted

    i have a folder that has multiple subfolders , and inside each subfolders there are files .

    I am able to get the folder name and then get the files inside of it , 

    what i need is to write to a textfile , the below :

    FolderName

    file 1

    file2

    file3

    next folder name

    file4

    fle5

    file6

    below is my code so far :

       string[] directories = Directory.GetDirectories(@"D:\smart");
    
    
                foreach (string dd in directories) {
    
    
                    MessageBox.Show(dd);
    
                    string[] files = Directory.GetFiles(dd, "*.*", SearchOption.TopDirectoryOnly);
    
                    foreach (string f in files) {
    
                        string ora = Path.GetFileName(f);
                        MessageBox.Show(ora);
    
                    }
    
    
                }

    any help ?

    Wednesday, April 4, 2018 6:29 AM

All replies

  • User3690988 posted

    How about something like this?  This code reads a Textbox for the starting directory and after hitting a button, writes directories and filenames to a Textbox.  This code was converted from VB and is untested.  Hopefully, it will give you a good start.

    private void btnGetFiles_Click(object sender, EventArgs e)
    {
        txtOut.Text = txtDir.Text + Environment.NewLine;
        findFiles(txtDir.Text);
        findDirs(txtDir.Text);
    }
    
    private void findDirs(string sDir)
    {
        string Dir;
        try
        {
            foreach (var Dir in Directory.GetDirectories(sDir))
            {
                txtOut.Text += Dir + Environment.NewLine;
                findFiles(Dir);
                findDirs(Dir);
            }
        }
        catch (System.Exception ex)
        {
            txtOut.Text += Environment.NewLine + ex.Message + Environment.NewLine;
        }
    }
    
    private void findFiles(string Dir)
    {
        string fyle;
        FileInfo fyleInfo;
        foreach (var fyle in Directory.GetFiles(Dir))
        {
            fyleInfo = new FileInfo(fyle);
            txtOut.Text += "   " + fyle + Environment.NewLine;
        }
    }

    Wednesday, April 4, 2018 11:09 AM
  • User2103319870 posted

    NAF

    what i need is to write to a textfile

    You can try with below code

    string[] directories = Directory.GetDirectories(@"D:\smart");
                foreach (string dd in directories)
                {
                    //Write File and Folder names to .txt document
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\smart\YourFile.txt", true))
                    {
                        //Write the folder name
                        file.WriteLine(Path.GetFileName(dd));
                        //MessageBox.Show(dd);
                        //Find all files
                        string[] files = Directory.GetFiles(dd, "*.*", SearchOption.TopDirectoryOnly);
                        //Loop through each files
                        foreach (string f in files)
                        {
                            //write file name to txt file
                            file.WriteLine(Path.GetFileName(f));
                            //MessageBox.Show(ora);
                        }
                    }
                }

    Wednesday, April 4, 2018 5:58 PM
  • User61956409 posted

    Hi NAF,

    The following sample code could be used to find the folders and files from specified directory and write the content to a file, please refer to it.

    string output = "";
    string[] dirs = Directory.GetDirectories(@"D:\smart");
    foreach (var dir in dirs)
    {
        string dname = Path.GetFileName(dir);
        output += dname + Environment.NewLine;
    
        string[] files = Directory.GetFiles(dir);
    
        foreach (var file in files)
        {
            string fname = Path.GetFileNameWithoutExtension(file);
            output += fname + Environment.NewLine;
        }
    }
    
    string path = Server.MapPath("~/files/test.txt");
    using (StreamWriter writer = new StreamWriter(path, true))
    {
        writer.WriteLine(output);
        writer.Close();
    }
    

    Note: I'm using above code in web application, which works for me.

    With Regards,

    Fei Han

    Thursday, April 5, 2018 8:30 AM
  • User-166373564 posted

    Hi NAF,

    what i need is to write to a textfile ,

    From your description,  I suggest you could use refer to the following code to write a text file:

        static void Main()
        {
    
            // These examples assume a "C:\Users\Public\TestFolder" folder on your machine.
            // You can modify the path if necessary.
            
    
            // Example #1: Write an array of strings to a file.
            // Create a string array that consists of three lines.
            string[] lines = { "First line", "Second line", "Third line" };
            // WriteAllLines creates a file, writes a collection of strings to the file,
            // and then closes the file.  You do NOT need to call Flush() or Close().
            System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
    
    
            // Example #2: Write one string to a text file.
            string text = "A class is the most powerful data type in C#. Like a structure, " +
                           "a class defines the data and behavior of the data type. ";
            // WriteAllText creates a file, writes the specified string to the file,
            // and then closes the file.    You do NOT need to call Flush() or Close().
            System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
    
            // Example #3: Write only some strings in an array to a file.
            // The using statement automatically flushes AND CLOSES the stream and calls 
            // IDisposable.Dispose on the stream object.
            // NOTE: do not use FileStream for text files because it writes bytes, but StreamWriter
            // encodes the output as text.
            using (System.IO.StreamWriter file = 
                new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
            {
                foreach (string line in lines)
                {
                    // If the line doesn't contain the word 'Second', write the line to the file.
                    if (!line.Contains("Second"))
                    {
                        file.WriteLine(line);
                    }
                }
            }
    
            // Example #4: Append new text to an existing file.
            // The using statement automatically flushes AND CLOSES the stream and calls 
            // IDisposable.Dispose on the stream object.
            using (System.IO.StreamWriter file = 
                new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
            {
                file.WriteLine("Fourth line");
            }
        }

    More details, see: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file

    write to table

    Do you mean you also want to isnert the file parth into sql server datatable? If that is the case, please refer to the following articles:

    https://msdn.microsoft.com/en-us/library/jj943772.aspx?f=255&MSPPError=-2147217396

    https://www.c-sharpcorner.com/blogs/crud-operation-in-c-sharp-application

    Best regards,
    Angie

    Thursday, April 5, 2018 8:37 AM