我用FolderBrowserDialog.SelectedPath 获取文件夹的路径为file="C:\abc1\abc2",假设文件名filename="filename".那我把这2个组合起来就是一个路径,方法是:string filePath=file+"\\"+filename.
 但是我用这个路径的调试时候发现系统给filePath的值是:fileName="C:\\abc1\\abc2\\filename".变成了双斜杠。然后我用这个方法也试了下:string filePath=@file+"\\"+filename.还是一样的结果。
 求解啊!!!!!!为什么啊!!!!!

解决方案 »

  1.   

    系统给你的filePath就是双斜杠,双斜杠就双斜杠,你获取后再转换下不就行了。string fileName="C:\\abc1\\abc2\\filename";
    fileName = fileName.Replace("\\","\");然后这个串不就是你想要的了嘛
      

  2.   

    大哥,你确定Replace("\\","\");中 ,“\“可以用? 我早就试了,没有用啊。
      

  3.   

    google. baidu
    转义字符
      

  4.   

    string fileName="C:\\abc1\\abc2\\filename";
    fileName = fileName.Replace("\\\\","\\");
      

  5.   

    或者是这样string fileName="C:\\abc1\\abc2\\filename";
    fileName = fileName.Replace(@"\\",@"\");
      

  6.   

    +1
    string filePath=file+"\\"+filename;
    或string filePath=file+@"\"+filename.
      

  7.   

     string filePath = @folderBrowserDialog1.SelectedPath + @"\" + textBox1.Text.Trim().ToString()+".bak";
     if (!File.Exists(filePath))
                        {
                            SqlConnection conn = new SqlConnection("server=" + local + ";database=Master;uid=" + username + ";pwd=" + pwd);
                            conn.Open();
                            SqlCommand cmd = new SqlCommand();
                           // SqlCommand cmd = new SqlCommand("backup database" + comboBox1.Text.Trim() + "to disk='" + filePath + "'",conn);
                            cmd.CommandText = "backup database " + comboBox1.Text.Trim() + "to disk='"+aa+"'";
                            cmd.Connection = conn;
                            cmd.ExecuteNonQuery();
                            conn.Close();
                            MessageBox.Show("备份成功!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.Close();                    }
                        else
                        {
                            MessageBox.Show("文件已存在!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
      

  8.   

       string filePath = textBox2.Text.Trim().ToString() + "\\"+ textBox1.Text.Trim().ToString() + ".bak"
                        if (!File.Exists(filePath))
                        {
                            SqlConnection conn = new SqlConnection("server=" + local + ";database=Master;uid=" + username + ";pwd=" + pwd);
                            conn.Open();
                            SqlCommand cmd = new SqlCommand();
                           // SqlCommand cmd = new SqlCommand("backup database" + comboBox1.Text.Trim() + "to disk='" + filePath + "'",conn);
                            cmd.CommandText = "backup database " + comboBox1.Text.Trim() + "to disk='"+filePath+"'";
                            cmd.Connection = conn;
                            cmd.ExecuteNonQuery();
                            conn.Close();
                            MessageBox.Show("备份成功!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            this.Close();                    }
                        else
                        {
                            MessageBox.Show("文件已存在!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
      

  9.   

    string filePath = @folderBrowserDialog1.SelectedPath + @"\" + textBox1.Text.Trim().ToString()+".bak";=====================》string filePath = folderBrowserDialog1.SelectedPath;
    filePath = folderBrowserDialog1.SelectedPath.Replace(@"\\",@"\");
    filePath += @"\" + textBox1.Text.Trim().ToString()+".bak";
      

  10.   

    转义符。\n代表换行,\t代表制表符,\r代表回车,\b代表退格符,\\代表\
    这是因为\用于转义前缀以后,它自身只能以\\来表示了。