I'm working on a project for which I need to use a 7805 voltage regulator. I feed 9V to its input pin and power a 5V board with the output. However, when the board is connected to a computer, the board outputs 5V through the same cable: even if I turned off the 9V power supply before connecting the board to a computer, would the voltage regulator be hurt by the 5V input to its output pin? Can I fix this with a diode?
for which I need to use a 7805 voltage regulator
And right there is your mistake. No you don't. Unless you're doing a historical project on antique voltage regulators, what you actually need is a 5V regulator. You don't need a specific one, you need one which meets your technical requirements.. The 7805 is not it.
If you're connecting to a computer, chances are you're using digital logic. In that case a switch-mode regulator is usually the best choice. But let's assume there's a reason you want a linear regulator. Regulators such as the LM2940 have protection built in against the various ways you can damage it and are a far better choice.
Basically, instead of kludging around with a near-obsolete device, design your circuit with something that isn't 40 years old. It's not 1980 any more. We have alternatives.
If you don't short or heavily load the regulator input to ground the backfeeding is unlikely to damage the 7805.
If the +5 isn't too critical you could do something like this to prevent backfeeding:
R1 establishes a minimum output current of ~5mA to match Iq.