我有一个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) 我有以下字典:
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,但它不起作用.
提前致谢.
我试图以编程方式选择ListView中的第一个项目,但它似乎没有被选中.我使用以下代码:
if (listView1.Items.Count > 0)
listView1.Items[0].Selected = true;
Run Code Online (Sandbox Code Playgroud)
其实我之前遇到过这个问题,但我不记得我是怎么解决它的!
编写内联事件处理程序是不好的做法吗?
对我来说,当我想在事件处理程序中使用局部变量时,我更喜欢使用它,如下所示:
我更喜欢这个:
// 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) JQuery是否支持Dictionaries(键,值)集合?
我想在结构中设置以下数据
[1, false]
[2, true]
[3, false]
Run Code Online (Sandbox Code Playgroud)
具有添加,查找,删除和更新的功能.
任何帮助!
为什么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)
在数学上,积分和浮点数除以零之间是否有任何差异?
任何人都可以帮助我(非通用接口)的Count扩展方法IEnumerable.
我知道LINQ不支持它但是如何手动编写它?
为什么这个显式转换会抛出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) 当我试图在Action委托中使用params时......
private Action<string, params object[]> WriteToLogCallBack;
Run Code Online (Sandbox Code Playgroud)
我收到了这个设计时错误:
类,结构或接口成员声明中的标记'params'无效
任何帮助!
我会使用组合框控件绑定枚举的值.
我写了这段代码:
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# ×9
.net ×2
dictionary ×2
linq ×2
winforms ×2
action ×1
casting ×1
collections ×1
delegates ×1
file-io ×1
ienumerable ×1
inline-code ×1
javascript ×1
jquery ×1
listview ×1
selection ×1