简体   繁体   中英

error : The given path's format is not supported

Getting this error The given path's format is not supported. at this line

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

C:\Users\Monika\Documents\Visual Studio 2010\Projects\SonoRepo\SonoRepo\bin\Debug\Patients\Patient_16\visit_4_16-10-2013 00:00:00\

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

additional note:

use Path.Combine to make full path, like:

var path = Path.Combine(strpath , "Patients", "Patient_" + pid);

and last

string suffix = "visit_"+visitNo+"_" + visitdate;
var visit_Path = Path.Combine(path, suffix);

In general always use Path.Combine to create paths:

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 "."
}

你不能有冒号:在文件路径中

You can't use colons ( : ) in a path. You can for example Replace() them with dots ( . ).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM