|
查看: 1161|回复: 1
|
[C#] 有时找到,有时找不到文件
[复制链接]
|
|
|
我用C# 写了一个singleton class DataProviderManager 来读取一个xml 文件 , 可是在执行时(ASP.Net application) , 有时可以读到,有时却出找不到文件, 这个问题已经困扰了我许久,不知道大家有没有遇过这样的问题。
当错误代码出现时, 我把文件放到指定的路径, 再执行, 还是同样的错误。
有想过是VS内建web server 的caching 的问题,也怀疑是Singleton class 的问题。但是还是无从下手。
里面的ReadConfigurationToDictionary() 就是问题所在, 它读取一个文件,放到Dictionary 里去。
也曾尝试过把ReadConfigurationToDictionary() 里的 providers.xml 改成 ~/providers.xml , 但是还是不行。
-
- /// <summary>
- /// Implementation of Singleton
- /// </summary>
- public sealed class DataProviderManager
- {
- // A Cache to keep the table & provider_name, to reduce I/O
- static Dictionary<string, string> dic_connection_provider = new Dictionary<string, string>();
- /// <summary>
- /// Constructor , run only once, to read the providers.xml to dictionary
- /// </summary>
- DataProviderManager()
- {
- ReadConfigurationToDictionary();
- }
- /// <summary>
- /// The singleton instance
- /// </summary>
- public static DataProviderManager Instance
- {
- get
- {
- return Nested.instance;
- }
- }
- /// <summary>
- /// A helper nested class to make sure singleton is thread-safe
- /// </summary>
- class Nested
- {
- static Nested()
- {
- }
- internal static readonly DataProviderManager instance = new DataProviderManager();
- }
-
-
- private void ReadConfigurationToDictionary()
- {
- string str_table;
- string str_provider;
- try
- {
- XmlReader reader;
- try
- {
- // Configuration file kept in providers.xml
- reader = XmlReader.Create("providers.xml");
- }
- catch (Exception ex)
- {
- throw ex;
- }
- // Read the tag <table_providers>
- reader.ReadStartElement("table_providers");
- // Repeat to read each <table_provider>, <table>,</table>, <provider>, </provider>
- do
- {
- reader.ReadStartElement("table_provider");
- reader.ReadStartElement("table");
- str_table = reader.ReadContentAsString();
- reader.ReadEndElement();
- reader.ReadStartElement("provider");
- str_provider = reader.ReadContentAsString();
- reader.ReadEndElement();
- // Add the pair <table,provider> to dictionary
- dic_connection_provider.Add(str_table, str_provider); // Add it to dictionary
- reader.ReadEndElement();
- } while (reader.ReadToNextSibling("table_provider"));
- // Read the tag </table_providers>
- reader.ReadEndElement();
- }
- catch (Exception ex)
- {
- throw ex;
- // Console.WriteLine(ex.Message);
- }
- }
-
-
-
复制代码
[ 本帖最后由 jangancari 于 14-3-2008 08:51 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 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 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|