Tag: .net

是否可以读取文件?

我正在开发一个应用程序,它检查由单独的程序(不是由我编写)对文件所做的更改。 如果检测到更改,则会打开文件,读取最后一行,然后关闭文件。 我正在使用以下代码来确保我的程序不会尝试锁定文件,但只在读取模式下打开它: FileStream fs = new FileStream( _scannerFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr = new StreamReader(fs); var str = sr.ReadToEnd(); sr.Close(); fs.Close(); 不幸的是,尽管如此,每当我的程序尝试读取文件时,我仍然会收到以下错误: System.IO.IOException was unhandled Message=”The process cannot access the file ‘D:\\LSDATA\\IdText.txt’ because it is being used by another process.” Source=”mscorlib” StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess […]

在Windows窗体中切换开关控件

我正在使用CheckBox设计一个Toggle Switch CheckBox ,但是目前我的控件只画了一个圆圈。 如何绘制如下图像的圆形图形以及如何根据控件的值更改圆的位置以表示已检查和未检查的状态,如下图所示: 这是我的代码: public class MyCheckBox:CheckBox { public MyCheckBox() { this.Appearance = System.Windows.Forms.Appearance.Button; this.BackColor = Color.Transparent; this.TextAlign = ContentAlignment.MiddleCenter; this.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.FlatAppearance.BorderColor = Color.RoyalBlue; this.FlatAppearance.BorderSize = 2; } protected override void OnPaint(PaintEventArgs e) { this.OnPaintBackground(e); using (var path = new GraphicsPath()) { var c = e.Graphics.ClipBounds; var r = this.ClientRectangle; r.Inflate(-FlatAppearance.BorderSize, […]

C#中的Unicode到字符串转换

如何将Unicode值转换为其等效字符串? 例如,我有“మెశమెశ”,我需要一个接受此Unicode值并返回一个字符串的函数。 我正在查看System.Text.Encoding.Convert()函数,但不接受Unicode值; 它需要两个编码和一个字节数组。 我基本上有一个字节数组,我需要保存在字符串字段中,然后稍后返回并将字符串转换回字节数组。 所以我使用ByteConverter.GetString(byteArray)将字节数组保存到字符串中,但我无法将其恢复为字节数组。

如何检查.Post是否已在Facebook C#sdk中成功发送?

我尝试使用以下代码发送消息: //… FacebookClient fbClient = new FacebookClient(appId, appSecret); fbClient.AccessToken = “…”; string to = “user id”; dynamic result = fbClient.Post(String.Format(“{0}/feed”, to), parameters); 但我怎么知道这封文章是否已成功发送给用户? 案例成功/错误的回报是什么? 我没有在面向C#的facebook-sdk-api文档中找到一些细节。 .NET版本: v4.0.30319 Facebook大会版: 5.2.1.0 提前致谢。

应用基于多维数组的LINQfilter

给定entity framework查询,例如 var query = (from property in _dbContext.Properties join location in _db.Locations on property.Id equals location.PropertyId select new PropertyDetail { Url = property.Url, Type = property.Type, Title = property.Title, Continent = location.Continent, Country = location.Country, State = location.State, }); 我已应用filter,例如: if (!string.IsNullOrWhitespace(searchFilters.Type)) { query = query.Where(model => model.Type == searchFilters.Type); } if (!string.IsNullOrWhitespace(searchFilters.Title)) { […]

使用GUID是一种生成随机字符串和数字的有效方法吗?

可能重复: 随机是System.Guid.NewGuid()? 基于这个问题,我想知道是否使用GUID生成随机字符串和数字有任何缺陷吗? 因此,例如,如果我想要一个随机字符串和32个或更少字符的数字,我可以使用以下C#代码: string s = Guid.NewGuid().ToString().Replace(“-“, “”); 如果长度需要小于32,我将截断字符串,如果需要更长,我会将多个GUID一起添加。 这种方法有哪些缺陷? 在我写完之后,我意识到一个缺陷就是它只会有字母a到f所以我会修改这个问题: 这是一个真正随机的6个字符和10个数字的序列吗?

从大文件中读取JSON对象

我正在寻找一个JSON Parser,它允许我从大型JSON文件(大小几百MB)中迭代JSON对象。 我从Json.NET尝试过JsonTextReader,如下所示: JsonTextReader reader = new JsonTextReader(new StringReader(json)); while (reader.Read()) { if (reader.Value != null) Console.WriteLine(“Token: {0}, Value: {1}”, reader.TokenType, reader.Value); else Console.WriteLine(“Token: {0}”, reader.TokenType); } 但它会在令牌之后返回令牌。 如果我需要整个对象而不是令牌,有没有更简单的方法?

C#MySQL连接池

我正在使用C# multi threading应用程序并使用MySQL与整个应用程序的单一连接。 但是当两个或多个线程同时尝试访问数据库时,我得到以下错误: 已经有一个与此Connection关联的开放DataReader ,必须先关闭它。 我的连接代码如下 public static _connectionSetup = new MySqlConnection(“Server=server ; Database=database;User ID=user;Password=pass;Pooling=true;”); 当我需要使用连接我使用下面的代码: – using (MySqlConnection connection =_connectionSetup ) { using (MySqlCommand command = new MySqlCommand(“proc”, connection)) { …. } } 我尝试使用pooling=true ,我已经为两个不同的线程创建了两个独立的连接,但我仍然遇到上述错误。 我错过了什么吗? 如何实现连接池,以便所有线程都使用单独的连接,不会导致任何问题?

EF 4.1 / Linq-to-SQL:什么是更好的:使用Equals或==?

我们正在讨论在LINQ查询中对int比较使用Equals或== 。 我们正在使用EF4.1 Code First。 什么是更好的? var query = context.Boodschappen .Where(b => b.BoodschapID == id).FirstOrDefault(); 要么: var query = context.Boodschappen .Where(b => b.BoodschapID.Equals(id)).FirstOrDefault(); 为什么 ?

从网络摄像头拍摄照片c#

我正在使用DirectShowLib将video流式传输到我的Window窗体。 当我按下“保存”按钮时,我也想保存图像。 如何使用DirectShowLib方法或WIN32 API方法从网络摄像头获取图片?