试图保存图表时,C#目录不正确



我希望你能帮我,我正在尝试从图表中生成图像,首先我使用:

public void generateDIR()
{
    DocumentsDIR = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    bool flag=true;
    int counter = 0;
    while (flag)
    {
        counter++;
        Console.WriteLine(counter);
        string dateforDir = "_"+DateTime.Now.Month.ToString() + "_" + DateTime.Now.DayOfWeek.ToString() + "_"+ DateTime.Now.Year.ToString();
        ReportDIR = DocumentsDIR + "/SisDatCal_Reports/Report_"+counter.ToString()+dateforDir+ "/";
        bool exists = System.IO.Directory.Exists(ReportDIR);
        if (!exists)
        {
            ReportPlotsDIR = ReportDIR + "Plots/";
            System.IO.Directory.CreateDirectory(ReportDIR);
            System.IO.Directory.CreateDirectory(ReportPlotsDIR);
            flag = false;
        }     
     }
}

要获得我想要保存图像的目录生成的"ReportPlotsDir",然后我使用下一个代码生成图表的图像"chartVolts":

String ChartVName = ReportPlotsDIR + "IMG_ChartVolts.png";
chartVolts.SaveImage(ChartVName, System.Drawing.Imaging.ImageFormat.Png);

这很好,在程序执行过程中没有显示错误,但当我转到文件夹并搜索"IMG_ChartVolts.png"时,它会保存在解决方案的"Bin\Debug"文件夹中,而不是"ReportPlotsDIR"中。知道为什么会这样吗?

放一个调试点,看看ReportDIR的值,你会注意到你需要使用反斜杠而不是正斜杠,但最好使用Path

ReportDIR = Path.Combine(DocumentsDIR, "SisDatCal_Reports\Report_" + counter + dateforDir + "\");

除此之外,对于格式化日期,您可以使用:

string dateforDir = DateTime.Now.ToString("_MM_dddd_yyyy", System.Globalization.CultureInfo.InvariantCulture);

谢谢大家,执行顺序中有一个错误,而不是路径生成中,直到我正确调试它,我才看到它。如果有人需要它,问题中的代码帖子会非常完美。

最新更新