C# 错误:不支持给定路径的格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19424368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error : The given path's format is not supported
提问by Durga
Getting this error The given path's format is not supported.
at this line
The given path's format is not supported.
在这一行得到这个错误
System.IO.Directory.CreateDirectory(visit_Path);
Where I am doing mistake in below code
我在下面的代码中做错的地方
void Create_VisitDateFolder()
{
this.pid = Convert.ToInt32(db.GetPatientID(cmbPatientName.SelectedItem.ToString()));
String strpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
String path = strpath + "\Patients\Patient_" + pid + "\";
string visitdate = db.GetPatient_visitDate(pid);
this.visitNo = db.GetPatientID_visitNo(pid);
string visit_Path = path +"visit_" + visitNo + "_" + visitdate+"\";
bool IsVisitExist = System.IO.Directory.Exists(path);
bool IsVisitPath=System.IO.Directory.Exists(visit_Path);
if (!IsVisitExist)
{
System.IO.Directory.CreateDirectory(path);
}
if (!IsVisitPath)
{
System.IO.Directory.CreateDirectory(visit_Path);\error here
}
}
getting this value for visit_Path
得到这个值 visit_Path
C:\Users\Monika\Documents\Visual Studio 2010\Projects\SonoRepo\SonoRepo\bin\Debug\Patients\Patient_16\visit_4_16-10-2013 00:00:00\
采纳答案by Tim Schmelter
In general always use Path.Combine
to create paths:
通常总是用于Path.Combine
创建路径:
String strPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
String path = Path.Combine(strPath,"Patients","Patient_" + pid);
string visitdate = db.GetPatient_visitDate(pid);
this.visitNo = db.GetPatientID_visitNo(pid);
string fileName = string.Format("visit_{0}_{1}", visitNo, visitdate);
string visit_Path = Path.Combine(path, fileName);
bool IsVisitExist = System.IO.Directory.Exists(path);
bool IsVisitPath=System.IO.Directory.Exists(visit_Path);
To replace invalid characters from a filename you could use this loop:
要从文件名中替换无效字符,您可以使用此循环:
string invalidChars = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
foreach (char c in invalidChars)
{
visit_Path = visit_Path.Replace(c.ToString(), ""); // or with "."
}
回答by CodeCaster
You can't use colons (:
) in a path. You can for example Replace()
them with dots (.
).
不能:
在路径中使用冒号 ( )。例如,您可以Replace()
用点 ( .
)来表示它们。
回答by Ben Robinson
You can't have colons :
in file paths
:
文件路径中不能有冒号
回答by Kamil Budziewski
You can not have :
in directory name, I suggest you to use this to string to get date in directory name:
您不能:
在目录名称中,我建议您使用它来字符串以获取目录名称中的日期:
DateTime.Now.ToString("yyyy-MM-dd hh_mm_ss");
it will create timestamp like:
它将创建时间戳,如:
2013-10-17 05_41_05
2013-10-17 05_41_05
additional note:
补充说明:
use Path.Combine
to make full path, like:
用于Path.Combine
制作完整路径,例如:
var path = Path.Combine(strpath , "Patients", "Patient_" + pid);
and last
最后
string suffix = "visit_"+visitNo+"_" + visitdate;
var visit_Path = Path.Combine(path, suffix);
回答by Ramesh Kotikalapudi
Just wanted to add my two cents. I assigned the path from a text box to string and also adding additional strings, but I forgot to add the .Text to the text box variable.
只是想加上我的两分钱。我分配了从文本框到字符串的路径,并添加了其他字符串,但我忘记将 .Text 添加到文本框变量中。
So instead of
所以代替
strFinalPath = TextBox1.Text + strIntermediatePath + strFilename
I wrote
我写
strFinalPath = TextBox1 + strIntermediatePath + strFilename
So the path became invalid because it contained invalid characters. I was surprised that c# instead of rejecting the assignment because of type mismatch, assigned invalid value to the final string. So look at the path assignment string closely.
所以路径变得无效,因为它包含无效字符。我很惊讶 c# 没有因为类型不匹配而拒绝赋值,而是为最终字符串分配了无效值。因此,请仔细查看路径分配字符串。