error:The given path's format is not supported

2019-02-25 01:24发布

问题:

What am I doing wrong? My major problem is that I'm getting an error says:

error : The given path's format is not supported

I would like to save the file within the project itself under a folder I have already created named:Screenshots

public void TakeScreenShot()
{
    string pth = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
    string actualPath = pth.Substring(0, pth.LastIndexOf("bin"));
    string projectPath = new Uri(actualPath).LocalPath;

    Screenshot ss = ((ITakesScreenshot)_driver).GetScreenshot();
    string screenshot = ss.AsBase64EncodedString;
    byte[] screenshotAsByteArray = ss.AsByteArray;
    ss.SaveAsFile(projectPath+"Screenshots\\Drisha"+DateTime.Now.ToString()+".jpeg", ImageFormat.Jpeg); 
}

回答1:

I don't know what culture your machine is set to but I assume calling DateTime.Now.ToString() gives you something like 08/02/2017 11:41:30 which contains slashes and colons and is therefore not a valid path.

Try specifying a format inside ToString() like this:

ss.SaveAsFile(projectPath+"Screenshots\\Drisha"+DateTime.Now.ToString("ddMMyyyyHHmmss")+".jpeg", ImageFormat.Jpeg); 

As a side note you should not be concatenating strings to make a path, instead use Path.Combine.

ss.SaveAsFile(Path.Combine(projectPath, "Screenshots\\Drisha", DateTime.Now.ToString("ddMMyyyyHHmmss"), ".jpeg"), ImageFormat.Jpeg);