Hartmann was born the son of Georg Hartmann and Barbara Geither. His maternal uncle was the German-born French general Jean-Michel Geither. Hartmann entered the French army in October 1804 as an infantryman in the 15th Light Infantry Regiment. Two years later he was transferred to the Infantry regiment of the Grand Duchy of Berg. After graduating from Saint-Cyr in July 1811 he was named a second lieutenant in the regiment. By December he had been promoted to first lieutenant. He served in the campaigns of 1814 and 1815 in the 27th Infantry Regiment, during which time he was awarded the Legion d’Honneur. In February 1816 he asked for and was released from the French service.
Bavarian service
In July 1816 Hartmann entered the Bavarian 10th Regiment of the Line as a first lieutenant. In 1818 he was posted to the Topgraphical Bureau. In 1822 he was given command of a company of pioneers. In 1827 he was given a position in the Bavarian Ministry of War. Hartmann was promoted to captain in 1829 and to major in 1838. In October 1842 Hartmann was named an aide-de-camp to the Crown Prince of Bavaria. In December 1843 he was ennobled a knight in the Bavarian nobility, entitling him to the use of “von” in his name. In 1844 Hartmann was promoted to lieutenant colonel. After the Crown Prince became King Maximilian II in 1848 he was named a personal aide-de-camp to the King and promoted to colonel. In June 1849 he was promoted to major general and given command of a brigade in the 2nd Royal Bavarian Division. In 1852 he once more became a personal aide-de-camp to the King. In 1855 he once more commanded a brigade in the 2nd Division. Hartmann was promoted to lieutenant-general in 1861 and given command of the garrison at Würzburg. During the Austro-Prussian War Bavaria joined the Austrian side and Hartmann commanded the 4th Royal Bavarian Division in engagements against the Prussians at Rossdorf and Hettstadt. After the defeat of Austria and its allies, Hartmann was retained in his command. In January 1869 Hartmann was promoted to General der Infanterie. When the Franco-Prussian War broke out, Bavaria fought on the side of its erstwhile enemy Prussia. Hartmann was given command of the II. Bavarian Army Corps, which was together with its sister Corps - I. Bavarian Army Corps under von der Tann - part of the III Army, led by the Crown Prince of Prussia. Hartmann lead the II Corps in the battles of Wissembourg, Wörth and Sedan. When the German armies neared Paris he fought in engagements at Corbeil, Bourg-la-Reine, Petit-Bicètre and Moulin de la Tour. On 19 September 1870 Hartmann defeated General Ducrot in an engagement at Sceaux. In July 1871 he was further ennobled a Freiherr. After the end of the war he kept command of his corps until his death.