Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for Open Text File in Notepad, and Closing it

Status
Not open for further replies.

BDRichardson

Programmer
Jul 23, 2003
46
GB
I wonder if anyone might be able to resolve the following issue:

I have a text file, which I write to using a StreamWriter. I want to be able to append to the file, but beforehand I must ensure that the file is closed.

I have tried using System.Threading.Mutex(), System.Diagnostics.Process.GetCurrentProcess(), and System.Diagnostics.Process.GetProcessesByName(). But with no success.

I think my problem mainly exists with either detecting whether a file is open (i.e. file locked), or the specific file is open, as opposed to the application (Notepad) which it runs in.

Any assistance would be greatly appreciated.

Thanks [morning]
 
I am not sure if I understood the problem.
First of all, the notepad takes only a copy of the file and keep it in memory until Save or Save as is performed. While notepad is running that file could be modified by other processes or deleted without having any notification send to notepad.
To append to an existing file you should do the followings:
Try to Open() the FileStream using FileMode.Append, FileAccess.Write , FileShare.None
If the Open () succeeds then use Write() to append records and close the file when no more records to append.
If the Open() fails, that means you have no rights to write –append to that file for many reasons: you have to privileges to access it or it is locked by another process.
-obislavu-
 
I think you have grasped my problem, and you have made me realise what my real problem is:

Notepad doesn't open the file, and create a file lock, it simply creates a copy of the file in memory.

Put simply, I have been trying to generate a log file on a server, and I wanted the document to be displayed to the user whenever an important entry is made, i.e. capturing the user's attention. The only problem, I have is, that instead of only having one copy of the file open at any time, I could potentially have lots of re-opened, up until the server runs out of memory and crashes.

I could use a Dialog Box or Message Box, but this too would have the same outcome.

My desired outcome, is to have one log file, which is opened for the user's attention whenever an important entry is added to the file. But I don't want a fresh copy of the file to be opened every time an entry is added.
 
Okay, it is not hard to do:
1. Have the log file created and open with Share.Read in order to allow other processes to read the file while these processes still to write there.
2. Implement a thread that will watch the that log file for updates. Use for that FileSystemWatcher class.

Code:
        FileSystemWatcher watcherlog = new FileSystemWatcher();
        // Watch for changes 
        watcher.NotifyFilter = NotifyFilters.LastWrite ;
        // File to watch.
        watcherlog.Path = "C:\\myapp\\log";

        watcher.Filter = "myfile.log";

        // Add event handlers.
        watcherlog.Changed += new FileSystemEventHandler(OnChanged);
        // Start watching.
        watcher.EnableRaisingEvents = true;

    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when the log ile is changed.
        // Open log file for READ ONLY  e.g. e.FullPath
        // Scan log file for error messages
        // Prepare e-mail notification 
        // Send mail to the designed recipients
    }
-obislavu-
 
Okay, it is not hard to do:
1. Have the log file created and open with Share.Read in order to allow other processes to read the file while these processes still to write there.

2. Implement a thread that will watch the that log file for updates. Use for that FileSystemWatcher class.

Code:
        FileSystemWatcher watcherlog = new FileSystemWatcher();
        // Watch for changes 
        watcher.NotifyFilter = NotifyFilters.LastWrite ;
        // File to watch.
        watcherlog.Path = "C:\\myapp\\log";

        watcher.Filter = "myfile.log";

        // Add event handlers.
        watcherlog.Changed += new FileSystemEventHandler(OnChanged);
        // Start watching.
        watcher.EnableRaisingEvents = true;

    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when the log ile is changed.
        // Open log file for READ ONLY  e.g. e.FullPath
        // Scan log file for error messages
        // Prepare e-mail notification 
        // Send mail to the designed recipients
    }
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top