Skip to content

Commit

Permalink
add file monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
sunminghong committed Apr 22, 2011
1 parent 80ffbea commit 7446734
Show file tree
Hide file tree
Showing 26 changed files with 444 additions and 132 deletions.
76 changes: 76 additions & 0 deletions FileMonitor.cs
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;


namespace MEditor
{

public class FileMonitor
{
private int TimeoutMillis = 2000;

System.IO.FileSystemWatcher fsw = new System.IO.FileSystemWatcher();
System.Threading.Timer m_timer = null;
List<String> files = new List<string>();
FileSystemEventHandler fswHandler = null;

public FileMonitor(FileSystemEventHandler watchHandler)
{
m_timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, Timeout.Infinite, Timeout.Infinite);
fswHandler = watchHandler;

}


public FileMonitor(FileSystemEventHandler watchHandler, int timerInterval)
{
m_timer = new System.Threading.Timer(new TimerCallback(OnTimer), null, Timeout.Infinite, Timeout.Infinite);
TimeoutMillis = timerInterval;
fswHandler = watchHandler;

}

public void Add(string dir,string filter)
{
System.IO.FileSystemWatcher fsw = new System.IO.FileSystemWatcher(dir, filter);
fsw.NotifyFilter = System.IO.NotifyFilters.LastWrite;
fsw.Changed += new FileSystemEventHandler(OnFileChanged);
fsw.EnableRaisingEvents = true;
}

public void OnFileChanged(object sender, FileSystemEventArgs e)
{
//MessageBox.Show("Created", "Create triggered");
Mutex mutex = new Mutex(false, "FSW");
mutex.WaitOne();
if (!files.Contains(e.FullPath))
{
files.Add(e.FullPath);
}
mutex.ReleaseMutex();

m_timer.Change(TimeoutMillis, Timeout.Infinite);
}

private void OnTimer(object state)
{
List<String> backup = new List<string>();

Mutex mutex = new Mutex(false, "FSW");
mutex.WaitOne();
backup.AddRange(files);
files.Clear();
mutex.ReleaseMutex();


foreach (string file in backup)
{
fswHandler(this, new FileSystemEventArgs(WatcherChangeTypes.Changed, string.Empty, file));
}

}
}
}
1 change: 1 addition & 0 deletions MDEditor.csproj
Expand Up @@ -42,6 +42,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileMonitor.cs" />
<Compile Include="frmCss.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
Binary file modified MEditor.suo
Binary file not shown.
56 changes: 53 additions & 3 deletions MarkdownEditorManager.cs
Expand Up @@ -21,6 +21,7 @@ public class MarkdownEditorManager
private int noName = 0;

SortedList<int, MarkdownEditor> _editors = new SortedList<int, MarkdownEditor>();
private SortedList<string, bool> _thisModify = new SortedList<string, bool>();

private string _workSpace;
//private int maxDisplayLength = 10;
Expand Down Expand Up @@ -254,15 +255,52 @@ public bool Open(string file)
else
{
foreach (MarkdownEditor me in _editors.Values)
{
{
if (me.FileName == file)
{
_tabparent.SelectedTab = me.MarkdownPage;
_tabparent.SelectedTab = me.MarkdownPage;
return true;
}
}
}

return openfile_(file);
}

public bool RefrushOpen(string file)
{
if (_thisModify.ContainsKey(file))
{
_thisModify.Remove(file);
return false;
}
foreach (MarkdownEditor me in _editors.Values)
{
if (me.FileName == file)
{
string noti = "";
if (me.AlreadyUpdate) noti = "(重新打开,当前修改将丢失)";

if (MessageBox.Show(file + "在被其他软件已经修改,你需要重新打开吗"+noti+"", "警告", MessageBoxButtons.YesNo) == DialogResult.No)
return false;

bool rel = me.Openfile(file);
if (rel)
{
_tabparent.SelectedTab = me.MarkdownPage;
}

return rel;

}
}

return false;
}


private bool openfile_(string file)
{
TabPage markPage = new TabPage(GetDisplayName(file));
markPage.ToolTipText = file;
_tabparent.TabPages.Add(markPage);
Expand All @@ -271,7 +309,7 @@ public bool Open(string file)
MarkdownEditor meditor = new MarkdownEditor(_thisForm, markPage);
_editors.Add(_fileInd, meditor);
_fileInd++;
meditor.SetStyle(_bgColor, _foreColor, _font, _wordWrap,_tabWidth);
meditor.SetStyle(_bgColor, _foreColor, _font, _wordWrap, _tabWidth);
bool rel = meditor.Openfile(file);
if (rel)
{
Expand Down Expand Up @@ -340,6 +378,10 @@ private bool save(MarkdownEditor meditor)

}
meditor.Save();
if (!_thisModify.ContainsKey(meditor.FileName))
{
_thisModify.Add(meditor.FileName, true);
}
mruManager.Add(meditor.FileName); // when file is successfully opened
return true;
}
Expand All @@ -360,8 +402,16 @@ private bool saveas(MarkdownEditor meditor)
{
try
{
if (_thisModify.ContainsKey(meditor.FileName))
{
_thisModify.Remove(meditor.FileName);
}
meditor.Save(fileone.FileName);
mruManager.Add(meditor.FileName); // when file is successfully opened
if (!_thisModify.ContainsKey(meditor.FileName))
{
_thisModify.Add(meditor.FileName, true);
}
return true;
}
catch (ArgumentException)
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Expand Up @@ -29,5 +29,5 @@
// 内部版本号
// 修订号
//
[assembly: AssemblyVersion("0.9.0.0")]
[assembly: AssemblyFileVersion("0.9.0.0")]
[assembly: AssemblyVersion("0.9.1.0")]
[assembly: AssemblyFileVersion("0.9.1.0")]
12 changes: 12 additions & 0 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Properties/Settings.settings
Expand Up @@ -225,9 +225,15 @@ th,td{padding:5px;border: 1px solid #CCC;}</Value>
&lt;setting name="css" serializeAs="String"&gt;
&lt;value&gt;{css}&lt;/value&gt;
&lt;/setting&gt;
&lt;setting name="extfile" serializeAs="String"&gt;
&lt;value&gt;{extfile}&lt;/value&gt;
&lt;/setting&gt;
&lt;/MEditor.Properties.Settings&gt;
&lt;/applicationSettings&gt;
&lt;/configuration&gt;</Value>
</Setting>
<Setting Name="extfile" Type="System.String" Scope="Application">
<Value Profile="(Default)">.txt|.js|.htm|.xml|.as|.log|.php|.cs</Value>
</Setting>
</Settings>
</SettingsFile>
6 changes: 6 additions & 0 deletions app.config
Expand Up @@ -211,6 +211,9 @@ border-collapse: separate;border-spacing: 0;
}
th,td{padding:5px;border: 1px solid #CCC;}</value>
</setting>
<setting name="extfile" serializeAs="String">
<value>.txt|.js|.htm|.xml|.as|.log|.php|.cs</value>
</setting>
</MEditor.Properties.Settings>
</applicationSettings>
<userSettings>
Expand All @@ -237,6 +240,9 @@ th,td{padding:5px;border: 1px solid #CCC;}</value>
&lt;setting name="css" serializeAs="String"&gt;
&lt;value&gt;{css}&lt;/value&gt;
&lt;/setting&gt;
&lt;setting name="extfile" serializeAs="String"&gt;
&lt;value&gt;{extfile}&lt;/value&gt;
&lt;/setting&gt;
&lt;/MEditor.Properties.Settings&gt;
&lt;/applicationSettings&gt;
&lt;/configuration&gt;</value>
Expand Down
Binary file modified exeoutput/MDEditor.exe
Binary file not shown.
42 changes: 6 additions & 36 deletions exeoutput/MDEditor.exe.config
Expand Up @@ -4,20 +4,17 @@
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MEditor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MEditor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<MEditor.Properties.Settings>
<setting name="font" serializeAs="String">
<value>微软雅黑, 10.5pt</value>
<value>[Font: Name=微软雅黑, Size=10.5, Units=3, GdiCharSet=1, GdiVerticalFont=False]</value>
</setting>
<setting name="color" serializeAs="String">
<value>White</value>
<value>255,255,255</value>
</setting>
<setting name="bgcolor" serializeAs="String">
<value>74, 82, 90</value>
<value>74,82,90</value>
</setting>
<setting name="css" serializeAs="String">
<value>body,td,th {font-family:"微软雅黑", Verdana, "Bitstream Vera Sans", sans-serif; }
Expand Down Expand Up @@ -211,36 +208,9 @@ border-collapse: separate;border-spacing: 0;
}
th,td{padding:5px;border: 1px solid #CCC;}</value>
</setting>
</MEditor.Properties.Settings>
</applicationSettings>
<userSettings>
<MEditor.Properties.Settings>
<setting name="appconfig" serializeAs="String">
<value>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
&lt;configSections&gt;
&lt;sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" &gt;
&lt;section name="MEditor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
&lt;/sectionGroup&gt;
&lt;/configSections&gt;
&lt;applicationSettings&gt;
&lt;MEditor.Properties.Settings&gt;
&lt;setting name="font" serializeAs="String"&gt;
&lt;value&gt;{font}&lt;/value&gt;
&lt;/setting&gt;
&lt;setting name="color" serializeAs="String"&gt;
&lt;value&gt;{color}&lt;/value&gt;
&lt;/setting&gt;
&lt;setting name="bgcolor" serializeAs="String"&gt;
&lt;value&gt;{bgcolor}&lt;/value&gt;
&lt;/setting&gt;
&lt;setting name="css" serializeAs="String"&gt;
&lt;value&gt;{css}&lt;/value&gt;
&lt;/setting&gt;
&lt;/MEditor.Properties.Settings&gt;
&lt;/applicationSettings&gt;
&lt;/configuration&gt;</value>
<setting name="extfile" serializeAs="String">
<value>.txt|.jds|.htm|.xml|.as|.log|.php|.cs</value>
</setting>
</MEditor.Properties.Settings>
</userSettings>
</applicationSettings>
</configuration>
Binary file modified exeoutput/MDEditor.pdb
Binary file not shown.
42 changes: 6 additions & 36 deletions exeoutput/MDEditor.vshost.exe.config
Expand Up @@ -4,20 +4,17 @@
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MEditor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MEditor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<MEditor.Properties.Settings>
<setting name="font" serializeAs="String">
<value>微软雅黑, 10.5pt</value>
<value>[Font: Name=微软雅黑, Size=10.5, Units=3, GdiCharSet=1, GdiVerticalFont=False]</value>
</setting>
<setting name="color" serializeAs="String">
<value>White</value>
<value>255,255,255</value>
</setting>
<setting name="bgcolor" serializeAs="String">
<value>74, 82, 90</value>
<value>74,82,90</value>
</setting>
<setting name="css" serializeAs="String">
<value>body,td,th {font-family:"微软雅黑", Verdana, "Bitstream Vera Sans", sans-serif; }
Expand Down Expand Up @@ -211,36 +208,9 @@ border-collapse: separate;border-spacing: 0;
}
th,td{padding:5px;border: 1px solid #CCC;}</value>
</setting>
</MEditor.Properties.Settings>
</applicationSettings>
<userSettings>
<MEditor.Properties.Settings>
<setting name="appconfig" serializeAs="String">
<value>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
&lt;configSections&gt;
&lt;sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" &gt;
&lt;section name="MEditor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
&lt;/sectionGroup&gt;
&lt;/configSections&gt;
&lt;applicationSettings&gt;
&lt;MEditor.Properties.Settings&gt;
&lt;setting name="font" serializeAs="String"&gt;
&lt;value&gt;{font}&lt;/value&gt;
&lt;/setting&gt;
&lt;setting name="color" serializeAs="String"&gt;
&lt;value&gt;{color}&lt;/value&gt;
&lt;/setting&gt;
&lt;setting name="bgcolor" serializeAs="String"&gt;
&lt;value&gt;{bgcolor}&lt;/value&gt;
&lt;/setting&gt;
&lt;setting name="css" serializeAs="String"&gt;
&lt;value&gt;{css}&lt;/value&gt;
&lt;/setting&gt;
&lt;/MEditor.Properties.Settings&gt;
&lt;/applicationSettings&gt;
&lt;/configuration&gt;</value>
<setting name="extfile" serializeAs="String">
<value>.txt|.jds|.htm|.xml|.as|.log|.php|.cs</value>
</setting>
</MEditor.Properties.Settings>
</userSettings>
</applicationSettings>
</configuration>

0 comments on commit 7446734

Please sign in to comment.