Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does MS Visual Studio determine that source file have changed?

Does it use modification timestamp or/and does it check whether the actual content has changed (e.g. by comparing the checksum)?

Edit: I need to know this since I use Git for source control and often change branches. It appears that sometimes even if I change the branch back and force (e.g. from develop to master and then back to develop), the VS rebuilds half of the sources files. I wonder why this happens and why does it happen sometimes and does not happen the other times.

like image 929
Sergiy Belozorov Avatar asked Feb 09 '12 21:02

Sergiy Belozorov


People also ask

How do I know if Visual Studio modified a file?

Visual Studio maintains a history of file changes. Modified files are marked with a red check mark in the Solution Explorer. You can compare your changes with the original file by right-clicking on the file name and selecting Compare with Unmodified. The Diff window shows new code in green and removed code in red.

How do I find the local file history in Visual Studio code?

Visual Studio Code allows us to check the history of navigated files in Navigation History lists. You can open this window from “Goto–> Navigation History” or by just simply pressing Ctrl + Tab. This will bring list of all previously navigated files with in Visual Studio Code.


Video Answer


2 Answers

Since Visual Studio is a closed-source project, I bet only developers would be able to give a definite answer on how exactly does it work. However, for my purposes it is enough to test some scenarios.

I have tested it with a small solution and a couple of files in it (one header and two source files). Test results bring to the following conclusion. Visual Studio looks for modification date and time. Even if the file content is the same - it compiles this file and also any other files that include it. If the modification date and time are the same - it won't recompile it even if the content is different. Visual Studio ignores creation and access dates and times.

like image 153
Sergiy Belozorov Avatar answered Oct 01 '22 01:10

Sergiy Belozorov


I'm guessing it uses FileSystemWatcher on the project directories and linked files (if any), just because it's the right way to do this kind of thing.

Some googling finds for more about this class (or just look it up yourself):

  • http://www.techrepublic.com/article/use-the-net-filesystemwatcher-object-to-monitor-directory-changes-in-c/6165137
  • http://www.dotnetperls.com/filesystemwatcher

Of course when the source file is open, it's content by the time of editing, as wel as any user changes (even not saved) are loaded in the RAM, but it doesn't compare it to disk content (that'd be too slow), it listens to a system event when the system tells it the file changed.

Update:

Probably not that class itslf, but the Win32 version of it, you know most of the system related .NET functionality classes are just Win32 wrappers.

From this StackOverflow answer: How does FileSystemWatcher work on another computers directory?
I think it wraps this API (not sure): http://msdn.microsoft.com/en-us/library/aa365465.aspx

Update 2:

This is Microsoft's approach to monitor file changes:
http://msdn.microsoft.com/en-us/library/chzww271(v=vs.80).aspx

Update 3

This is an old answer, and it was mentioned above that it was a guess, as Visual Studio is closed source as mentioned in other answers. It's worth mentioning that the accepted answer suggests Visual Studio looks for file modification dates instead, which suggests it doesn't use the approach guessed in this very answer, and that it was wrong.

I hope the reader didn't mind the effort given to rationalize possibilities in this answer (causing reader discomfort or down votes). Keeping it for archival reason only.

like image 34
Meligy Avatar answered Oct 01 '22 03:10

Meligy