I have the following scenario: My solution in VS 2017 includes some python files which other and I often edit. How can I achieve that every time I save one of these open .py files, some code or .bat file (or whatever is most convenient in your opinion) that should copy them from X to Y gets executed? For example, files a.py and b.py are open in my solution among others, and I just edited some of them and clicked "Save" or "Save all", so if I clicked "Save" I want only the current document to be copied - if it's a .py - from path X to path Y, and if I clicked "Save all" I want all the .py documents to be copied.
I came across this question: Run script when finished saving file - Visual Studio Extensibility
that introduced me to DTE which allows me to subscribe to file-save-events, so I suppose I could right some code that uses it, but I am unclear as to how this code would run automatically upon the file saves.
You create a windows service and make use of the FileSystemWatcher
class but i will rather give u a simpler solution because i cannot really write how to make a windows service as it may be huge :(
Anyways, here's an ugly solution ;)
1 . Create a WinForm's app
2 . Create a timer in it
3 . Use FileSystemWatcher
in the timer's tick event
4 . Fire the timer on Form_Load
and hide the form :)
5 . Add your app to the startup
Example :
public class form1
{
Forms.Timer tmr = new Forms.Timer
private void form_Load()
{
tmr.Tick += tmr_Tick();
tmr.Start
me.Hide();
}
private void tmr_Tick()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
//Copies file to another directory.
}