开发者

saving in programmer-defined folder in C#

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);`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜