简体   繁体   中英

Filepath contains invalid characters

I've created a small program that creates compressed backups. Please see the edit below.

The filepaths have a colon in them that is generating a NotSupportedException when I try to run it.
If I have the following path:
C:\\Testing facility\\SampleDirectory
What can I replace : with in order that my program will process the filepath, but still find the correct directory/file?

The example shown in this MSDN article says nothing about any issues with colons or any other illegal characters, am I doing something wrong?

My directory compression code is as follows:

private void CompressDirectory()
{
    zipPath = backupPath + DateTime.Now.ToString().Replace(':', '-') + ").";

    try
    {
        ZipFile.CreateFromDirectory(sourcePath, zipPath, CompressionLevel.Optimal, true);
        getresultmessage();
    }
    catch (IOException v)
    {
        errMsg = "Failed trying to start compression. \n" + v.ToString();
    }
}

Edit:
After some more debugging, I've realised that the filestrings are being double-appended for soem reason. By this, I mean that sourcePath has gained the value:
"C:\\\\Testing facility\\\\SampleDirectory"
ans a similar event has occurred with the backup path.

Why is this, and what can I do to fix it?

try this it might work just fine for you

@"C:\\Testing facility\\SampleDirectory"

Replace:

DateTime.Now.ToString().Replace(':', '-')

with:

DateTime.Now.ToString("yyyy-MM-dd-HH-mm")

Remove ")."

As for your Edit - You see \\\\ because \\ is escaped .

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