错误:不支持给定路径的格式

Dur*_*rga 2 c#

The given path's format is not supported.在此行获取此错误

System.IO.Directory.CreateDirectory(visit_Path);
Run Code Online (Sandbox Code Playgroud)

我在下面的代码中做错了

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
            }
        }
Run Code Online (Sandbox Code Playgroud)

得到这个值 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\
Run Code Online (Sandbox Code Playgroud)

Kam*_*ski 7

你不能:在目录名中,我建议你用它来字符串来获取目录名中的日期:

DateTime.Now.ToString("yyyy-MM-dd hh_mm_ss");
Run Code Online (Sandbox Code Playgroud)

它会创建时间戳,如:

2013-10-17 05_41_05

附加说明:

Path.Combine做完整路径,如:

var path = Path.Combine(strpath , "Patients", "Patient_" + pid);
Run Code Online (Sandbox Code Playgroud)

最后

string suffix = "visit_"+visitNo+"_" + visitdate;
var visit_Path = Path.Combine(path, suffix);
Run Code Online (Sandbox Code Playgroud)


Tim*_*ter 6

通常总是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);
Run Code Online (Sandbox Code Playgroud)

要从文件名中替换无效字符,您可以使用此循环:

string invalidChars = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
foreach (char c in invalidChars)
{
    visit_Path = visit_Path.Replace(c.ToString(), ""); // or with "."
}
Run Code Online (Sandbox Code Playgroud)