Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ArgumentException: Illegal characters in path. error

Tags:

c#

excel

I`m getting an ArgumentException from the following code:

string strPath="C:\somename.xls";
startPath=System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
filePath = System.IO.Path.Combine(startPath, strPath);

I found this code on Stack Overflow. Link: C#:Copy protected worksheet to another excel file I don't exactly know what it is. Please tell me what it is. This code I'm building into an exe.

Lastly, I need to Copy one worksheet to another file.

What`s wrong am I doing? I deploy this in server.

like image 758
user2144293 Avatar asked Mar 12 '13 18:03

user2144293


1 Answers

what that code appears to do, is it gets your working directory(wherever the exe associated with your code is), and combines it with "C:\\somename.xls"(which doesn't make sense.)

I think you might have intended something like

string strPath=@"somename.xls";

so assume you're running your application from

"C:\Users\owner\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug"

what that code would do is set filePath to

"C:\Users\owner\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\somename.xls"

the first thing I saw was

string filePath="C:\somename.xls";

\ is a special character, for determining other characters. for instance '\n' is a newline. '\\' is the actual backslash.

so, you want to escape your \ with another \

string filePath="C:\\somename.xls";

or make it a literal string by putting a @ in front of it.

string filePath=@"C:\somename.xls";

like image 56
Sam I am says Reinstate Monica Avatar answered Oct 21 '22 12:10

Sam I am says Reinstate Monica