I'm trying to save a picture in a relative path of my project. I want it to be in a folder called "images". By using this code
theImage.Save("\\images\\image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
I realized by testing with an absolute path, that the folder "images" must exist for it to work. problem is, I don't know what the root folder for t开发者_Go百科he project is, so I can manually create the folder in windows explorer. how can I fix my problem?
Directory.Create("images");
theImage.Save(@"images\image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Directory.Create
will only make a folder if one does not yet exist, so no need to worry about that. The @
before a string literal makes it so that the only character that needs to be escaped is the "
which is done like ""
. Anyways, I don't think you should have to make your own folder manually now. Also, for future reference, you can get the current working directory programmatically by using Environment.CurrentDirectory
.
EDIT: In that case, try:
Directory.CreateDirectory("images");
string path = Path.Combine(Environment.CurrentDirectory, @"images\image.jpg";
theImage.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);`
精彩评论