Click here to Skip to main content
15,920,801 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
So here the solution consists of a Console Application and a Windows Form.

I have used multiple startup options to run them simultaneously. But the console app doesn't work as it was running as a single unit.

What I have tried:

This is the code

using System;
using System.IO;
using System.Security.Permissions;

public class Watcher
{

    public static void Main()
    {
    Run();

    }

    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    public static void Run()
    {
        string[] args = System.Environment.GetCommandLineArgs();

        // If a directory is not specified, exit program.
        if(args.Length != 2)
        {
            // Display the proper way to call the program.
            Console.WriteLine("Usage: Watcher.exe (directory)");
            return;
        }

        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = args[1];
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        // Only watch text files.
        watcher.Filter = "*.txt";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;

        // Wait for the user to quit the program.
        Console.WriteLine("Press \'q\' to quit the sample.");
        while(Console.Read()!='q');
    }

    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
       Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        // Specify what is done when a file is renamed.
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }
}
Posted
Updated 4-Apr-17 7:26am
v3
Comments
[no name] 4-Apr-17 12:06pm    
You set your solution to have multiple startup projects. But, course, it doesn't make sense in the real world unless you simply have your console application start your windows application.
[no name] 4-Apr-17 12:22pm    
Maybe I don't understand your request correctly, but there is no restriction to show also a WindowsForm from an Console App.

I googled "visual studio multiple startup projects" for you and here is the first result

How to: Set Multiple Startup Projects[^]
 
Share this answer
 
Comments
kuharan 4-Apr-17 12:27pm    
I fixed that. Thanks . And How do I exchange data among them.
kuharan 4-Apr-17 12:35pm    
Somehow the console application is just showing up at startup but its not working.
See answer #3 to this[^] SO question.

/ravi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900