当前位置:编程学习 > C#/ASP.NET >>

关于 StreamReader 的用法问题

各位大哥,小弟这段时间刚接触.net,在使用中出了些问题,想请教下各位

我在使用 StreamReader 读取csv的时候碰到了问题,
StreamReader ^ sr = gcnew StreamReader("G:\\out.csv");
如果定义成这样可以正常操作,而如果把路径放到字符串string path中,然后函数调用就会出错
StreamReader ^ sr = gcnew StreamReader(path);

错误信息是“System::IO::StreamReader::StreamReader(System::IO::Stream ^)”: 不能将参数 1 从“std::string”转换为“System::IO::Stream ^”

我已经不知道要怎么改了,各位大哥给提提建议吧,因为我要读取的文件路径不是固定的,是查找然后传递过来的,所以只能放在变量中,那要怎么使用 StreamReader呢? --------------------编程问答-------------------- 错误提示已经很明确了,你的path变量是std::string类型,而StreamReader构造函数接受的是System::String类型
建议你使用System::String类型,实在需要用std::string的话,在这里用
gcnew StreamReader(gcnew System::String(path.c_str()))

代替原来的 --------------------编程问答-------------------- 楼上的大哥,我之前已经试过System::String了,也不行,错误提示都基本一样,只是std变成System --------------------编程问答-------------------- 你确定你用的是3.5或者4.0+的.Net Framework而且不是Portable/Windows Store库?
你看看对象浏览器里StreamReader有没有以String^为参数的构造函数 --------------------编程问答-------------------- String^ yourpath
--------------------编程问答-------------------- path定义不对
String^ path = ??;//你自己的路径
不是string 是String^
补充:.NET技术 ,  VC.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,