C# System.ArgumentException:路径中存在非法字符。错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15368908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.ArgumentException: Illegal characters in path. error
提问by user2144293
I`m getting an ArgumentException from the following code:
我从以下代码中得到一个 ArgumentException:
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 fileI don't exactly know what it is. Please tell me what it is. This code I'm building into an exe.
我在 Stack Overflow 上找到了这段代码。链接: C#:Copy protected worksheet to another excel file我不知道它是什么。请告诉我它是什么。这段代码我正在构建到一个 exe 中。
Lastly, I need to Copy one worksheet to another file.
最后,我需要将一个工作表复制到另一个文件。
What`s wrong am I doing? I deploy this in server.
我怎么了?我在服务器中部署它。
回答by Sam I am says Reinstate Monica
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.)
该代码似乎在做什么,它获取您的工作目录(exe
与您的代码相关联的任何位置),并将其与"C:\\somename.xls"
(这没有意义。)
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
该代码将执行的操作设置filePath
为
"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.
\
是一个特殊字符,用于确定其他字符。例如'\n'
是一个换行符。 '\\'
是实际的反斜杠。
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";
回答by Adam Plocher
Your code should be:
你的代码应该是:
string filePath = "C:\somename.xls"
You need double backslashes.
你需要双反斜杠。
回答by M Afifi
Two problems with the code,
代码有两个问题,
First
第一的
string filePath="C:\somename.xls";
\
is a special character, for determining other characters. for instance '\n'
is a newline. '\\'
is the actual backslash.
\
是一个特殊字符,用于确定其他字符。例如'\n'
是一个换行符。 '\\'
是实际的反斜杠。
Second
第二
filePath
contains a root path, C:\\
. Path.Combine
will just return filePath
then, it cannot be combined.
filePath
包含一个根路径,C:\\
。 Path.Combine
届时将返回filePath
,无法合并。
回答by ganji
Your main problem is in startPath
parameter.
您的主要问题在于startPath
参数。
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
If you trace your code, in FileName
you will see a bad symbol character witch is illegal
如果你跟踪你的代码,FileName
你会看到一个坏符号字符女巫是非法的