在我的.NET 2.0应用程序中,我需要检查是否有足够的权限来创建和写入目录的文件.为此,我有以下函数尝试创建一个文件并向其写入一个字节,然后删除自己以测试权限是否存在.
我认为检查的最佳方法是实际尝试并执行此操作,捕获发生的任何异常.虽然我对一般的异常捕获并不是特别高兴,所以有更好的或者更可接受的方式吗?
private const string TEMP_FILE = "\\tempFile.tmp";
/// <summary>
/// Checks the ability to create and write to a file in the supplied directory.
/// </summary>
/// <param name="directory">String representing the directory path to check.</param>
/// <returns>True if successful; otherwise false.</returns>
private static bool CheckDirectoryAccess(string directory)
{
bool success = false;
string fullPath = directory + TEMP_FILE;
if (Directory.Exists(directory))
{
try
{
using (FileStream fs = new FileStream(fullPath, FileMode.CreateNew,
FileAccess.Write))
{
fs.WriteByte(0xff);
}
if (File.Exists(fullPath))
{
File.Delete(fullPath);
success …Run Code Online (Sandbox Code Playgroud) DTR/DSR和RTS/CTS硬件流控制有什么区别?什么时候使用?为什么我们需要不止一种硬件流控制?:)
在.NET中,似乎有几种方法可以获取当前的Windows用户名.其中三个是:
string name = WindowsIdentity.GetCurrent().Name;
Run Code Online (Sandbox Code Playgroud)
要么
string name = Thread.CurrentPrincipal.Identity.Name;
Run Code Online (Sandbox Code Playgroud)
要么
string name = Environment.UserName;
Run Code Online (Sandbox Code Playgroud)
有什么区别,为什么选择一种方法而不是另一种?还有其他方法吗?
我知道这很懒,但有没有什么办法在Visual C#2010 Express上自动生成接口实现?(我不是说在运行时但是在设计时,就像代码片段一样).也许有第三方实用程序?
我有一个类的BindingList <>设置为BindingSource的DataSource属性,该属性又设置为DataGridView的DataSource属性.
1.我的理解是,对列表的任何添加都将触发ListChanged事件,该事件将通过BindingSource传播,然后传播到DataGridView,DataGridView将更新自身以显示更改.这将发生,因为事件已自动连接.(是?)
当所有工作都在UI线程上完成时,这一切都很好,但是当从非UI线程创建和更改列表时,最终在更新网格时会发生跨线程异常.我能理解为什么会这样,但是没有办法解决这个问题......
2.我很难理解,我应该在哪里最好拦截ListChanged事件来尝试将内容整理到UI线程中?我猜我需要一个UI线程的引用以某种方式帮助做到这一点?
我已经阅读了很多这方面的帖子/文章,但我很难挣扎,因为我不完全理解这里的工作机制.
我们永远不会在列表中更改任何项目,只添加它们,并最初清除列表.
(我使用的是.NET 2.0)
我试图使用.NET2.0 SerialPort的.BaseStream属性来进行异步读写(BeginWrite/EndWrite,BeginRead/EndRead).
我在这方面取得了一些成功,但过了一段时间后,我注意到(使用Process Explorer)应用程序正在使用的句柄逐渐增加,偶尔会增加一个额外的线程,这也增加了句柄数.
每次出现新线程时,上下文切换速率也会增加.
应用程序不断向PLC设备发送3个字节,并返回800个左右的字节,并以波特率57600进行.
最初的CSwitch Delta(再次来自Process Explorer)大约是2500,无论如何它似乎都很高.每次出现新线程时,此值都会增加,CPU负载也会相应增加.
我希望有人可能做过类似的事情,可以帮助我,甚至说'以上帝的名义,不要这样做.'
在下面的代码中,'this._stream'是从SerialPort.BaseStream获得的,而CommsResponse是我用作IAsyncresult状态对象的类.
这个代码对于我作为替代使用串口的TCP连接是常见的(我有一个CommsChannel基类,从它派生的串行和TCP通道)并且它没有这些问题所以我有理由充满希望CommsResponse类没有任何问题.
感激地收到任何评论.
/// <summary>
/// Write byte data to the channel.
/// </summary>
/// <param name="bytes">The byte array to write.</param>
private void Write(byte[] bytes)
{
try
{
// Write the data to the port asynchronously.
this._stream.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback(this.WriteCallback), null);
}
catch (IOException ex)
{
// Do stuff.
}
catch (ObjectDisposedException ex)
{
// Do stuff.
}
}
/// <summary>
/// Asynchronous write callback operation.
/// </summary>
private …Run Code Online (Sandbox Code Playgroud) 假设我有一个通过.ShowDialog()方法打开的表单.
在某些时候,我将一些事件处理程序附加到窗体上的某些控件.
例如
// Attach radio button event handlers.
this.rbLevel1.Click += new EventHandler(this.RadioButton_CheckedChanged);
this.rbLevel2.Click += new EventHandler(this.RadioButton_CheckedChanged);
this.rbLevel3.Click += new EventHandler(this.RadioButton_CheckedChanged);
Run Code Online (Sandbox Code Playgroud)
当表单关闭时,我需要删除这些处理程序,对吧?
目前,我在触发FormClosing事件时执行此操作.
例如
private void Foo_FormClosing(object sender, FormClosingEventArgs e)
{
// Detach radio button event handlers.
this.rbLevel1.Click -= new EventHandler(this.RadioButton_CheckedChanged);
this.rbLevel2.Click -= new EventHandler(this.RadioButton_CheckedChanged);
this.rbLevel3.Click -= new EventHandler(this.RadioButton_CheckedChanged);
}
Run Code Online (Sandbox Code Playgroud)
但是,我已经看到一些在Dispose()方法中删除处理程序的示例.
这样做有"最佳实践"方式吗?
(使用C#,Winforms,.NET 2.0)
谢谢.
我正在使用Visual C#Express 2008在我的XP开发机器上开发一个C#WinForms应用程序.
我将表单设置为我喜欢的尺寸,设计师的宽度和高度都很好看.我还将这些维度设置为MaximumSize属性.
将应用程序部署到另一台XP计算机,该应用程序看起来就像我的开发人员.
但是,在Win7机器上测试应用程序时,表单同时应用了水平和垂直滚动条.我认为这是由于Win7确定的表单的非客户端大小的改变.我可以调整窗口大小,但我希望它能够正确显示.
所以,我的问题是:在OS'es中正确维护表单大小客户区的最佳方法是什么?
谢谢大家.
我有一个BindingList <>对象,设置为BindingSource的DataSource.这被设置为DataGridView的DataSource.
我担心不会导致任何潜在的内存泄漏,所以想知道在我完成数据时是否有一种首选方法来取消绑定这些连接.
我在考虑:
datagridview.DataSource = null;
bindingsource.DataSource = null;
bindingsource.Clear();
Run Code Online (Sandbox Code Playgroud)
重新绑定:
bindingsource.DataSource = bindinglist<myObjects>;
datagridview.DataSource = bindingsource;
Run Code Online (Sandbox Code Playgroud)
这个订单是正确的,还是真的重要?我是否遗漏了应该存在的任何内容?
任何指针都表示赞赏,谢谢.
我通过在运行主表单之前立即启动新线程来显示启动表单.
在这个线程运行的方法中,我正在使用Application.Run,如下面的选项1所示.这是一个正确的方法,或者是否有问题等我,因为我已经两次调用Application.Run?另一种选择是选项2,也在下面显示,我调用.ShowDialog()来显示表单.
启动表单本身在指定时间后关闭,在表单本身内控制,两个选项似乎都运行良好.
所以我的问题是:哪个更受欢迎 - 选项1还是选项2?如果你能给出一个或那个很好的具体原因.
谢谢.
主要片段:
// Run splash screen thread.
Thread splash = new Thread(new ThreadStart(ShowSplash));
splash.Start();
// Run main application.
Application.Run(new MainForm());
Run Code Online (Sandbox Code Playgroud)
显示启动表单选项1:
static void ShowSplash()
{
Application.Run(new SplashForm());
}
Run Code Online (Sandbox Code Playgroud)
显示启动表单选项2:
static void ShowSplash()
{
using (SplashForm splash = new SplashForm())
{
splash.ShowDialog();
}
}
Run Code Online (Sandbox Code Playgroud) c# ×8
winforms ×6
.net ×3
data-binding ×2
datagridview ×2
serial-port ×2
asynchronous ×1
client ×1
directory ×1
forms ×1
operation ×1
protocols ×1
size ×1
stream ×1
username ×1
windows ×1