Click here to Skip to main content
15,890,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all, I have created new directory and wanted to save chart image in this new created directory, how can I do this?

This is my code:
C#
if (!Directory.Exists(@"C:\Users\user\Desktop\Project\Project1\Test\Test\NewProject\StoreImage\" + panel.HeaderText))
                            {
                                Directory.CreateDirectory(@"C:\Users\user\Desktop\Project\Project1\Test\Test\NewProject\StoreImage\" + panel.HeaderText);
                            }
//I want to save chart image here in new created directory


Question: How to save chart image in the new created directory using C#?
Please guide me on this thanks!
Posted

1 solution

Try:
C#
string basePath = Path.Combine(@"C:\Users\user\Desktop\Project\Project1\Test\Test\NewProject\StoreImage, panel.HeaderText);
if (!Directory.Exists(basePath)
   {
   Directory.CreateDirectory(basePath);
   }
MyImageOfAChart.Save(path.Combine(basePath, "MyFileName.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
 
Share this answer
 
Comments
Member 11999641 22-Oct-15 5:04am    
hi OriginalGriff, it works, thanks for your help! :)
OriginalGriff 22-Oct-15 5:34am    
You're welcome!
pietvredeveld 22-Oct-15 16:45pm    
The solution will work. But note that the if (!Directory.Exists()) is not needed. Directory.CreateDirectory handles existing directories well. And it is a good idea to add exception handling. Also consider using the return value of Directory.CreateDirectory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900