佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1161|回复: 1

[C#] 有时找到,有时找不到文件

[复制链接]
发表于 14-3-2008 08:48 AM | 显示全部楼层 |阅读模式
我用C#  写了一个singleton class DataProviderManager 来读取一个xml 文件 , 可是在执行时(ASP.Net  application) , 有时可以读到,有时却出找不到文件, 这个问题已经困扰了我许久,不知道大家有没有遇过这样的问题。

当错误代码出现时, 我把文件放到指定的路径, 再执行, 还是同样的错误。

有想过是VS内建web server 的caching 的问题,也怀疑是Singleton class 的问题。但是还是无从下手。

里面的ReadConfigurationToDictionary()  就是问题所在, 它读取一个文件,放到Dictionary 里去。

也曾尝试过把ReadConfigurationToDictionary() 里的   providers.xml 改成 ~/providers.xml , 但是还是不行。




  1. /// <summary>
  2. /// Implementation of Singleton
  3. /// </summary>
  4. public sealed class DataProviderManager
  5. {
  6. // A Cache to keep the table & provider_name, to reduce I/O
  7. static Dictionary<string, string> dic_connection_provider = new Dictionary<string, string>();

  8. /// <summary>
  9. /// Constructor , run only once, to read the providers.xml to dictionary
  10. /// </summary>
  11. DataProviderManager()
  12. {
  13. ReadConfigurationToDictionary();
  14. }
  15. /// <summary>
  16. /// The singleton instance
  17. /// </summary>
  18. public static DataProviderManager Instance
  19. {
  20. get
  21. {
  22. return Nested.instance;
  23. }
  24. }
  25. /// <summary>
  26. /// A helper nested class to make sure singleton is thread-safe
  27. /// </summary>
  28. class Nested
  29. {
  30. static Nested()
  31. {
  32. }
  33. internal static readonly DataProviderManager instance = new DataProviderManager();
  34. }


  35. private void ReadConfigurationToDictionary()
  36. {
  37. string str_table;
  38. string str_provider;
  39. try
  40. {
  41. XmlReader reader;
  42. try
  43. {
  44. // Configuration file kept in providers.xml
  45. reader = XmlReader.Create("providers.xml");
  46. }
  47. catch (Exception ex)
  48. {
  49. throw ex;
  50. }
  51. // Read the tag <table_providers>
  52. reader.ReadStartElement("table_providers");
  53. // Repeat to read each <table_provider>, <table>,</table>, <provider>, </provider>
  54. do
  55. {
  56. reader.ReadStartElement("table_provider");
  57. reader.ReadStartElement("table");
  58. str_table = reader.ReadContentAsString();
  59. reader.ReadEndElement();
  60. reader.ReadStartElement("provider");
  61. str_provider = reader.ReadContentAsString();
  62. reader.ReadEndElement();
  63. // Add the pair <table,provider> to dictionary
  64. dic_connection_provider.Add(str_table, str_provider); // Add it to dictionary
  65. reader.ReadEndElement();
  66. } while (reader.ReadToNextSibling("table_provider"));
  67. // Read the tag </table_providers>
  68. reader.ReadEndElement();
  69. }
  70. catch (Exception ex)
  71. {
  72. throw ex;
  73. // Console.WriteLine(ex.Message);
  74. }
  75. }



复制代码

[ 本帖最后由 jangancari 于 14-3-2008 08:51 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 15-3-2008 12:35 AM | 显示全部楼层
当错误代码出现时, 我把文件放到指定的路径, 再执行, 还是同样的错误。

錯誤訊息是什么?
Could not find file '...providers.xml' ?
每次都是一模一樣的訊息?

如果有用VS 2008,建議debug進去XmlReader的源碼:
http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

[ 本帖最后由 fxam 于 15-3-2008 11:57 AM 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 1-1-2026 02:42 AM , Processed in 0.159324 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表