Créer fichier txt qui contient ce que la console écrit

octola - Modifié le 3 juil. 2020 à 20:27
Whismeril Messages postés 18790 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 décembre 2023 - 3 juil. 2020 à 19:06
Bonjour,

J'ai un programme qui affiche les dossiers et sous-dossiers contenus dans une boîte mail Outlook. J'aimerais créer un fichier txt qui contient tout ce qui est écrit dans la console. Que dois-je rajouter à mon code ?

Le voici :

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Text;

namespace OutlookAddIn1
{
    class Program
    {
        static void Main(string[] args)
        {
            Sample sample = new Sample();
            sample.EnumerateFoldersInDefaultStore();

            Console.WriteLine();
            Console.WriteLine("Fin du programme. Taper n'importe quelle touche pour quitter...");
            Console.ReadKey();

        }
    }

    class Sample
    {
        Outlook.Application GetApplicationObject()
        {

            Outlook.Application application = null;

            // Check whether there is an Outlook process running.
            if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
            {

                // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
            }
            else
            {

                // If not, create a new instance of Outlook and sign in to the default profile.
                application = new Outlook.Application();
                Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
                nameSpace.Logon("", "", Missing.Value, Missing.Value);
                nameSpace = null;
            }

            // Return the Outlook Application object.
            return application;
        }
        public void EnumerateFoldersInDefaultStore()
        {
            Outlook.Application application = GetApplicationObject();

            Outlook.Folder root =
                application.Session.
                DefaultStore.GetRootFolder() as Outlook.Folder;
            EnumerateFolders(root);
        }

        // Uses recursion to enumerate Outlook subfolders.
        private void EnumerateFolders(Outlook.Folder folder)
        {
            Outlook.Folders childFolders =
                folder.Folders;
            if (childFolders.Count > 0)
            {
                foreach (Outlook.Folder childFolder in childFolders)
                {
                    // Write the folder path.
                    Debug.WriteLine(childFolder.FolderPath);
                    Console.WriteLine(childFolder.FolderPath);
                    // Call EnumerateFolders using childFolder.
                    EnumerateFolders(childFolder);
                }
            }
        }
        

    }
}

1 réponse

Whismeril Messages postés 18790 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 1 décembre 2023 645
3 juil. 2020 à 19:06
Bonsoir

Regarde l’aide en ligne de la classe File.
0
Rejoignez-nous