我正在创建一个程序来在驱动器上运行文件I/O压力,当它完成后,我想删除文件,但不想将它们放在回收站中.这是我现在的代码:
public static void DeleteDirectory(string target_dir)
{
//Delete the files and folders created during stress.
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Thread.Sleep(30);
Directory.Delete(target_dir, true);
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码只是将文件移动到回收站.我不想添加代码来自动清空回收站,因为那里可能有文件我不想删除(文件可能属于其他人).
注意:从目前为止的回复来看,似乎人们可能会误解我在这里要做的事情.当我的程序删除文件时,它会将它们发送到回收站.我不希望发生这种情况,我想立即删除文件.我没有看到任何方法告诉系统删除文件而不将它们发送到回收站.我永远不需要恢复文件.它是默认发送到回收站,但我没有看到任何改变它的方法.如果可能的话,请编辑我发布的代码,让它立即删除文件.我是新手C#用户的开始.
我有VS 2010并希望通过Yes | No | Cancel对话框取消表单结束事件,但是当我将e.Cancel放入对话框的事件处理程序时,我收到一条错误,上面写着"'System.EventArgs'不包含'Cancel'的定义,也没有扩展方法'Cancel'接受类型'System.EventArgs'的第一个参数可以找到(你是否缺少using指令或汇编引用?)." "取消"一词下面也有一条红线.我在网上看到的一切都说这是取消FormClosing活动的唯一方法.我测试了VS2008中的代码,它做了同样的事情.
事件处理程序的代码如下:
private void displayMessageBox(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Do you want to save the changes to the document before closing it?", "MyNotepad",MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
saveToolStripMenuItem_Click(sender, e);
}
else if (result == DialogResult.No)
{
rtbMain.Clear();
this.Text = "Untitled - MyNotepad";
}
else if (result == DialogResult.Cancel)
{
// Leave the window open.
e.Cancel() = true;
}
Run Code Online (Sandbox Code Playgroud)
以下是使用(如果它有所不同):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; …Run Code Online (Sandbox Code Playgroud) 我正在编写一个复制文件的程序.我有正确的文件复制,进度条更新,但我收到一个错误,指出e.ProgressPercentage是101. bgWorker_ProgressChanged事件处理程序的代码是:
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// We will increase the progress bar when work progress is reported.
pbCopyProgress.Maximum = 100;
pbCopyProgress.Value = e.ProgressPercentage;
}
Run Code Online (Sandbox Code Playgroud)
以下是bgWorker_DoWork事件处理程序的代码:
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Gets the size of the file in bytes.
Int64 iSize = strInputFile.Length;
// Keeps track of the total bytes downloaded so we can update the progress bar.
Int64 iRunningByteTotal = 0;
// Open the input file for reading.
using (FileStream InputFile = new …Run Code Online (Sandbox Code Playgroud) 我在尝试确定地址是 IP 地址还是主机名时遇到问题。我发现的所有内容都表明要使用正则表达式。我不知道如何形成 IF 语句。这是我的代码:
private void btnPingAddress_Click(object sender, EventArgs e)
{
intByteSize = Convert.ToInt32(numericDataSize.Value);
intNumberOfPings = Convert.ToInt32(numericPing.Value);
strDnsAddress = cmbPingAddress.Text;
//If address is IP address:
if (strDnsAddress Contains ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?")
{
txtPingResults.Text = "Pinging " + strIpAddress + " with " + intByteSize + " bytes of data:" + "\r\n";
}
// If address is hostname:
else
{
strIpAddress = Convert.ToString(Dns.GetHostEntry(strDnsAddress));
txtPingResults.Text = "Pinging " + strDnsAddress + " [" + strIpAddress + "] with " + intByteSize + " …Run Code Online (Sandbox Code Playgroud)