Correct option is B
Explanation
The sentence expresses an unreal/imaginary situation in the past. The speaker did not know that the person was in town, so the visit did not happen.
This is the Third Conditional (also called the Type 3 Conditional), which is used for hypothetical situations in the past.
Structure
If + past perfect, would have + past participle
Here:
If I had known → Past Perfect
I would have visited → would have + past participle
Therefore:
If I had known you were in town, I would have visited you.
Meaning
The sentence means:
“I did not know you were in town, so I did not visit you.”