There's a fundamental assumption error here.
High level D&D parties, especially ones with access to 9th level spells don't need to ever enter dungeons unless it is for their own amusement.
We begin by articulating strategies of 18th level casters faced with an imposing dungeon:
Their central strategy is to force the "defenders" to emerge from behind their protections, face a battery of save-or-die spells... and then die. If the defenders refuse to respond to the threat, it leaves the protagonists able to choose their attacking strategy.
- Casters begin by narrowing their area of interest.
- Discern Location finds the targets. The only way to defeat discern location is with mind blank, a spell not available until at least 15th level.
- If necessary, the party can burn a wish or miracle on this, as they're at their end-of-campaign "not expecting to level" stage. Wish or mircale will burn through any scrying protection.
- This provides a teleport target to the attacking party. Any competent dungeon will be completely lined with forbiddance (at absolutely non-trivial expense) to avoid teleportation in.
Players figure out best route to area of interest.
- If there are any teleport-weak spots within the area, they will be found.
- Areas will be scouted with prying eyes, greater until vulnerabilities are found. If an area is completely impregnable to prying eyes due to screen (which may or may not work, depending if screen counters true seeing or vice versa) players will note that section of the Maginot Line and simply avoid it.
- Assuming teleportation is blocked at great expense, ethereal is blocked (which is why forbiddance is necessary instead of dimension lock), astral is blocked (forbiddance), and scrying is blocked as much as it is able. (It's till vulnerable to the high level scry spells) Competent players will use Polymorph Any Object or Earth to Mud to tunnel down through the shortest path. Extraction of transmuted objects is a trivial exercise left for the reader's unseen servants or better. A better option than polymorphing rock is polymorphing players into any of the many many creatures that can pass through (whatever material the opposition constructed their fortress out of). Assuming players don't want to polymorph today can use Stoneshape or Summon a creature to make a tunnel, break through defenses, penetrate down to the forbidden area and cast dispel magic, etc. Therefore, high level players will find the schwerpunkt of the dungeon and apply arbitrary amounts of force to it. Parties that don't include Tier-1 casters can simply purchase scrolls to accomplish the same thing.
Once players have found the best means of approach, they move in, disjunct the enemy, and kill them. If players want to amuse themselves, they can simply use mage's disjunction to penetrate each successive level of defenses, completely wiping out any defensive spells the opposition emplaced. For extra amusement, they can then use stoneshape to turn the defences around on the enemy.
All of these recommendations have been made without using splatbooks. The spells of spell compendium simply advance the levels of "win" for higher level parties. Psionics also makes things more entertaining for higher level characters.
All hope is not lost
This situation is actually a fairly clear one in game-theoretic strategies and military strategies. Most modern forces simply cannot sit in fixed defenses against a technically superior enemy. Large defenses simply mean a larger aiming point for arbitrarily large explosive power.
In order to solve the problem, the idea of mobility is found. We take the idea of mobility, and we combine it with the game-theoretic strategy of random movement. If the enemy has perfect knowledge of you, then any decision you make can be responded to in the correct way. In order to force the enemy to hedge his/her bets, randomness must be employed.
Weaker forces must manoeuvre and feint against stronger forces. Enchanting a plane shift spell into an item that activates randomly for a random sub-plane in the infinite dimension of your choice. It must be linked to a detect scrying and a detect teleportation spell. Any time teleportation is detected, any time scrying is detected, and generally 1/round, the spell must trigger. This strategy is, however, defeatable via going to an appropriate fast-time demi-plane (which the heroes should have access to anyway), casting discern location and an appropriate transport spell before the 1/round ticks over, emerging and then dropping disjunction and/or dispel magic on the target.
This rather destroys the idea of a dungeon, however. To reiterate there is no way that a party of lower-level characters can create a strongly held dungeon against a group of high level characters
If this situation must transpire, then it must take place on an anti-magic demi-plane where creativity and inventiveness have some utility. Even there it can be trivially defeated by a wish.
If you give better requirements as to the resources available to your evil party and the composition of the good party, I may be able to make some specific recommendations.