A distributed control method is proposed to simultaneously optimize the power sharing among sources of islanded dc microgrids, while regulating the distribution bus voltage. During the optimization process, an economic dispatch problem is solved to minimize the total generation cost by setting the output powers of the dispatchable sources. To this end, the voltage set points of individual dc-dc converters are adjusted using a voltage regulator and an optimizer, which regulates the average voltage of the sources to establish the generation-consumption equality constraint and matches the incremental costs, respectively. Afterward, the proposed optimizer is modified to exclude the sources from the incremental cost consensus protocol upon reaching their generation limits, enforcing inequality constraints. This coregulation and cooptimization paradigm is developed in a fully distributed fashion. The dynamical model of the proposed controller is established. The steady-state analysis verifies the fulfillment of the control objectives, i.e., voltage regulation and cost minimization. Experimental results verify the controller performance and validate its resiliency against cyber and physical faults.