Xamarin Visual Studio createcertfromfile 路径无效 [英] Xamarin Visual Studio createcertfromfile path no working
问题描述
我一整天都在工作,我需要使用 xamarin 在 c# 中建立与蚊子 mqtt 代理的 ssl 连接.
I have been working all day, I need to make an ssl connection to mosquito mqtt broker in c# using xamarin.
我正在尝试 CreateFromCertFile(path to cert) ......
I am trying to CreateFromCertFile(path to cert) ......
但是我尝试的所有方法都行不通.
However everything I try won't work.
文件位于 C:\Cert\MyCARoot.cer
The file is located at C:\Cert\MyCARoot.cer
我试过 CreateFromCertFile(@"c:\Cert\MyCARoot.cer");
I have tried CreateFromCertFile(@"c:\Cert\MyCARoot.cer");
和
CreateFromCertFile("c:\\Cert\\MyCARoot.cer");
CreateFromCertFile("c:\\Cert\\MyCARoot.cer");
我不断收到的错误是
System.IO.FileNotFoundException: 找不到文件/C:\Cert\MyCARoot.cer"
System.IO.FileNotFoundException: Could not find file "/C:\Cert\MyCARoot.cer"
基本上它添加了一个/"然后说路径不好.我只是不明白我读到的所有内容都说这应该有效.
Basically it adds a "/" and then says the path is no good. I just don't get it everything I read says this should work.
推荐答案
我不知道 xamarin 中的路径有什么问题,但是有一个问题.我确实找到了解决方案.
I don't know what the problem with paths in xamarin, however there is a problem. I did find a solution.
- 确保您在活动中工作.
- 在您的资产文件夹中添加您要使用的文件(右键单击添加现有)
- 实例化一个 AssetManager 类 -- AssetManager assets = this.Assets;
现在这是我的应用程序可能与其他应用程序不同的地方,因为我想要从我的资产创建一个字节 [],我可以用它来实例化 x509....2 证书类.
Now this is where my application may be different from others because I wanted a byte[] created from my asset which I could use to instantiate a x509....2 certificate class.
我使用 Stream 和 MemoryStream 来获得我想要的结果,见下文
I used Stream and MemoryStream to get my desired results see below
AssetManager assets = this.Assets;
using (Stream input = assets.Open("ca-bundle.crt"))
{
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
data = ms.ToArray();
}
}
然后我用我的新字节 [] 实例化了我的连接类,然后我有一个到我的 mosquitto mqtt 代理的 ssl 连接.
I then instantiated my connection class with my new byte [], and bing, bang, boom I had an ssl connection to my mosquitto mqtt broker.
我看到 assets.Open("fileInAsset") 的其他应用程序代替了其他操作的文件路径.
Other applications I saw that assets.Open("fileInAsset") worked in place of the path to file for other operations.
祝你好运
这篇关于Xamarin Visual Studio createcertfromfile 路径无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!