在C#中使用FtpWebRequest时设置端口号

当我尝试使用VS2008作为调试器从C#代码FTP到我的Win 2008服务器时,我一直遇到exception。 我的测试类看起来像这样: public class FTP { private string ftpServerIP = “192.168.10.35:21”; private string ftpUserID = “Administrator”; private string ftpPassword = “XXXXXXXX”; private string uploadToFolder = “uploadtest”; public void Upload(string filename) { FileInfo fileInf = new FileInfo(filename); string uri = “ftp://” + ftpServerIP + “/” + uploadToFolder + “/” + fileInf.Name; FtpWebRequest reqFTP; reqFTP = […]

Canvas.GetTop()返回NaN

我有一个带有一些UIElements的Canvas。 通过动画顶部和左侧属性将我们在canvas上移动后,偶尔会调用Canvas.GetTop,导致NaN。 我没有正确地“关闭”动画吗? 这是我如何做的举动 private void InternalMove(double durationMS, FrameworkElement fElement, Point point, EventHandler callback) { _moveElement = fElement; _destination = point; Duration duration = new Duration(TimeSpan.FromMilliseconds(durationMS)); DoubleAnimation moveLeftAnimation = new DoubleAnimation(Canvas.GetLeft(fElement), point.X, duration, FillBehavior.Stop); Storyboard.SetTargetProperty(moveLeftAnimation, new PropertyPath(“(Canvas.Left)”)); DoubleAnimation moveTopAnimation = new DoubleAnimation(Canvas.GetTop(fElement), point.Y, duration, FillBehavior.Stop); Storyboard.SetTargetProperty(moveTopAnimation, new PropertyPath(“(Canvas.Top)”)); // Create a storyboard to contain the […]

如何在C#中为运行Windows 8的计算机获取唯一标识符?

我正在开发一个用C#编写的Metro应用程序,需要一种唯一识别设备的方法。 我在文档中找到了ASHWID看起来很棒。 建议的代码如下: HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null); IBuffer hardwareId = token.Id; IBuffer signature = token.Signature; IBuffer certificate = token.Certificate; 问题是,如何将IBuffer变成我可以使用的字符串?

如何检查两个字符串是否在C#中是部分匹配?

可能重复: 是否有为C#编写的模糊搜索或字符串相似性函数库? 我正在创建一个应用程序,除了用户输入歌曲或艺术家或专辑名称之外,然后将通过字符串数组或ArrayList查找任何可能的匹配。 将根据匹配百分比计算自动建议。 例如 如果用户键入链接prk,它应该找到Linkin Park或Link 80或Link Wray但匹配百分比将对所有人不同 假设该集合仅搜索艺术家集合中的艺术家姓名和歌曲集合中的歌曲名称。 (百分比数字仅供参考) Linkin Park – 98% Link Wray -82% Link 80 – 62% 解决方案不一定是C#代码,任何正则表达式或伪代码都会很好,但应该可以在C#中实现。

C#从另一个列表中更新列表

我有2个List 。 第一个,让我们称之为ListA更像是一个完整的列表,第二个ListB是一个修改过的列表。 现在我想要做的是用ListB修改ListA。 这是可行的,我该怎么做。 这是我到目前为止但不起作用: var ListB = _repository.Get(m => m.Approved == true).ToList(); foreach (var x in ListB) { ListA.Where(d => d.Name == x.Name).First() = x; } return ListA; 编辑:视觉演示描述“修改”在我的情况下意味着什么 ListA Id Name Age 1 John 14 2 Mark 15 3 Luke 13 4 Matthew 18 ListB Id Name Age 2 Mark 0 4 […]

在运行时创建类型“MyClass:OtherClass {}”?

在C#中是否可以在运行时创建一个inheritance自generics类的类型,其中基类的模板参数是正在构造的当前类? 这将编译正常: // I have this class: public class OtherClass where T : OtherClass { } // I want to create this at runtime: public class MyClass : OtherClass { } 但我不知道如何使用System.Reflection.Emit.ModuleBuilder.TypeBuilder创建MyClass : AssemblyName asn = new AssemblyName(“test.dll”); AssemblyBuilder asb = AppDomain.CurrentDomain.DefineDynamicAssembly( asn, AssemblyBuilderAccess.RunAndSave, @”D:\test_assemblies”); ModuleBuilder = modb = asb.DefineDynamicModule(“test”, “test.dll”); TypeBuilder tb = modb.DefineType( […]

为什么Select,Where和GroupBy的这种组合会导致exception?

我有一个简单的服务表结构,每个服务都有很多设施。 在数据库中,这是一个Service表和一个Facility表,其中Facility表引用了Service表中的一行。 在我们的应用程序中,我们有以下LINQ工作: Services .Where(s => s.Facilities.Any(f => f.Name == “Sample”)) .GroupBy(s => s.Type) .Select(g => new { Type = g.Key, Count = g.Count() }) 但由于我无法控制的原因,源集在Where调用之前被投射到非实体对象,这样: Services .Select(s => new { Id = s.Id, Type = s.Type, Facilities = s.Facilities }) .Where(s => s.Facilities.Any(f => f.Name == “Sample”)) .GroupBy(s => s.Type) .Select(g => new { […]

StackFrame在发布模式下表现不同

这是我的代码: public class UserPreferences { /// /// The EMail signature. /// [UserPreferenceProperty(Category = “Email”, DefaultValue = “My default value”)] public static string Signature { get { return UserPreferenceManager.GetValue(); } set { UserPreferenceManager.SetValue(value); } } } public static string GetValue() { if (((VTXPrincipal)Thread.CurrentPrincipal).VTXIdentity.OperatorID == null) { throw new Exception(“Missing Operator ID”); } string value = string.Empty; […]

PrincipalContext.ValidateCredentials始终返回FALSE

我有一个MVC应用程序需要登录并validation用户对Active Directory。 我正在使用PrincipalContext.ValidateCredentials方法,但总是获得false的身份validation。 连接到服务器很好。 问题似乎发生在ValidateCredentials 。 这是我的代码: public static bool IsAuthenticated(string domain, string username, string pwd) { bool IsAuthenticated = false; try { PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, domain, “DC=c1w,DC=com”); username = “c1w\\” + username; IsAuthenticated = insPrincipalContext.ValidateCredentials(username, pwd); } catch (Exception ex) { // Rethrow this exception ExceptionPolicy.HandleException(ex, “Exception Policy”); } return IsAuthenticated; } […]

为什么使用DllImport属性作为添加引用?

我见过几个这样的例子: [DllImport(“user32.dll”)] static extern bool TranslateMessage([In] ref Message lpMsg); [DllImport(“user32.dll”)] static extern IntPtr DispatchMessage([In] ref Message lpmsg); 但是,我不明白的是为什么有人会这样做,因为只是引用DLL就像他们做其他库一样? MSDN声明:“在托管应用程序中重用现有的非托管代码时,DllImport属性非常有用。例如,托管应用程序可能需要调用非托管WIN32 API。” 但是,是不是说引用一个非托管的dll是不行的,否则不可能?