Wolfish
Wolfish

Reputation: 970

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?

Upvotes: 0

Views: 651

Answers (2)

Ofiris
Ofiris

Reputation: 6151

Replace:

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

with:

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

Remove ")."

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

Upvotes: 1

Agent_Spock
Agent_Spock

Reputation: 1197

try this it might work just fine for you

@"C:\Testing facility\SampleDirectory"

Upvotes: 1

Related Questions