小编Hom*_*mam的帖子

如何在不锁定的情况下读取文本文件?

我有一个Windows服务以简单的格式将其日志写入文本文件.

现在,我将创建一个小应用程序来读取服务的日志,并将现有日志和添加的日志显示为实时视图.

问题是服务锁定文本文件以添加新行,同时查看器应用程序锁定文件以供读取.

服务代码:

void WriteInLog(string logFilePath, data)
{
    File.AppendAllText(logFilePath, 
                       string.Format("{0} : {1}\r\n", DateTime.Now, data));
}
Run Code Online (Sandbox Code Playgroud)

观众代码:

int index = 0;
private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                using (StreamReader sr = new StreamReader(logFilePath))
                {
                    while (sr.Peek() >= 0)  // reading the old data
                    {
                        AddLineToGrid(sr.ReadLine());
                        index++;
                    }
                    sr.Close();
                }

                timer1.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


private void timer1_Tick(object sender, EventArgs e)
        {
            using (StreamReader sr = new StreamReader(logFilePath))
            {
                // skipping the old data, …
Run Code Online (Sandbox Code Playgroud)

c# file-io

84
推荐指数
5
解决办法
7万
查看次数

如何使用LINQ过滤字典并将其返回到相同类型的字典

我有以下字典:

Dictionary<int,string> dic = new Dictionary<int,string>();
dic[1] = "A";
dic[2] = "B";
Run Code Online (Sandbox Code Playgroud)

我想过滤字典的项目并将结果重新分配给同一个变量:

dic = dic.Where (p => p.Key == 1);
Run Code Online (Sandbox Code Playgroud)

如何将结果作为字典从同一类型[ <int,string>]返回?

我试过ToDictionary,但它不起作用.

提前致谢.

c# linq linq-to-objects dictionary

76
推荐指数
1
解决办法
6万
查看次数

如何以编程方式选择ListView中的项目?

我试图以编程方式选择ListView中的第一个项目,但它似乎没有被选中.我使用以下代码:

if (listView1.Items.Count > 0)
    listView1.Items[0].Selected = true;
Run Code Online (Sandbox Code Playgroud)

其实我之前遇到过这个问题,但我不记得我是怎么解决它的!

.net c# listview selection winforms

61
推荐指数
4
解决办法
15万
查看次数

编写内联事件处理程序是不好的做法

编写内联事件处理程序是不好的做法吗?

对我来说,当我想在事件处理程序中使用局部变量时,我更喜欢使用它,如下所示:

我更喜欢这个:

// This is just a sample
private void Foo()
{
    Timer timer = new Timer() { Interval = 1000 };
    int counter = 0; // counter has just this mission
    timer.Tick += (s, e) => myTextBox.Text = (counter++).ToString();
    timer.Start();
}
Run Code Online (Sandbox Code Playgroud)

而不是这个:

int counter = 0; // No need for this out of Boo & the event handler

private void Boo()
{
    Timer timer = new Timer() { Interval = 1000 };

    timer.Tick += timer_Tick;
    timer.Start();
}

void …
Run Code Online (Sandbox Code Playgroud)

c# event-handling inline-code

58
推荐指数
1
解决办法
4万
查看次数

JQuery是否支持Dictionaries(key,value)集合?

JQuery是否支持Dictionaries(键,值)集合?

我想在结构中设置以下数据

[1, false]
[2, true]
[3, false]
Run Code Online (Sandbox Code Playgroud)

具有添加,查找,删除和更新的功能.

任何帮助!

javascript collections jquery dictionary

55
推荐指数
2
解决办法
7万
查看次数

为什么C#允许在浮点类型中将非零数除以零?

为什么C#允许:

1.0 / 0 // Infinity
Run Code Online (Sandbox Code Playgroud)

并且不允许:

1 / 0 // Division by constant zero [Compile time error]
Run Code Online (Sandbox Code Playgroud)

在数学上,积分和浮点数除以零之间是否有任何差异?

c# floating-point divide-by-zero

51
推荐指数
4
解决办法
2万
查看次数

计算IEnumerable的计数(非通用)

任何人都可以帮助我(非通用接口)的Count扩展方法IEnumerable.

我知道LINQ不支持它但是如何手动编写它?

.net c# linq ienumerable extension-methods

46
推荐指数
2
解决办法
4万
查看次数

为什么(int)(对象)10m抛出"指定的强制转换无效"异常?

为什么这个显式转换会抛出Specified cast is not valid.异常?

decimal d = 10m;
object o = d;
int x = (int)o;
Run Code Online (Sandbox Code Playgroud)

但这有效:

int x = (int)(decimal)o;
Run Code Online (Sandbox Code Playgroud)

c# casting

39
推荐指数
3
解决办法
2万
查看次数

我可以在Action或Func委托中使用params吗?

当我试图在Action委托中使用params时......

private Action<string, params object[]> WriteToLogCallBack;
Run Code Online (Sandbox Code Playgroud)

我收到了这个设计时错误:

类,结构或接口成员声明中的标记'params'无效

任何帮助!

c# delegates action params-keyword

39
推荐指数
3
解决办法
2万
查看次数

如何将枚举绑定到组合框

我会使用组合框控件绑定枚举的值.

我写了这段代码:

cboPriorLogicalOperator.DataSource = Enum.GetValues(typeof(MyEnum))
    .Cast<MyEnum>()
    .Select(p => new { Key = (int)p, Value = p.ToString() })
    .ToList();

myComboBox.DisplayMember = "Value";
myComboBox.ValueMember = "Key";
Run Code Online (Sandbox Code Playgroud)

它运作良好,但我想知道是否有一个更简单的方法.

c# winforms

39
推荐指数
2
解决办法
2万
查看次数