我该如何解决这个问题? String Path = Server.Mappath(" Files \Hrms Phase 2 - Kovai.Xlsx& Quot;);"在C#? [英] How Do I Fix This " String Path = Server.Mappath("Files\Hrms Phase 2 - Kovai.Xlsx&Quot;);" In C#?

查看:57
本文介绍了我该如何解决这个问题? String Path = Server.Mappath(" Files \Hrms Phase 2 - Kovai.Xlsx& Quot;);"在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这显示了c#中的错误。



string path = Server.MapPath(Files\filename.xls);



它说



描述:编译服务此请求所需的资源时出错。请查看以下具体错误详情并适当修改你的源代码。



编译器错误信息:CS1009:无法识别的转义序列



源错误:





第21行:protected void Open21_Click(object sender,EventArgs e)

第22行:{

第23行:string path = Server.MapPath(Files \HRMS Phase 2 - Kovai.xlsx);

第24行:string connstr =Provider = Microsoft .ACE.OLEDB.12.0;数据源=+路径+;扩展属性= Excel 10.0;;

第25行:OleDbConnection conn =新OleDbConnection(connstr);



源文件:d:\Homepage.aspx.cs行:23

This shows error in c#.

string path = Server.MapPath("Files\filename.xls");

it says

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1009: Unrecognized escape sequence

Source Error:


Line 21: protected void Open21_Click(object sender, EventArgs e)
Line 22: {
Line 23: string path = Server.MapPath("Files\HRMS Phase 2 - Kovai.xlsx");
Line 24: string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;­Data Source=" + path + ";Extended Properties=Excel 10.0;";
Line 25: OleDbConnection conn = new OleDbConnection(connstr);

Source File: d:\Homepage.aspx.cs Line: 23

推荐答案

我可以看到三个问题。首先,输入'\'的正确语法是
I can see three problems. First, correct syntax for entering of '\' is
<br />
@"Files\filename.xls"

Files\\filename.xls。请阅读C#字符串文字的参考。但为什么? (见下文。:-))



此外,你不需要使用相对路径的 MapPath 。如果真的需要 MapPath ,可能你的意思是〜/ Files / filename.xls。 '〜'表示为您的站点设置的根目录。确实需要 MapPath 从相对于文件路径的文件路径中获取服务器主机文件系统中的绝对文件路径(服务器端文件I / O确实需要) root。



第三个问题是:你根本不需要反斜杠。您应该在Web路径中使用斜杠,而不是反斜杠。 '/',而不是'\'。



-SA

or "Files\\filename.xls". Please read the reference on the C# string literal. But why? (See below. :-))

Besides, you would not need to use MapPath with relative path. If MapPath was really needed, probably you meant "~/Files/filename.xls". '~' means the root directory set up for your site. MapPath is really needed to obtain the absolute file path in the server host's file system (which is really needed for server-side file I/O) from the file path relative to the root.

Third problem is: you don't need backslash at all. You should use slash in Web paths, not backslash. '/', not '\'.

—SA


这篇关于我该如何解决这个问题? String Path = Server.Mappath(&quot; Files \Hrms Phase 2 - Kovai.Xlsx&amp; Quot;);&quot;在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆