在资源映射Xamarin中找不到文件

本文关键字:找不到 文件 Xamarin 资源 映射 | 更新日期: 2023-09-27 18:25:37

我有一个关于Xamarin工作室的小问题。我正在制作一个连接Http服务器的应用程序。现在我要做的是将配置保存在INI文件中。这看起来像这样:

[Configuration]
IpAdress= 192.192.192.192
Port= 80

现在我想读这个文件,但是找不到我。如何使用存储在资源文件夹中的文件。

这是我用来尝试读取文件的:

IniParser iniParser = new IniParser("Resources/Configuration.ini");

此函数调用Load函数:

public bool Load(string filename)
        {
            if (File.Exists(filename))
            {
                try
                {
                    var content = File.ReadAllLines(filename);
                    _iniFileContent = new Dictionary<string, Dictionary<string, string>>();
                    string currentSectionName = string.Empty;
                    foreach (var line in content)
                    {
                        Match m = _sectionRegex.Match(line);
                        if (m.Success)
                        {
                            currentSectionName = m.Groups["SectionName"].Value;
                        }
                        else
                        {
                            m = _keyValueRegex.Match(line);
                            if (m.Success)
                            {
                                string key = m.Groups["Key"].Value;
                                string value = m.Groups["Value"].Value;
                                Dictionary<string, string> kvpList;
                                if (_iniFileContent.ContainsKey(currentSectionName))
                                {
                                    kvpList = _iniFileContent[currentSectionName];
                                }
                                else
                                {
                                    kvpList = new Dictionary<string, string>();
                                }
                                kvpList[key] = value;
                                _iniFileContent[currentSectionName] = kvpList;
                            }
                        }
                    }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

所以它再次找不到资源文件夹中的文件,我如何才能找到文件?

在资源映射Xamarin中找不到文件

你想做的是不可能的。你试图在资源文件夹中设置一个ini文件,但当你编译项目并在android设备上运行时,你无法访问该文件。这是因为该文件已编译到APK中。

你需要做的是检查文件是否存在,如果存在,如果不存在,请阅读它。这个链接可以帮助我:

http://developer.xamarin.com/recipes/ios/general/file_system/save_documents/

您可能没有在查找Resources文件夹。请查看有关Assets文件夹的答案。https://stackoverflow.com/a/19768857/1858751