不支持给定路径的格式

不支持给定路径的格式

本文介绍了错误:不支持给定路径的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做错了什么?我的主要问题是我收到一条错误消息:

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

我想将文件保存在项目本身中的一个我已经创建的文件夹下,名称为:Screenshots

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);
}

推荐答案

我不知道您的计算机设置了什么文化,但我想调用DateTime.Now.ToString()会给您类似08/02/2017 11:41:30的内容,其中包含斜杠和冒号,并且因此不是有效路径.

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.

尝试在ToString()内部指定格式,如下所示:

Try specifying a format inside ToString() like this:

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

作为旁注,您不应串联字符串以构成路径,而应使用Path.Combine.

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

这篇关于错误:不支持给定路径的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 04:10