[已解决]在C#中使用硬编码字符串值时出错 [英] [Solved] Error while using Hardcoded String value in C#

查看:103
本文介绍了[已解决]在C#中使用硬编码字符串值时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码.
但这给了我错误:无法识别的转义序列

路径= " ;  

这是我的代码

 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用使用System.Drawing;
使用 System.Linq;
使用 System.Text;
使用使用System.Windows.Forms;

命名空间 WindowsFormsApplication1
{
    公共 部分  FrmImportFile:表格
    {
        公共 FrmImportFile()
        {
            InitializeComponent();
        }

        私有 无效 button2_Click(对象发​​件人,EventArgs e)
        {
            字符串路径;
            路径= " ;
            dataGridView1.DataSource = ControlRules.ClsConnect.ReadCSV();
        }
    }
} 

解决方案

使用其中一个

Path = @"D:\AAIGUILDMAY10.csv";


使得字符串无法解析


Path ="D:\\AAIGUILDMAY10.csv";


这样可以逃脱"\"


干杯


I have below code with me .
But it gives me the error : Unrecognized escape sequence

Path ="D:\AAIGUILDMAY10.csv";



Here is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FrmImportFile : Form
    {
        public FrmImportFile()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string Path;
            Path ="D:\AAIGUILDMAY10.csv";
            dataGridView1.DataSource = ControlRules.ClsConnect.ReadCSV();
        }
    }
}

解决方案

use either

Path = @"D:\AAIGUILDMAY10.csv";


that makes the string unparsed

or

Path ="D:\\AAIGUILDMAY10.csv";


this escapes the "\"


Cheers


这篇关于[已解决]在C#中使用硬编码字符串值时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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