© 2016 CERN.for the ALICE Collaboration. Background: In the context of the investigation of the quark gluon plasma produced in heavy-ion collisions, hadrons containing heavy (charm or beauty) quarks play a special role for the characterization of the hot and dense medium created in the interaction. The measurement of the production of charm and beauty hadrons in proton-proton collisions, besides providing the necessary reference for the studies in heavy-ion reactions, constitutes an important test of perturbative quantum chromodynamics (pQCD) calculations. Heavy-flavor production in proton-nucleus collisions is sensitive to the various effects related to the presence of nuclei in the colliding system, commonly denoted cold-nuclear-matter effects. Most of these effects are expected to modify open-charm production at low transverse momenta (pT) and, so far, no measurement of D-meson production down to zero transverse momentum was available at mid-rapidity at the energies attained at the CERN Large Hadron Collider (LHC). Purpose: The measurements of the production cross sections of promptly produced charmed mesons in p-Pb collisions at the LHC down to pT=0 and the comparison to the results from pp interactions are aimed at the assessment of cold-nuclear-matter effects on open-charm production, which is crucial for the interpretation of the results from Pb-Pb collisions. Methods: The prompt charmed mesons D0,D+,D∗+, and Ds+ were measured at mid-rapidity in p-Pb collisions at a center-of-mass energy per nucleon pair sNN=5.02 TeV with the ALICE detector at the LHC. D mesons were reconstructed from their decays D0→K-π+,D+→K-π+π+, D∗+→D0π+,Ds+→φπ+→K-K+π+, and their charge conjugates, using an analysis method based on the selection of decay topologies displaced from the interaction vertex. In addition, the prompt D0 production cross section was measured in pp collisions at s=7 TeV and p-Pb collisions at sNN=5.02 TeV down to pT=0 using an analysis technique that is based on the estimation and subtraction of the combinatorial background, without reconstruction of the D0 decay vertex. Results: The production cross section in pp collisions is described within uncertainties by different implementations of pQCD calculations down to pT=0. This allowed also a determination of the total cc production cross section in pp collisions, which is more precise than previous ALICE measurements because it is not affected by uncertainties owing to the extrapolation to pT=0. The nuclear modification factor RpPb(pT), defined as the ratio of the pT-differential D meson cross section in p-Pb collisions and that in pp collisions scaled by the mass number of the Pb nucleus, was calculated for the four D-meson species and found to be compatible with unity within uncertainties. The results are compared to theoretical calculations that include cold-nuclear-matter effects and to transport model calculations incorporating the interactions of charm quarks with an expanding deconfined medium. Conclusions: These measurements add experimental evidence that the modification of the D-meson transverse momentum distributions observed in Pb-Pb collisions with respect to pp interactions is due to strong final-state effects induced by the interactions of the charm quarks with the hot and dense partonic medium created in ultrarelativistic heavy-ion collisions. The current precision of the measurement does not allow us to draw conclusions on the role of the different cold-nuclear-matter effects and on the possible presence of additional hot-medium effects in p-Pb collisions. However, the analysis technique without decay-vertex reconstruction, applied on future larger data samples, should provide access to the physics-rich range down to pT=0.