开发者

C# Check If Text File Has Content

开发者 https://www.devze.com 2023-02-10 08:22 出处:网络
I am building a C# WPF Browser App (my C# skills are quite rusty). I have 开发者_Python百科a button that I want to have change color depending on if a text document has anything in it. IE: Color is G

I am building a C# WPF Browser App (my C# skills are quite rusty).

I have 开发者_Python百科a button that I want to have change color depending on if a text document has anything in it. IE: Color is Green if there is any text in it, or Red if it is empty.

Can someone please push me off in the right direction. Thank you.


Take a look at System.IO.FileInfo

FileInfo f = new FileInfo( "<file path>" );
if( f.Length > 0 ) 
  // Color button green
else 
  // Color button red

Note that if you keep f around and plan to check it again later, you will have to call f.Refresh() to ensure it has the latest information.


Clearly I am very late on this one, but my answer turned into a big blog post.

Here is a full solution using FileSystemWatcher and all the WPF bells and whistles

Hopefully you get some use out of it.


button.Color = (new FileInfo("foo.bar")).Length == 0 ? Color.Red : Color.Green;
0

精彩评论

暂无评论...
验证码 换一张
取 消