Domanda

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?

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top