Frage

I'm creating a copy of an Office 2010 word document for some OpenXML automation. The below code blows up on File.Copy saying it doesn't like the path\name combination of the copiedPath variable

        string mainPath = @"Path\Name.docx";
        string copiedPath = @"Path\Name" +
        DateTime.Now.ToString().Replace("/", "-").Replace(" ", "-") +".docx";

        File.Copy(mainPath, copiedPath);

If I make the following change the code works fine:

string copiedPath = @"Path\Name_Test_.docx"; 

but since this is going to be used on the company intranet I'd like a better way to separate the different requests by users. Has anyone pulled off what I'm trying to do or can you see what I'm doing wrong?

War es hilfreich?

Lösung

You are not allowed to have a : in a file name, so you will have to replace that charactor as well.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top