473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to show warning message when an already opened file ismodified in external editor

Hi All,

I have one text editor application which can open XML files for editing.

Now when a file is aleady opened, i have opened the same file in an external editor(notepad or wordpad) and saved it.

In this scenario, how to throw a warning message to the XML editor, notifying that file has been chaged in an external editor and how to accept or reject that change?

I am using C# .NET

Thanks,
-Anupam
Nov 12 '07 #1
11 2021
hi
by using following code help you to solve problem

Expand|Select|Wrap|Line Numbers
  1. string    stringFileName ;
  2. DateTime  openingtime;
  3.         private void trim_Load(object sender, System.EventArgs e)
  4.         {
  5.             if(openFileDialog1.ShowDialog() == DialogResult.OK)
  6.             {
  7.               openingtime =Convert.ToDateTime( DateTime.Now.ToString());
  8.                 stringFileName = openFileDialog1.FileName ;
  9.  
  10.  
  11.             }
  12.         }
  13.  
  14.         private void button2_Click(object sender, System.EventArgs e)
  15.         {
  16.             FileInfo objFileSize = new         FileInfo(stringFileName);
  17.             DateTime     dtCreateDate = objFileSize.CreationTime; 
  18.             //GetLastWriteTime(stringFileName);
  19.             DateTime dtModifyDate = objFileSize.LastWriteTime; 
  20.             if(openingtime < dtModifyDate)
  21.             {
  22.                 MessageBox.Show("file aready  modified");
  23.             }
  24.         }
Nov 14 '07 #2
hi
by using following code help you to solve problem

Expand|Select|Wrap|Line Numbers
  1. string    stringFileName ;
  2. DateTime  openingtime;
  3.         private void trim_Load(object sender, System.EventArgs e)
  4.         {
  5.             if(openFileDialog1.ShowDialog() == DialogResult.OK)
  6.             {
  7.               openingtime =Convert.ToDateTime( DateTime.Now.ToString());
  8.                 stringFileName = openFileDialog1.FileName ;
  9.  
  10.  
  11.             }
  12.         }
  13.  
  14.         private void button2_Click(object sender, System.EventArgs e)
  15.         {
  16.             FileInfo objFileSize = new         FileInfo(stringFileName);
  17.             DateTime     dtCreateDate = objFileSize.CreationTime; 
  18.             //GetLastWriteTime(stringFileName);
  19.             DateTime dtModifyDate = objFileSize.LastWriteTime; 
  20.             if(openingtime < dtModifyDate)
  21.             {
  22.                 MessageBox.Show("file aready  modified");
  23.             }
  24.         }


Hi hemant,

Thanks a lot for your reply...but i have some doubts which i want to clarify.

If i am not wrong you have created two event handler classes
"button2_Click" and "trim_Load" and implemented your logic inside it.

But i m confused how to register the event handler and how shall i pass the name of the file in a correct way.

i have one class in which i have called the ShowEditor() function which will open the editor.

I have the editorfile name inside the class and its full path as of now.So after ShowEditor() is called i want to invoke the Watcher and start looking for any change in the file in some external editor(process)

Now i want to implement your code.

Could you provide me the way how i can acheive this?

Thanks and Regards,
-Anupam
Nov 14 '07 #3
Plater
7,872 Expert 4TB
I think there is a FileSystemWatcher that can be used for this?

Expand|Select|Wrap|Line Numbers
  1. System.IO.FileSystemWatcher sw = new System.IO.FileSystemWatcher("c:\", "filename.txt");
  2. sw.Changed += new System.IO.FileSystemEventHandler(sw_Changed);
  3.  
Expand|Select|Wrap|Line Numbers
  1. void sw_Changed(object sender, System.IO.FileSystemEventArgs e)
  2. {//file has been changed
  3. }
  4.  
Nov 14 '07 #4
I think there is a FileSystemWatcher that can be used for this?

Expand|Select|Wrap|Line Numbers
  1. System.IO.FileSystemWatcher sw = new System.IO.FileSystemWatcher("c:\", "filename.txt");
  2. sw.Changed += new System.IO.FileSystemEventHandler(sw_Changed);
  3.  
Expand|Select|Wrap|Line Numbers
  1. void sw_Changed(object sender, System.IO.FileSystemEventArgs e)
  2. {//file has been changed
  3. }
  4.  
Hi plater,

Thanks for the reply!!
I have already implemented FileSystemWatcher class.

My code is somewhat like this..

MyWatcherClass(editorFile.FullPath.ToString());... .editorfile is the file to be watched

public static void MyWatcherClass(string File)
{

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path="C:\\abc\\xyz\\........";


/* Watch for changes for the specific file*/


watcher.Filter = "*.xml"; or even i can specify the file..


/* Watch for changes */
watcher.NotifyFilter = NotifyFilters.LastWrite;


// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);

//watcher.WaitForChanged;

watcher.EnableRaisingEvents = true;


}

private static void OnChanged(object source, FileSystemEventArgs e)
{
Type type=e.GetType();

WatcherChangeTypes type1=e.ChangeType;
// Specify what is done when a file is changed, created, or deleted.
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);


}


but the problem is as soon as the watcher.EnableRaisingEvents = true is set t starts watching the file and throws the warning irrespective from where the file is accesssed.

But my requirement is iff and if only the file is modified from some external process(like notepad,wordpad) the warning message should generate.

But in this scenario, this is not the case,.
Even when the file is modified from my customised editor( the xml editor which is invoking or opening the file) its thrwing the warning message!!

Since we are not able to filter the process from where the file is accessed!! this is not helping me...

Hope i could make my point clear!!

Thanks,
-ANupam
Nov 14 '07 #5
Plater
7,872 Expert 4TB
Don't have your editor edit the file itself, but an in-memory copy of it.
Then it won't tip off the watcher
Nov 14 '07 #6
Don't have your editor edit the file itself, but an in-memory copy of it.
Then it won't tip off the watcher
Hi Plater!

Could you please kindly elaborate your comment.

I am not very sure how you wanted me to implement.

Can you please give some reference?


How to do the editing in an in-memory copy?

Thanks and Regards,
-ANupam!!
Nov 14 '07 #7
Plater
7,872 Expert 4TB
The contents of the file to a string, or a byte[] or something.
And than manipulate that. The watcher won't be tipped off until you go to "save" the data back to the file
Nov 14 '07 #8
The contents of the file to a string, or a byte[] or something.
And than manipulate that. The watcher won't be tipped off until you go to "save" the data back to the file

I could certainly understand ur point but i have a doubt like i have to atlast save the contents back to the file while closing the file from my customised editor itself....

So in this scenario, watcher will again throw the warning...

How to get rid off this scenario is my doubt!

Thanks a lot for your inputs!!

Regards,
-ANupam
Nov 15 '07 #9
Plater
7,872 Expert 4TB
Well when you actually WANT to do the saving, change a boolean value that the watcher handler function can look and decided if it need to "alert" you to the change?
Nov 15 '07 #10
Well when you actually WANT to do the saving, change a boolean value that the watcher handler function can look and decided if it need to "alert" you to the change?
Hi Plater,

I have declared a public static bool variable, which will decide whether the event handler function to show the warning message or not.

just before saving the file i am setting that variable to false so that it warning message is not thrown,but after the saving is done i am again setting this value to true.

Immeadiately after this the event handler function is throwing the warning message since file has been changed.

Is there any way i can stop the watcher?

By setting the value
FileSystemWatcher.EnableRaisingEvents =false will stop the watcher or the thread will keep running?

My intention is to stop the watcher when i am finally writing the file contents back and once it is done i should start the Watcher.

How to control this behaviour?

Regards,
-Anupam
Nov 20 '07 #11
Plater
7,872 Expert 4TB
setting enableraising events to false will just "pause" it i think.
You want the event to fire when you save it, you just want to ignore the event.

Or did it fire twice?
Nov 20 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Carl Owenby | last post by:
Re: Microsoft Knowledge Base Article - 174942 "File Modified Outside Source Editor" Warning Message I believe I have discovered a cause of this warning in a specific situation. I encountered...
0
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
6
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
3
by: pemigh | last post by:
A while back I imported tables to a new database via Files-->Get External Data --> Import... All was well for several months, and then the database started behaving badly in a couple of ways,...
19
by: lawrence k | last post by:
How can I find out where my script is outputting to the screen for the first time? My error logs are full of stuff like this: PHP Warning: session_start(): Cannot send session cache...
0
by: Manish | last post by:
PHP INI File Setting ------------------------------------------------------------------------------------------------------------------ error_reporting = E_ALL & ~E_NOTICE No warning message are...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
1
by: dewi | last post by:
Dear All, I am trying to compile a C code using Visual C++. Can anyone explain how to solve it? Thank You. #include <math.h> #include <string.h> #include "RV2AJFRONT_NEW.h" #include...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.