如何使用C#Windows Form在Excel文件的路径中添加/(反斜杠) [英] how to add / (backslash) to path of excel file using c# windows form

查看:212
本文介绍了如何使用C#Windows Form在Excel文件的路径中添加/(反斜杠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用savefiledialog动态分配excel文件的路径

所以我需要为Excel文件分配动态路径

这样

我将替换Data Source = D:\\ Book3105.xlsx

使用

数据源= textbox2.text

但是如果我通过savefiledialog获取到textbox2的路径,它将是

D:/Book1000.xlsx,但textbox2的路径必须为D://Book1000.xlsx

如何通过C#在路径编程中添加反斜杠/?

表示我需要在D:/Book1000.xlsx中添加/反斜杠


私有无效按钮7_Click_1(对象发送者,EventArgs e)
        {
            流myStream;
            SaveFileDialog saveFileDialog2 = new SaveFileDialog();

            //saveFileDialog2.Filter ="Execl文件(* .xls)| * .xls | txt文件(* .txt)| * .txt |所有文件(*.*)| *.*"; Excel | * .xlsx
            saveFileDialog2.Filter ="Excel | * .xlsx";
            saveFileDialog2.FilterIndex = 0;
            saveFileDialog2.RestoreDirectory = true;

            如果(saveFileDialog2.ShowDialog()== DialogResult.OK)
            {
                如果((myStream = saveFileDialog2.OpenFile())!= null)
                {
                   
                    myStream.Close();
                    path3 = string.Format("\" {0} \",saveFileDialog2.FileName);
                    textBox2.Text = path3;
                }
            }
        } 

使用savefiledialog创建Excel文件时如何在反斜杠添加到分配给文本框的路径中

解决方案

也许使用string.Replace

字符串somePath ="D:/Book1000.xlsx";
字符串结果= somePath.Replace("/","//");
Console.WriteLine(result); 

结果

 D://Book1000.xlsx


I need to assign path of excel file dynamically using savefiledialog

so that i need to assign dynamic path to excel file

so that

i will replace Data Source=D:\\Book3105.xlsx

with

Data Source= textbox2.text

But if i get path by savefiledialog to textbox2 it will be

D:/Book1000.xlsx but textbox2 must have path as D://Book1000.xlsx

How to add backslash / to path programming by c# ?

meaning i need to add / backslash to D:/Book1000.xlsx


 private void button7_Click_1(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog2 = new SaveFileDialog();

            // saveFileDialog2.Filter = "Execl files (*.xls)|*.xls|txt files (*.txt)|*.txt|All files (*.*)|*.*"; Excel | *.xlsx
            saveFileDialog2.Filter = "Excel | *.xlsx"; 
            saveFileDialog2.FilterIndex = 0;
            saveFileDialog2.RestoreDirectory = true;

            if (saveFileDialog2.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog2.OpenFile()) != null)
                {
                   
                    myStream.Close();
                    path3 = string.Format("\"{0}\"", saveFileDialog2.FileName);
                    textBox2.Text = path3;
                }
            }
        }

How to add backslash to pathh assigned to textbox when create excel file using savefiledialog

解决方案

Perhaps using string.Replace

string somePath = "D:/Book1000.xlsx";
string result = somePath.Replace("/", "//");
Console.WriteLine(result);

Result

D://Book1000.xlsx


这篇关于如何使用C#Windows Form在Excel文件的路径中添加/(反斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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