我有一些代码使用FileSystemWatcher来监视我的应用程序之外的文件更改.
在Windows 7上,使用.NET 4,下面的代码将检测文件何时编辑并保存在记事本等应用程序中,同时我的应用程序正在运行.但是,这种逻辑在Windows 8上使用.NET 4无效.具体来说,FileSystemWatcher的Changed事件永远不会触发.
public static void Main(string[] args)
{
const string FilePath = @"C:\users\craig\desktop\notes.txt";
if (File.Exists(FilePath))
{
Console.WriteLine("Test file exists.");
}
var fsw = new FileSystemWatcher();
fsw.NotifyFilter = NotifyFilters.Attributes;
fsw.Path = Path.GetDirectoryName(FilePath);
fsw.Filter = Path.GetFileName(FilePath);
fsw.Changed += OnFileChanged;
fsw.EnableRaisingEvents = true;
// Block exiting.
Console.ReadLine();
}
private static void OnFileChanged(object sender, FileSystemEventArgs e)
{
if (File.Exists(e.FullPath))
{
Console.WriteLine("File change reported!");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以改变NotifyFilter以包含NotifyFilters.LastWrite,它可以解决我的问题.但是,我想了解为什么此代码在Windows 7上有效但现在无法在Windows 8上触发Changed事件.我也很想知道在Windows 8中运行时是否有办法恢复我的Windows 7 FileSystemWatcher行为(不更改NotifyFilter).
遵循MS准则,我的WPF应用程序的App构造函数包含以下代码以实现正确的焦点行为:
HwndSource.DefaultAcquireHwndFocusInMenuMode = false;
Keyboard.DefaultRestoreFocusMode = RestoreFocusMode.None;
Run Code Online (Sandbox Code Playgroud)
如本文所述,这些设置可防止焦点窃取.
但是,将DefaultRestoreFocusMode设置为None会产生不良副作用.当使用Alt + Tab离开WPF应用程序然后返回它时,WPF应用程序不会获得焦点.但是,如果我没有将DefaultRestoreFocusMode设置为none,它会按预期获得焦点.有没有办法防止焦点窃取,但在通过Alt + Tab返回WPF应用程序时仍然设置焦点?
-Craig
我有一个引用.NET 4.0程序集的WPF/C#应用程序.但是,在应用程序中是一个文本编辑器,需要显示与.NET 3.5程序集绑定的C#intellisense.因此,我希望能够在运行时加载适当的.NET 3.5程序集.但是,当我尝试使用时:
Assembly.Load()
Assembly.LoadFile()
Assembly.LoadFrom()
Run Code Online (Sandbox Code Playgroud)
我总是从GAC获得最新版本.阅读他们的MSDN页面,看起来不幸的是设计.
我试着在这个堆栈溢出帖子中遵循提出的解决方案.但是,链接的网页解决方案不起作用,远程处理过程似乎有点矫枉过正.有没有人知道在运行时加载旧.NET程序集的更好和/或更简单的方法?
我有一个绑定的形式:
Path=SpecialCollection[0]
Run Code Online (Sandbox Code Playgroud)
SpecialCollection类扩展了ObservableCollection并具有索引器属性.
public T this[string propertyValue]
{
get
{
// do stuff
return default(T);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是绑定尝试获取索引器属性值,而不是返回集合中的第0项.有没有办法强制绑定将0视为一个整数,因此它返回一个集合元素,而不是调用集合的索引器属性的getter?
我正在使用C#/ .NET反序列化类似于此的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book
Title="Animal Farm"
>
<Thing1>""</Thing1>
<Thing2>""</Thing2>
<Thing3>""</Thing3>
...
<ThingN>""</ThingN>
</Book>
... More Book nodes ...
</Books>
Run Code Online (Sandbox Code Playgroud)
对于反序列化的XML,我的类看起来像:
[XmlRoot("Books")]
public class BookList
{
// Other code removed for compactness.
[XmlElement("Book")]
public List<Book> Books { get; set; }
}
public class Book
{
// Other code removed for compactness.
[XmlAttribute("Title")]
public string Title { get; set; }
[XmlAnyElement()]
public List<XmlElement> ThingElements { get; set; }
public List<Thing> Things { get; set; }
}
public class …Run Code Online (Sandbox Code Playgroud) 目标:我有一个具有以下格式的 JSON 有效负载:
{
"Values": [
{
"Details": {
"14342": {
"2016-06-07T00:00:00": {
"Value": 99.62,
"Count": 7186
},
"2016-06-08T00:00:00": {
"Value": 99.73,
"Count": 7492
}
},
"14362": {
"2016-06-07T00:00:00": {
"Value": 97.55,
"Count": 1879
},
"2016-06-08T00:00:00": {
"Value": 92.68,
"Count": 355
}
}
},
"Key": "query5570027",
"Total": 0.0
},
{
"Details": {
"14342": {
"2016-06-07T00:00:00": {
"Value": 0.0,
"Count": 1018
},
"2016-06-08T00:00:00": {
"Value": 0.0,
"Count": 1227
}
}
},
"Key": "query4004194",
"Total": 0.0
}
],
"LatencyInMinute": 0.0
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Windows API代码包中的CommonOpenFileDialog作为文件夹选择器对话框.我将InitialDirectory属性设置为Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).但是,当我显示对话框时,地址栏中的路径是Libraries\Documents(而不是我期望的C:\ users\craig\my documents).此外,如果我只按"选择文件夹"按钮,我会看到一个对话框,说"您已经选择了一个库".请改为选择一个文件夹.
有人知道为什么我的文件路径被忽略,支持'libraries\documents'吗?更重要的是,如何让对话框尊重我传入的InitialDirectory值?
我用于对话框的代码是:
if (CommonFileDialog.IsPlatformSupported)
{
var folderSelectorDialog = new CommonOpenFileDialog();
folderSelectorDialog.EnsureReadOnly = true;
folderSelectorDialog.IsFolderPicker = true;
folderSelectorDialog.AllowNonFileSystemItems = false;
folderSelectorDialog.Multiselect = false;
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folderSelectorDialog.Title = "Project Location";
if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
ShellContainer shellContainer = null;
try
{
// Try to get a valid selected item
shellContainer = folderSelectorDialog.FileAsShellObject as ShellContainer;
}
catch
{
MessageBox.Show("Could not create a ShellObject from the selected item");
}
FilePath = shellContainer != null ? shellContainer.ParsingName : string.Empty;
} …Run Code Online (Sandbox Code Playgroud) 我有一个C#控制台应用程序,代码如下:
using (var cn = new SqlConnection())
{
cn.ConnectionString = "Data Source=localhost;Integrated Security=True;Persist Security Info=False";
cn.Open();
System.Console.WriteLine("past open");
}
System.Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
运行时,我设置了两个断点.一个在WriteLine,另一个在ReadLine.我也打开了Sql Server Management Studio(SSMS),正在观看Activity Monitor的进程列表.
当我运行应用程序并点击我的第一个断点(在WriteLine上)时,我很快就会在SSMS进程列表中看到一个进程,其应用程序名称为".Net SqlClient Data Provider",其数据库为"master".
当我F5到第二个断点时,我的期望是该进程将从进程列表中删除,因为我已经关闭了我的SqlConnection.但是,在应用程序关闭之前,该过程仍保留在列表中.
我的假设是该进程仍然在列表中,因为我的连接并未真正关闭.如果我的假设是准确的,那么我还需要做些什么来关闭连接?如果我的假设不准确,那么为什么我的流程仍然列出,如何测试我的连接是否实际正确关闭?
谢谢.
我已经看到PowerQuery表达式可以从日期时间中减去天数,比如DateTimeZone.UtcNow.AddDays(-1).但是,我想从DateTime中减去小时,而不是几天.
由于我没有在Power Query规范中看到类似AddHours()的函数,是否有一种强大的方法可以追溯到几个小时?例如,DateTimeZone.UtcNow + #duration(0,-1,0,0)之类的东西会给我一个提前一小时的日期时间吗?
[注意:我自己试试,但我无法在我的本地盒子上安装Power Query,我找不到独立的编辑器来试试,因此社区要求]
下面是我正在处理的MSBuild文件的一部分:
<ItemGroup>
<Tests Include="$(SolutionDir)\**\bin\$(TestPlatform)\$(Configuration)\*.Tests.dll" />
</ItemGroup>
<PropertyGroup>
<TestProperties>/testcontainer:%(Tests.FullPath)</TestProperties>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
我想拥有一个包含命令行开关的属性.但是,当我尝试$(TestProperties)在Exec命令字符串中使用时,%(Tests.FullPath)永远不会解析为测试项的绝对路径.相反,它总是按字面意思处理,如"%(Tests.FullPath)".
我做错了什么或这是一个标准的MSBuild行为?如果是后者,我有办法解决这个问题吗?
谢谢
PS - 我意识到我可能不需要访问FullPath属性,因为我的Include值是绝对路径.但是,我仍然想了解这个问题,以及如何处理它.