Questions tagged «c#»

C#(发音为“ See Sharp”)是由Microsoft开发的一种高级,静态类型的多范例编程语言。C#代码通常针对Microsoft的.NET系列工具和运行时,其中包括.NET Framework,.NET Core和Xamarin。使用此标记可解决有关用C#或C#正式规范编写的代码的问题。

1
使用BookSleeve的ConnectionUtils.Connect()将SignalR与Redis消息总线故障转移结合使用
我正在尝试使用SignalR应用程序创建Redis消息总线故障转移方案。 最初,我们尝试了一个简单的硬件负载平衡器故障转移,该故障转移仅监视两个Redis服务器。SignalR应用程序指向单个HLB端点。然后,我使一台服务器发生故障,但无法在不回收SignalR应用程序池的情况下成功通过第二台Redis服务器获得任何消息。大概是因为它需要向新的Redis消息总线发出安装命令。 从SignalR RC1开始,Microsoft.AspNet.SignalR.Redis.RedisMessageBus使用Booksleeve's RedisConnection()连接到用于发布/ 订阅的单个Redis。 我创建了一个新类,该类RedisMessageBusCluster()使用Booksleeve's ConnectionUtils.Connect()连接到Redis服务器集群中的一个。 using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using BookSleeve; using Microsoft.AspNet.SignalR.Infrastructure; namespace Microsoft.AspNet.SignalR.Redis { /// <summary> /// WIP: Getting scaleout for Redis working /// </summary> public class RedisMessageBusCluster : ScaleoutMessageBus { private readonly int _db; private readonly string[] _keys; …

7
如何一次性删除通用列表中的所有null元素?
是否在.Net中为C#定义了默认方法,以删除列表中的所有元素null? List<EmailParameterClass> parameterList = new List<EmailParameterClass>{param1, param2, param3...}; 假设一些参数是null; 我无法事先知道,我想将其从列表中删除,以便仅包含不为null的参数。
112 c#  list  null  element 

4
在.NET中将对象序列化为UTF-8 XML
为了简洁起见,删除了正确的对象处理方法,但是如果这是将对象编码为内存中的UTF-8的最简单方法,我会感到震惊。必须有一种更简单的方法吗? var serializer = new XmlSerializer(typeof(SomeSerializableObject)); var memoryStream = new MemoryStream(); var streamWriter = new StreamWriter(memoryStream, System.Text.Encoding.UTF8); serializer.Serialize(streamWriter, entry); memoryStream.Seek(0, SeekOrigin.Begin); var streamReader = new StreamReader(memoryStream, System.Text.Encoding.UTF8); var utf8EncodedXml = streamReader.ReadToEnd();



9
如何检查文件夹中是否存在文件?
我需要检查文件夹中是否存在xml文件。 DirectoryInfo di = new DirectoryInfo(ProcessingDirectory); FileInfo[] TXTFiles = di.GetFiles("*.xml"); if (TXTFiles.Length == 0) { log.Info("no files present") } 这是检查文件夹中文件是否存在的最佳方法。 我只需要检查一个xml文件是否存在
112 c#  xml  file  fileinfo 

9
ASP Net Web API 2.1获取客户端IP地址
您好,我需要在Web api中获取请求某种方法的客户端IP,我尝试从此处使用此代码,但它始终返回服务器本地IP,如何以正确的方式获取? HttpContext.Current.Request.UserHostAddress; 来自其他问题: public static class HttpRequestMessageExtensions { private const string HttpContext = "MS_HttpContext"; private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; public static string GetClientIpAddress(this HttpRequestMessage request) { if (request.Properties.ContainsKey(HttpContext)) { dynamic ctx = request.Properties[HttpContext]; if (ctx != null) { return ctx.Request.UserHostAddress; } } if (request.Properties.ContainsKey(RemoteEndpointMessage)) { dynamic remoteEndpoint = …


4
使用字典作为数据源的C#DropDownList
我想设置DataTextField和DataValueField一个中Dropdownlist使用的词典(名单)(languageList)languageCod(EN-GB)的关键和语言名称(英文)作为文本显示。 相关代码: string[] languageCodsList= service.LanguagesAvailable(); Dictionary<string, string> list = new Dictionary<string, string>(languageCodsList.Length); foreach (string cod in languageCodsList) { CultureInfo cul = new CultureInfo(cod); list.Add(cod, cul.DisplayName); } languageList.DataSource = list; languageList.DataBind(); 如何设置DataTextField和DataValueField?


4
MVC3 DropDownListFor-一个简单的例子?
我DropDownListFor在MVC3应用中遇到问题。我能够使用StackOverflow弄清楚如何使它们出现在视图上,但是现在我不知道如何在提交视图模型时捕获其相应属性中的值。为了使它起作用,我必须创建一个具有ID和value属性的内部类,然后必须使用an IEnumerable<Contrib>来满足DropDownListFor参数要求。但是,现在,MVC FW应该如何将在此下拉列表中选择的值映射回我的视图模型的simple string属性中? public class MyViewModelClass { public class Contrib { public int ContribId { get; set; } public string Value { get; set; } } public IEnumerable<Contrib> ContribTypeOptions = new List<Contrib> { new Contrib {ContribId = 0, Value = "Payroll Deduction"}, new Contrib {ContribId = 1, Value = …

3
C#HttpWebRequest与WebRequest
我看到了这段代码: var request = (HttpWebRequest) WebRequest.Create("http://www.google.com"); 为什么需要投射(HttpWebRequest)?为什么不只是使用HttpWebRequest.Create?为何HttpWebRequest.Create制作一个WebRequest而不是一个HttpWebRequest?
112 c#  httpwebrequest 

6
在C#中访问Imap [关闭]
从目前的情况来看,这个问题不适合我们的问答形式。我们希望答案会得到事实,参考或专业知识的支持,但是这个问题可能会引起辩论,争论,民意测验或进一步的讨论。如果您认为此问题可以解决并且可以重新提出,请访问帮助中心以获取指导。 7年前关闭。 是否存在内置方法来访问C#中的Imap服务器(使用SSL),还是有一个不错的免费库?
112 c#  imap 

10
WPF应用程序没有输出到控制台吗?
我从一个非常简单的WPF测试应用程序中使用Console.WriteLine(),但是当我从命令行执行该应用程序时,却看不到任何内容写入控制台。有人知道这里可能会发生什么吗? 我可以通过在VS 2008中创建WPF应用程序并在要执行的任何位置简单地添加Console.WriteLine(“ text”)来重现它。有任何想法吗? 我现在所需要的只是与Console.WriteLine()一样简单的东西。我意识到我可以使用log4net或其他日志记录解决方案,但是我确实不需要此应用程序那么多的功能。 编辑:我应该记得Console.WriteLine()是用于控制台应用程序的。哦,没有愚蠢的问题,对吧?:-)我现在只使用System.Diagnostics.Trace.WriteLine()和DebugView。
112 c#  .net  wpf  console 

4
如何正确发出http Web GET请求
我仍然对C#还是陌生的,并且我正在尝试为此页面创建一个应用程序,当我收到通知(回答,评论等)时,它将告诉我。但是现在,我只是尝试对api进行简单调用,以获取用户的数据。 我正在使用Visual Studio Express 2012构建C#应用程序,在此(现在)输入用户ID,因此该应用程序将使用用户ID进行请求并显示此用户ID的统计信息。 这是我试图发出请求的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //Request library using System.Net; using System.IO; namespace TestApplication { class Connect { public string id; public string type; protected string api = "https://api.stackexchange.com/2.2/"; protected string options = "?order=desc&sort=name&site=stackoverflow"; public string request() { string totalUrl …
112 c#  .net  httpwebrequest 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.