using (FileStream fileStream = File.OpenRead(path))报路径访问被拒绝,要怎么改~~~

q9w8e7r4t5y1u3 2011-07-12 05:00:38
namespace 手机号码归属地查询
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() != DialogResult.OK)
{
return;
}
string path = dlg.SelectedPath;

string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "delete from table1";
cmd.ExecuteNonQuery();
}
}


using (FileStream fileStream = File.OpenRead(path)) {
using (StreamReader streamReader = new StreamReader(fileStream))
{

using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into table1(StartNo,EndNo,Name) values (@StartNo,@EndNo,@Name)";
string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
foreach (string file in files)
{
string 运营商 = Path.GetFileNameWithoutExtension(file);
// string[] lines = File.ReadAllLines(file, Encoding.Default);
string line = null;
while ((line = streamReader.ReadLine()) != null)
{
string[] strs = line.Split('-');
string 开始号码 = strs[0];
string 结束号码 = strs[1];
string 市 = strs[2];
cmd.Parameters.Clear();
cmd.Parameters.Add("StartNo", 开始号码);
cmd.Parameters.Add("EndNo", 结束号码);
cmd.Parameters.Add("Name", 运营商 + 市);
cmd.ExecuteNonQuery();
}



}
}
}
}

}






MessageBox.Show("导入成功");

}
}
}
...全文
2503 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
q9w8e7r4t5y1u3 2011-07-13
  • 打赏
  • 举报
回复
恩 改好了 谢谢哈 我刚学就乱用的
孟子E章 2011-07-13
  • 打赏
  • 举报
回复
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() != DialogResult.OK)
{
return;
}
string path = dlg.SelectedPath;

你的path 是个文件夹,怎么使用FileStream 呢?


using (FileStream fileStream = File.OpenRead(path)) {
using (StreamReader streamReader = new StreamReader(fileStream))
这2行也没有什么用处啊


应该
去掉上面2行
然后
string 运营商 = Path.GetFileNameWithoutExtension(file);
StreamReader streamReader = new StreamReader(file);
while ((line = streamReader.ReadLine()) != null)
.........
q9w8e7r4t5y1u3 2011-07-13
  • 打赏
  • 举报
回复
权限应该没问题 这个文件夹是我自己建的 那应该怎么改程序???
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 net_lover 的回复:]

看文件的属性-安全性
[/Quote]
将登录用户相关权限的勾点上
孟子E章 2011-07-13
  • 打赏
  • 举报
回复
看文件的属性-安全性
q9w8e7r4t5y1u3 2011-07-13
  • 打赏
  • 举报
回复
怎么看 当前这个帐户是否对这个路径下的文件有访问的权限?
636f6c696e 2011-07-12
  • 打赏
  • 举报
回复
估计是权限不对或者路径不存在,调试下应该就知道了:)
蝶恋花雨 2011-07-12
  • 打赏
  • 举报
回复
路径不对/。或者没有权限。请设定断点进行跟踪。

打开文件进行读取: File.OpenRead(String);

如下:
//在C盘新建123.txt文件,使用流对象fs对文件进行操作,fs可以进行读文件File.OpenRead()

FileStream fs=File.OpenRead(@"c:\123.txt");

蝶恋花雨 2011-07-12
  • 打赏
  • 举报
回复
你看看当前这个帐户是否对这个路径下的文件有访问的权限。
蝶恋花雨 2011-07-12
  • 打赏
  • 举报
回复
using (FileStream fs = new FileStream(path, FileMode.Open)) 用这个试试
孟子E章 2011-07-12
  • 打赏
  • 举报
回复
MessageBox.Show(path);看是什么,是完整的文件路径吗

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧