We propose an all-optical scheme to prolong the quantum coherence of a negatively charged nitrogen-vacancy (NV) center in diamond at cryogenic temperatures. Optical control of the NV spin suppresses energy fluctuations of the
ground states and forms an energy gap protected subspace. By optical control, the spectral linewidth of magnetic resonance is much narrower and the measurement of the frequencies of magnetic field sources has higher resolution. The optical control also improves the sensitivity of the magnetic field detection and can provide measurement of the directions of signal sources.