温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用C#无法访问路径如何解决

发布时间:2020-12-29 16:23:26 来源:亿速云 阅读:856 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用C#无法访问路径如何解决,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

用C#想写一个直接将数据库查询得到的datatable,直接导出为csv格式的文件,拷贝到导出的操作类后,一直catch到的错误提示是对路径的泛微被拒绝,一直排查原因,发现原来:FileStream(path, FileMode.OpenOrCreate,FileAccess.ReadWrite),path处所读取的字符串必须包含文件名称以及格式。现在贴完整代码,以供帮助到像我一样的初学者。

  private void button1_Click(object sender, EventArgs e)
    {
      System.IO.StreamReader st;

//由于我的查询语句较长,采用了读取txt文本的方式后做查询操作。
      st = new System.IO.StreamReader(Application.StartupPath + "\\SQL2.txt", System.Text.Encoding.Default);
   
      string stingsql=st.ReadToEnd();
      st.Close();

      textBox1.Text = stingsql;
      DataTable dt = new DataTable();
      dt = bc.QueryCommand(stingsql);
   
      string filepath = @"F:\病案导出备份\患者统计表.csv";//此处必须为路径加文件名称,否则
      ImportToCSV(dt, filepath);
    }

    public static void ImportToCSV(DataTable dt, string filepath)
    {
      FileStream fs = null;
      StreamWriter sw = null;
      try
      {
        fs = new FileStream(filepath, FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(fs, Encoding.Default);
        string head = "";
        //拼接列头
        for (int cNum = 0; cNum < dt.Columns.Count; cNum++)
        {
          head += dt.Columns[cNum].ColumnName + ",";
        }
        //csv文件写入列头
        sw.WriteLine(head);
        string data = "";
        //csv写入数据
        for (int i = 0; i < dt.Rows.Count; i++)
        {
          string data2 = string.Empty;
          //拼接行数据
          for (int cNum1 = 0; cNum1 < dt.Columns.Count; cNum1++)
          {
            data2 = data2 + "\"" + dt.Rows[i][dt.Columns[cNum1].ColumnName].ToString() + "\",";
          }
          bool flag = data != data2;
          if (flag)
          {
            sw.WriteLine(data2);
          }
          data = data2;

        }
        string msg = "数据被成功导出到:" + filepath;
        MessageBox.Show(msg);
      }
      catch (Exception ex)
      {
        // logger.Error("导出csv失败!" + ex.Message);

        MessageBox.Show("导出失败" + ex.Message);
        return;
      }
      finally
      {
        if (sw != null)
        {
          sw.Close();
        }
        if (fs != null)
        {
          fs.Close();
        }
        sw = null;
        fs = null;
      }
    }

示例2

问题代码:

 private bool GetChannelInfo()
 {
      comCheckWindow.LoadCheckResult("准备加载项目通道信息", Color.FromName("Green"));
      XmlDocument proFile = new XmlDocument(); //读取项目配置文件
      proFile.Load(proFilePath);
      XmlNodeList channelList = proFile.SelectSingleNode("Project").ChildNodes;
      if (channelList.Count == 0) return false;
      ......
      return true;
 }

在“proFile.Load(proFilePath)”语句处发生错误,提示对路径…(proFilePath的值)的访问被拒绝。

尝试过将目标文件重新选择路径(从C盘转移到D盘),或提升程序运行权限(在以管理员身份运行Visual Studio的情况下打开项目文件),均无效。

最后检查程序时发现:路径proFilePath的值不正确,运行“proFile.Load(proFilePath)”要求路径proFilePath指向一个确定的XML文件,但此处路径的值为该XML文件所在目录的路径,由于Load函数的参数指向对象类型不匹配,从而导致出错。

上述就是小编为大家分享的使用C#无法访问路径如何解决了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI