C#:将屏幕截图保存到应用程序基目录中的子目录中?

C# : saving screenshot into a sub directory within the app base directory?

我正在尝试将屏幕截图保存到位于应用程序基本目录内的 img 子目录中;但是,图像文件保存在应用程序的基本目录中。如何将图像保存到 img 子目录?

代码:

private void screenCapture()
{
    try
    {
        string appDir = AppDomain.CurrentDomain.BaseDirectory;
        string pathString = System.IO.Path.Combine(appDir, "img");

        Bitmap memoryImage;
        memoryImage = new Bitmap(1000, 900);
        Size s = new Size(memoryImage.Width, memoryImage.Height);

        Graphics memoryGraphics = Graphics.FromImage(memoryImage);

        memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);

        string fileName =
            string.Format(pathString
            + DateTime.Now.ToString("yyyyMMdd_hhmmss")
            + ".png");

        // save it  
        memoryImage.Save(fileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

您只是没有正确组合路径组件和文件名。试试这个:

string appDir = AppDomain.CurrentDomain.BaseDirectory;
string pathString = System.IO.Path.Combine(appDir, "img");
string fileName = DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".png";
string fullPath = System.IO.Path.Combine(pathString, fileName);