这个备份器放在项目目录下面,每次使用就双击一下,因为便捷性,就不采用xml等等储存信息,全部在面板内做,这样可以保证一个exe就运行了.

我发现运行起来还蛮快的,唯一没有实现的是ping通的电脑如果出现空密码登陆,没有登陆过,还是会有问题...要保证先连接过那台电脑

如果可以写个大的,例如直接把局域网给弄通了就好了0.0

代码仅供参照..

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using static JingJingBoxDD.Intranet; namespace JingJingBoxDD
{
public partial class IntranetBackupDevice : Form
{
#if 宏
private const string _pcNameMostly = "A2";
private const string _proName = "01.饰施图";
#elif 锐
private const string _pcNameMostly = "hzr";
private const string _proName = "01.施工图";
#endif
private string currentPath;
private bool on_off = false;//暂停修改checkedListBox1 /// <summary>
/// 点击计数,用于重新初始化
/// </summary>
private static int CheckedListBox1Counter = ; //关闭
private void Button2_Click(object sender, EventArgs e)
{
Close();
} //作者信息
private void Button3_Click(object sender, EventArgs e)
{
string jj = "www.cnblogs.com/JJBox";
DialogResult dr = MessageBox.Show(
"说明:本程序会在'备份器exe所在位置'进行备份" +
"\n此文件夹前将视为'项目文件夹': " + _proName +
"\n更换网段时候后需要重新ping" +
"\n\n作者:刘启宏 企鹅:540762622" +
"\n博客: " + jj + " ('确定' 进入博客)"
, "惊惊盒子",
MessageBoxButtons.OKCancel
);
if (dr == DialogResult.OK)
{
System.Diagnostics.Process.Start(jj);
}
} public IntranetBackupDevice()
{
try
{
InitializeComponent(); TopMost = true;//置顶窗体
FormBorderStyle = FormBorderStyle.FixedDialog;//设置边框为不可调节
StartPosition = FormStartPosition.CenterScreen;//在当前屏幕中央 checkedListBox1.HorizontalScrollbar = true;//显示垂直滚动条
checkedListBox1.ScrollAlwaysVisible = true;//显示水平滚动条
checkedListBox1.CheckOnClick = true;//按一下就打钩 textBox3.Text = NetworkSegment;
label6.Text = "默认主机:" + _pcNameMostly;
button1.TabIndex = ; //获取exe完整路径,若是网络驱动器,就转换,本地就原本
currentPath = PathTool.GetUNCPath(Process.GetCurrentProcess().MainModule.FileName); //可以获得不带文件名的路径
currentPath = Path.GetDirectoryName(currentPath);
string fileName = StringTool.PathToFileNameProjectName(currentPath, );//获取文件名 if (_proName == fileName)//获取项目名
{
textBox2.Text = StringTool.PathToFileNameProjectName(currentPath, );
label7.Text = "警告:无";
}
else
{
label7.Text = string.Format("警告:当前备份器exe不在 {0} 内,将采用以上对话框信息备份.", _proName);
textBox2.Text = fileName;
} var beifennian = StringTool.GetFlie4Number(textBox2.Text);
if (beifennian == "")//年份确定
{
DateTime currentTime = DateTime.Now;
textBox4.Text = currentTime.Year.ToString();
}
else
{
textBox4.Text = beifennian;
}
{
//执行死循环修改的
//创建线程 Invoke方法是同步的方法,所以执行过程是有先后顺序的,所以就不会出现那个异常了
Thread newThread = new Thread(new ThreadStart(Runtime))
{
//加上这句话,否则在关闭窗体时会出现如下错误:在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。
IsBackground = true
};
newThread.Start();
}
{
ThreadPool.GetMaxThreads(out int max, out int maxIo);
ThreadPool.GetMinThreads(out int min, out int minIo); int ipNumbers = ipNumber + ;
if (max - ipNumbers > )
{
max = ipNumbers;
}
if (maxIo - ipNumbers > )
{
maxIo = ipNumbers;
}
if (min - ipNumbers < )
{
min = ipNumbers;
}
if (minIo - ipNumbers < )
{
minIo = ipNumbers;
}
//启动线程池
bool maxbool = ThreadPool.SetMaxThreads(max, maxIo);
bool minbool = ThreadPool.SetMinThreads(min, minIo);
if (maxbool && minbool)
{
Button4_Click(null,null);//开始就执行遍历局域网
label7.Text = string.Format("注明:线程数设置{0},{1},{2},{3}", max, maxIo, min, minIo);
}
else
{
label7.Text = "警告:线程数设置失败";
}
}
}
catch (Exception e)
{
label7.Text = e.Message;
throw e;
}
} /// <summary>
/// 点击备份
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, EventArgs e)
{
string dPath = textBox1.Text;
if (dPath != "")
{
if (Directory.Exists(dPath))//防止第二次点击备份
{
label7.ForeColor = System.Drawing.Color.Blue;
label7.Text = "警告:路径已经存在,请更换路径!(是否重复备份了?)";
}
else
{
bool flag = FileTool.Copy(currentPath, dPath);//复制文件
if (flag)
{
label7.ForeColor = System.Drawing.Color.MediumSeaGreen;
label7.Text = "备份成功!";
button5.TabIndex = ;//设置焦点到打开文件夹
}
else
{
label7.ForeColor = System.Drawing.Color.Blue;
label7.Text = "警告:备份不成功,已执行回滚,请再尝试!";
}
}
}
else
{
label7.ForeColor = System.Drawing.Color.Red;
label7.Text = "警告:备份路径为空!";
}
} /// <summary>
/// Esc键退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == ) //Esc键
{
Close();
}
} /// <summary>
/// 窗体将比其内的控件优先获得键盘事件的激活权
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
KeyPreview = true;
} /// <summary>
/// 开始ping 遍历局域网
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button4_Click(object sender, EventArgs e)
{
//如果用多线程调用的话,会出现多个pingEnt和qiyongEnt值,对加入表造成错误例如多个A2
try
{
//暂停加表,清空表,初始化
on_off = false;
{
pingEnt++;
qiyongEnt++;
NetworkSegment = textBox3.Text; CheckedListBox1Counter = ;
for (int i = ; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.Items.Clear();
}
Initialization();
}
on_off = true; //继续加入表中 for (int i = ; i < ipNumber; i++)//生成255个线程
{
ThreadPool.QueueUserWorkItem(new WaitCallback(PingIP), new int[] { i, qiyongEnt });
}
}
catch
{
label7.Text = "警告:RunPing出错";
}
} /// <summary>
/// 默认选中主机的时候不触发 CheckedListBox1选择事件
/// </summary>
private bool buchufagenggai = true; /// <summary>
/// 通过委托实现死循环修改面板
/// </summary>
/// https://blog.csdn.net/htiannuo/article/details/52229695
public void Runtime()
{
HostInformation[] conames = new HostInformation[] { };
StringBuilder kongge; while (true)
{
if (on_off)
{
lock (ComputerNames)
{
conames = ComputerNames.ToArray();
}
if (CheckedListBox1Counter < conames.Length)
{
var conameN = conames[CheckedListBox1Counter];
if (conameN.HostName != "" && conameN.HostIP != "")//锁了就应该不会出现这样的问题了
{
Invoke((EventHandler)delegate
{
//ip可能为名称,ip长度 255.255.255.255 15长+5个空格=20
int bukongge = - conameN.HostName.Length;
kongge = new StringBuilder();
for (int i = ; i < bukongge; i++)
{
kongge.Append(" ");
}
checkedListBox1.Items.Add(conameN.HostName + kongge + conameN.HostIP + " \t" + conameN.Remarks); if (conameN.HostName == _pcNameMostly)
{
buchufagenggai = false;
//选择默认主机
checkedListBox1.SetItemCheckState(CheckedListBox1Counter, CheckState.Checked);//打钩
checkedListBox1.SelectedIndex = CheckedListBox1Counter;//焦点项
buchufagenggai = true;
}
});
}
CheckedListBox1Counter++;
//这个不能放在Invoke里面,不然又Form1窗体假死情况
Thread.Sleep();
}
}
}
} /// <summary>
/// 打开文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button5_Click(object sender, EventArgs e)
{
if (Directory.Exists(textBox1.Text)) //若路径存在
{
Process.Start("explorer.exe", textBox1.Text);
}
else
{
label7.ForeColor = System.Drawing.Color.Red;
label7.Text = "警告:备份路径为空!请点击备份再点我~";
}
} /// <summary>
/// 弄成单选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.CurrentValue == CheckState.Checked)
{
return;//取消选中就不用进行以下操作
}
for (int i = ; i < ((CheckedListBox)sender).Items.Count; i++)
{
((CheckedListBox)sender).SetItemChecked(i, false);//将所有选项设为不选中
}
e.NewValue = CheckState.Checked;//刷新
} private static string patha = "";
private static string pathTime = ""; /// <summary>
/// CheckedListBox1选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// if (buchufagenggai)用了的话会无法直接出现在 textBox1.Text
{
//获取唯一选中项,修改textBox1路径
for (int j = ; j < checkedListBox1.Items.Count; j++)
{
if (checkedListBox1.GetItemChecked(j))
{
try
{
var conames = ComputerNames.ToArray();
string time = StringTool.GetTimeNoSeconds(out int miao);
string pathb = @"\\" + conames[j].HostName + _SharedFolders;
string pathc = @"\\" + conames[j].HostIP + _SharedFolders; for (int i = ; true; i++)
{
bool pb = Directory.Exists(pathb);
if (!pb)
{
pb = Directory.Exists(pathc);
patha = pathc;
}
else
{
patha = pathb;
}
if (pb) //存在一级
{
pathTime = StringTool.GetTimeName(time, ref miao, i);//循环名称不加长
string path = ChangPath(textBox4.Text, textBox2.Text);
if (!Directory.Exists(path)) //存在就循环
{
textBox1.Text = path;
break;
}
}
else
{
DialogResult dr = MessageBox.Show(
patha + "\n\n无法连接,没有开机?不存在机名?没有访问权限?", "惊惊盒子", MessageBoxButtons.OK);
break;
}
}
}
catch
{ }
break;
}
}
}
} private void TextBox2_TextChanged(object sender, EventArgs e)
{
if (patha != "")//防止初始化的时候就更改
{
textBox1.Text = ChangPath(textBox4.Text, textBox2.Text);
}
} /// <summary>
/// 备份全路径
/// </summary>
/// <param name="beifennian"></param>
/// <param name="project"></param>
/// <returns></returns>
private string ChangPath(string beifennian, string project)
{
return patha + @"\" + beifennian + @"\" + project + @"\" + _proName + @"\" + pathTime;
} private void Form1_Load(object sender, PreviewKeyDownEventArgs e)
{ }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading; namespace JingJingBoxDD
{
public partial class Intranet
{
public const string _SharedFoldersA = "中转站";
public const string _SharedFolders = @"\" + _SharedFoldersA; /// <summary>
/// 网段
/// </summary>
public static string NetworkSegment { get; set; } = "192.168.1"; /// <summary>
/// 按了第几次ping,直到关闭才重置
/// </summary>
public static int pingEnt = ; /// <summary>
/// 启用ping的时候计数,直到关闭才重置
/// </summary>
public static int qiyongEnt = ; public static bool WaitOne = true;
public static int ipNumber = ;//ip数
public static List<HostInformation> ComputerNames { get; set; } = new List<HostInformation>();//记录电脑名称和对应的ip,必须初始化,不然无法lock
public static AutoResetEvent pingOver;//线程池结束标记 /// <summary>
/// 初始化
/// </summary>
public static void Initialization()
{
WaitOne = true;
ipNumber = ;//ip数
pingOver = new AutoResetEvent(false);//线程池结束标记
ComputerNames = new List<HostInformation>();
} public static void PingIP(object obj)
{
var ints = obj as int[];//0是ip,1是启动点击的位置
string ip = NetworkSegment + "." + ints[].ToString();
try
{
Ping myPing = new Ping();
PingReply reply = myPing.Send(ip, ); if (reply.Status.Equals(IPStatus.Success))//ping通了
{
string hostname = ""; //通过ip获取电脑名称,没有电脑名称会引起错误,造成下面无法递减,必须容错
hostname = Dns.GetHostEntry(ip).HostName; //当前计数和启用计数要一样才加入,启用是传值的,也是固定的,
//而pingEnt是按了按钮就会改的,存在时间差,造成可以判断.
if (pingEnt == ints[])
{
if (hostname != null && hostname.Trim() != "")
{
string[] strs = GetNetShareList(hostname);
if (strs.Length == )
{
//名称和ip在win10通过空密码时候验证不一样..所以两个都测试一下(如果空密码没有进入过,都会失败
strs = GetNetShareList(ip);
}
if (strs.Length > )
{
lock (ComputerNames)
{
if (strs.Contains(_SharedFoldersA))
{
ComputerNames.Add(new HostInformation(hostname.ToUpper(), ip, "有:" + _SharedFoldersA));
}
else
{
ComputerNames.Add(new HostInformation(hostname.ToUpper(), ip, "有共享文件夹,无:" + _SharedFoldersA));
}
}
}
}
else
{
lock (ComputerNames)
{
ComputerNames.Add(new HostInformation(ip, ip, "没有机名但是ping通了"));
}
}
}
} }
catch
{ }
//上面必须容错,实行这里的递减
//线程池计数,用来实现最后一个线程时候通知.
ipNumber--;
//Thread.Sleep(500);
if (ipNumber == )
{
pingOver.Set();
}
} public struct HostInformation
{
public string HostName;
public string HostIP;
public string Remarks; public HostInformation(string hostname, string hostip, string remarks)
{
HostName = hostname;
HostIP = hostip;
Remarks = remarks;
}
} /// <summary>
/// 一直等待到找到或者结束
/// </summary>
/// <param name="pcname"></param>
/// <returns></returns>
public static string WhilePing(string pcname)
{
pcname = pcname.ToUpper();
string ip = null;
try
{
while (true)
{
foreach (var item in ComputerNames.ToArray())//防止线程更改了list,必须toarray
{
if (item.HostName == pcname)
{
ip = item.HostIP; //如果找到了ip,就拿出来
break;
}
}
if (WaitOne) //等待过一次就不能再用这个函数
{
WaitOne = false;
pingOver.WaitOne();//等待 pingOver.Set();执行,表示线程池已经终止,如果线程结束,重复等待就会死掉了
}
else
{
break;
}
}
}
catch
{ }
return ip;
}
}
}
using System;
using System.Collections;
using System.Runtime.InteropServices; namespace JingJingBoxDD
{
// https://q.cnblogs.com/q/46971/
public partial class Intranet
{
[StructLayout(LayoutKind.Sequential)]
protected struct SHARE_INFO_1
{
[MarshalAs(UnmanagedType.LPWStr)]
public string shi1_netname;
[MarshalAs(UnmanagedType.U4)]
public uint shi1_type;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi1_remark;
} [DllImport("Netapi32.dll", CharSet= CharSet.Auto)]// EntryPoint = "NetShareEnum"
protected static extern int NetShareEnum(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.U4)] uint level,
out IntPtr bufptr,
[MarshalAs(UnmanagedType.U4)] int prefmaxlen,
[MarshalAs(UnmanagedType.U4)] out uint entriesread,
[MarshalAs(UnmanagedType.U4)] out uint totalentries,
[MarshalAs(UnmanagedType.U4)] out uint resume_handle
); /// <summary>
/// 遍历某台电脑的共享文件
/// </summary>
/// <param name="server"></param>
/// <returns></returns>
public static string[] GetNetShareList(string server)
{ //-1应该是获取所有的share,msdn里面的例子是这么写的,返回0表示成功
if (NetShareEnum(server, , out IntPtr buffer, -, out uint entriesread, out uint totalentries, out uint resume_handle) == )
{
int ptr = buffer.ToInt32();
ArrayList alShare = new ArrayList();
for (int i = ; i < entriesread; i++)
{ SHARE_INFO_1 shareInfo = (SHARE_INFO_1)Marshal.PtrToStructure(new IntPtr(ptr), typeof(SHARE_INFO_1));
if (shareInfo.shi1_type == )//Disk drive类型
{
alShare.Add(shareInfo.shi1_netname);
}
ptr += Marshal.SizeOf(shareInfo);//有点类似C代码
}
string[] share = new string[alShare.Count];
for (int i = ; i < alShare.Count; i++)
{
share[i] = alShare[i].ToString();
}
return share;
}
else
{
return null;
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms; namespace JingJingBoxDD
{
public static partial class FileTool
{ /// <summary>
/// 复制文件到文件,文件到路径,路径到路径
/// </summary>
/// <param name="sPath"></param>
/// <param name="dPath"></param>
/// <returns></returns>
public static bool Copy(string sPath, string dPath)
{
bool fa =false;
if (sPath.Length - > && sPath.Length - > )
{
sPath = sPath.Trim();
while (sPath[sPath.Length - ] == '\\')
{
sPath = sPath.Substring(, sPath.Length - );
}
dPath = dPath.Trim();
while (dPath[dPath.Length - ] == '\\')
{
dPath = dPath.Substring(, dPath.Length - );
} if (File.Exists(sPath) && File.Exists(dPath)) //都是文件
{
fa = CopyFile(sPath, dPath);
}
else if (File.Exists(sPath)) //文件复制到路径
{
fa = CopyFileToPath(sPath, dPath, true);
}
else if (Directory.Exists(sPath)) //目标路径要新建,不能判断
{
fa = CopyDirectory(sPath, dPath);
}
else
{
fa = false;
}
}
return fa;
} /// <summary>
/// 递归拷贝所有子路径
/// </summary>
/// <param name="sPath">来源</param>
/// <param name="dPath">目标</param>
/// <returns>true成功false不成功</returns>
private static bool CopyDirectory(string sPath, string dPath)
{
bool flag = false;
if (NewFolder(dPath))
{
DirectoryInfo dir = new DirectoryInfo(sPath);//当前路径
flag = CopyFile(dir, dPath);//复制当前路径到目标路径
if (flag)
{
try
{
foreach (var 来源子路径文件夹名 in dir.GetDirectories())//获取所有子路径
{
string 来源子路径的完整路径 = 来源子路径文件夹名.FullName;
string 目标子路径的完整路径 = 来源子路径的完整路径.Replace(sPath, dPath);
if (NewFolder(目标子路径的完整路径))
{
CopyFile(来源子路径文件夹名, 目标子路径的完整路径);
CopyDirectory(来源子路径的完整路径, 目标子路径的完整路径);
}
}
}
catch
{
flag = false;
DelPath(dPath);
}
}
else
{
DelPath(dPath);
}
}
return flag;
} /// <summary>
/// 拷贝路径下的所有文件到目标路径
/// </summary>
/// <param name="sPath">来源路径</param>
/// <param name="dPath">目标路径</param>
private static bool CopyFile(DirectoryInfo sPath, string dPath)
{
bool flag = true;
try
{
string sourcePath = sPath.FullName;//来源的全路径
FileInfo[] files = sPath.GetFiles();//获取所有文件
foreach (FileInfo file in files)//遍历所有文件
{
file.Attributes = FileAttributes.Normal; //设置为普通文件
string sourceFileFullName = file.FullName;//文件的全路径
string destFileFullName = sourceFileFullName.Replace(sourcePath, dPath);
file.CopyTo(destFileFullName, true);
}
}
catch
{
return false;
}
return flag;
} /// <summary>
/// 复制大文件
/// </summary>
/// <param name="fromPath">源文件的路径</param>
/// <param name="toPath">文件保存的路径</param>
/// <param name="eachReadLength">每次读取的长度</param>
/// <returns>是否复制成功</returns>
private static bool CopyFile(string fromPath, string toPath)
{
int eachReadLength = * * ; //将源文件 读取成文件流
FileStream fromFile = new FileStream(fromPath, FileMode.Open, FileAccess.Read);//这样无法读取局域网
//已追加的方式 写入文件流
FileStream toFile = new FileStream(toPath, FileMode.Append, FileAccess.Write);
//实际读取的文件长度
int toCopyLength = ;
//如果每次读取的长度小于 源文件的长度 分段读取
if (eachReadLength < fromFile.Length)
{
byte[] buffer = new byte[eachReadLength];
long copied = ;
while (copied <= fromFile.Length - eachReadLength)
{
toCopyLength = fromFile.Read(buffer, , eachReadLength);
fromFile.Flush();
toFile.Write(buffer, , eachReadLength);
toFile.Flush();
//流的当前位置
toFile.Position = fromFile.Position;
copied += toCopyLength;
}
int left = (int)(fromFile.Length - copied);
toCopyLength = fromFile.Read(buffer, , left);
fromFile.Flush();
toFile.Write(buffer, , left);
toFile.Flush();
}
else
{
//如果每次拷贝的文件长度大于源文件的长度 则将实际文件长度直接拷贝
byte[] buffer = new byte[fromFile.Length];
fromFile.Read(buffer, , buffer.Length);
fromFile.Flush();
toFile.Write(buffer, , buffer.Length);
toFile.Flush();
}
fromFile.Close();
toFile.Close();
return true;
} /// <summary>
/// 复制文件到路径
/// </summary>
/// <param name="file">文件,要完整路径</param>
/// <param name="dPath">目标路径</param>
/// <param name="overwrite">是否覆盖文件</param>
/// <returns></returns>
private static bool CopyFileToPath(string file, string dPath, bool overwrite)
{
bool fa = false;
try
{
if (NewFolder(dPath))
{
string destPath = Path.Combine(dPath, Path.GetFileName(file));
File.Copy(file, destPath, overwrite);
fa = true;
}
}
catch (Exception e)
{
DialogResult dr = MessageBox.Show("复制文件错误!\n" + e.Message, "惊惊盒子", MessageBoxButtons.OKCancel);
}
return fa;
} #region 删除文件
/// <summary>
/// 删除目录
/// </summary>
/// <param name="pathWhole">全路径</param>
public static bool DelPath(string pathWhole)
{
try
{
pathWhole = StringTool.PathDelLastSlash(pathWhole);
string pathFront = StringTool.PathHigherLevel(pathWhole); //删除所有子文件
DelDirectory(pathWhole);
//子目录和文件数都是0,才能删除母文件夹
if (Directory.GetDirectories(pathFront).Length == && Directory.GetFiles(pathFront).Length == )
{
DirectoryInfo dir = new DirectoryInfo(pathFront)
{
Attributes = FileAttributes.Normal //设置为普通目录
};
dir.Delete(true);//删除文件夹
}
return true;
}
catch
{
return false;
}//回滚出错无需提示,可能是复制不进去
} /// <summary>
/// 递归删除所有子路径
/// </summary>
/// <param name="sPath">来源</param>
/// <returns>是否成功</returns>
private static bool DelDirectory(string sPath)
{
bool flag = false;//是否成功删除
DirectoryInfo dir = new DirectoryInfo(sPath);//路径
DirectoryInfo[] dirs = dir.GetDirectories(); //获取所有子路径
if (dirs.Length > )
{
foreach (var item in dirs)//先处理子路径的文件
{
//子路径如果是文件夹,就嵌套进去
FileAttributes attr = File.GetAttributes(item.FullName);
if (attr == FileAttributes.Directory)
{
DelDirectory(item.FullName);//递归进去删除文件
}
else
{
DirectoryInfo dir2 = new DirectoryInfo(item.FullName)
{
Attributes = FileAttributes.Normal //设置为普通文件
};
dir2.Delete(true);
}
}
flag = true;
}
//最后处理主路径的文件
dir.Attributes = FileAttributes.Normal; //设置为普通文件
dir.Delete(true);//删除文件夹
return flag;
} /// <summary>
/// 删除文件
/// </summary>
/// <param name="path">文件的路径</param>
/// <returns></returns>
public static bool DelFile(this string path)
{
bool flag = false;
if (File.Exists(path))
{
try
{
FileInfo dir = new FileInfo(path)
{
Attributes = FileAttributes.Normal //设置为普通文件
};
dir.Delete();
flag = true;
}
catch
{ }
}
return flag;
} /// <summary>
/// 重命名文件名字,如果路径还存在就名字+1
/// </summary>
/// <param name="path">文件路径</param>
/// <returns>返回最终的名字,文件不在返回""</returns>
public static string RenameFile(this string path)
{
string str = path;
if (File.Exists(str))
{
int a = ;
try
{
FileInfo dir = new FileInfo(str)
{
Attributes = FileAttributes.Normal //设置为普通文件
};
string qianzhui = dir.DirectoryName + "\\"; // "D:\\K01.惊惊盒子\\03.用户配置"
while (true)
{
string name = Path.GetFileNameWithoutExtension(path); //返回不带扩展名的文件名 string pathFull = qianzhui + name + a.ToString() + dir.Extension;
if (!File.Exists(pathFull))//如果没有这个文件
{
dir.MoveTo(pathFull);//就重命名这个文件名
str = pathFull;
break;
}
else
{
a++;
}
}
}
catch
{ }
}
return str;
}
#endregion /// <summary>
/// 新建文件夹
/// </summary>
/// <param name="path">路径</param>
/// <returns>成功true,失败false</returns>
public static bool NewFolder(string path)
{
bool flag = false;
try
{
if (!Directory.Exists(path))
{
DirectoryInfo aa = Directory.CreateDirectory(path);//新建文件夹
aa.Attributes = FileAttributes.Normal;//设置文件夹属性为普通
flag = true;
}
}
catch
{ }
return flag;
} /// <summary>
/// 路径中包含某个文件夹,有就返回文件夹路径
/// </summary>
/// <param name="dwgPath">路径</param>
/// <param name="name">文件夹名称</param>
/// <returns>成功返回含有这个文件夹名称的路径,失败返回""</returns>
public static string GetPathIncludeString(string dwgPath, string name)
{
//遍历路径中第几级有"01.饰施图"
char ca = '\\';
string[] dwgPaths = dwgPath.Split(ca);
List<string> st = new List<string>();
for (int i = ; i < dwgPaths.Length; i++)
{
if (dwgPaths[i] == name)
{
break;
}
else
{
st.Add(dwgPaths[i]);//把前面的路径合并起来
}
}
//如果两个数组相同,表示路径没有"01.饰施图"
string[] stArr = st.ToArray();
string x = "";
if (stArr.Length != dwgPaths.Length)
{
foreach (string item in stArr)
{
x += item + "\\";
}
x += name;
}
return x;
} /// <summary>
/// 列出本文件夹某个后缀的文件
/// </summary>
/// <param name="path">路径</param>
/// <param name="searchPattern">获取后缀的文件 *.txt</param>
/// <returns></returns>
public static string[] GetThisFolderFiles(string path, string searchPattern)
{
List<string> list = new List<string>();
DirectoryInfo theFolder = new DirectoryInfo(path);
FileInfo[] thefileInfo = theFolder.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);
foreach (FileInfo NextFile in thefileInfo) //遍历文件
{
list.Add(NextFile.FullName);
}
return list.ToArray();
} #if false
/// <summary>
/// 获取本文件夹及其子文件夹所有后缀的文件
/// </summary>
/// <param name="path">路径</param>
/// <param name="searchPattern">获取后缀的文件 *.txt</param>
/// <returns></returns>
public static string[] GetThisFolderAndSunFiles(string path, string searchPattern)
{
List<string> list = new List<string>();
DirectoryInfo theFolder = new DirectoryInfo(path);
FileInfo[] thefileInfo = theFolder.GetFiles(searchPattern, SearchOption.AllDirectories);
foreach (FileInfo NextFile in thefileInfo) //遍历文件
{
list.Add(NextFile.FullName);
}
return list.ToArray();
}
#endif /// <summary>
/// 获取路径下所有文件以及子文件夹中文件
/// </summary>
/// <param name="path">全路径根目录</param>
/// <param name="FileList">存放所有文件的全路径</param>
/// <param name="RelativePath"></param>
/// <returns></returns>
public static string[] GetFile(string path, string extension)
{
List<string> pas = new List<string>();
DirectoryInfo dir = new DirectoryInfo(path);
var fil = dir.GetFiles();//获取本文件夹所有文件
foreach (FileInfo f in fil)
{
if (f.Extension.ToUpper() == extension.ToUpper())
{
pas.Add(f.FullName);//保存文件路径到表中
}
}
//获取子文件夹内的文件列表,递归遍历
var dii = dir.GetDirectories();
foreach (DirectoryInfo d in dii)
{
var a = GetFile(d.FullName, extension);//返回数组
foreach (var item in a)
{
pas.Add(item);
}
}
return pas.ToArray();
} /// <summary>
/// 判断文件是否被程序占用
/// </summary>
/// <param name="fileName">文件路径</param>
/// <returns>true表示正在使用,false没有使用</returns>
public static bool IsFileInUse(string fileName)
{
bool inUse = true;
if (File.Exists(fileName))
{
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
inUse = false;
}
catch
{ }
finally
{
if (fs != null)
{
fs.Close();
}
}
return inUse;
}
else
{
return false;//文件不存在,肯定没有被使用
}
}
}
public static partial class FileTool
{
//https://blog.csdn.net/catchme_439/article/details/54616175 private const int FO_COPY = 0x0002;
private const int FOF_ALLOWUNDO = 0x00044;
//显示进度条 0x00044
//不显示一个进度对话框 0x0100 显示进度对话框单不显示进度条 0x0002显示进度条和对话框
private const int FOF_SILENT = 0x0002;//0x0100;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = )]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)]
public int wFunc;
public string pFrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
public static bool ApiCopy(string strSource, string strTarget)
{
SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT
{
wFunc = FO_COPY,
pFrom = strSource.Trim(),
lpszProgressTitle = "复制大文件",
pTo = strTarget.Trim(),
//fileop.fFlags = FOF_ALLOWUNDO;
fFlags = FOF_SILENT
};
return SHFileOperation(ref fileop) == ;
}
}
}

c# 多线程实现ping 多线程控制控件的更多相关文章

  1. OpenLayers 3 的 图层控制控件

    openlayers3的control中没有提供默认的图层控制控件. 但是git上已经有造好的轮子,直接拿来用就可以了.地址 https://github.com/walkermatt/ol3-lay ...

  2. 如何在多线程中调用winform窗体控件

    由于 Windows 窗体控件本质上不是线程安全的.因此如果有两个或多个线程适度操作某一控件的状态(set value),则可能会迫使该控件进入一种不一致的状态.还可能出现其他与线程相关的 bug,包 ...

  3. android 代码控制控件的长宽,小技巧

    要在代码里改变ImageView 的长宽,如图 通过拿到contentImage这对象的控件参数,再去改变,再设置 , 上图的contentImage为ImageView对象: 而这里 要提醒的是,L ...

  4. 附加属性来控制控件中,要扩展模块的visibility

    可解决: 文本框控件中的按钮,DataGridColumnHeader中加入Filter控件... cs文件中的 附加属性 + 样式文件中的 template+控件 -> visibility ...

  5. java 随意控制控件的位置

    package chat1; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class chat1{ ...

  6. c# 多线程调用窗体上的控件 示例

    private delegate void InvokeCallback(string msg); private void SetCountValue(string s) { if (this.fo ...

  7. BPM控制控件的隐藏与显示

    在BPM官方给的属性中控件属性DisplayRule中设置条件控制显示或者隐藏的时候,只能控制选中的,标题也要重新设置,这样就比较麻烦,不如自己来写javascript代码. 举一个简单的例子: 通过 ...

  8. 使用padding值控制控件的隐藏与显示

    在学自定义控件下拉刷新这一案例,控制ListView头条目和尾条目的显示隐藏时,就是设置其padding值的正负控制其的显示与隐藏.这并不是什么很大的知识点.只是一个小技巧,这里给大家分享一下. 这一 ...

  9. js控制控件不可编辑

    js 页面加载执行 function Isedit() { //document.getElementById("SyTxtBox23_DataControl").disabled ...

随机推荐

  1. django-内网项目上线测试部署步骤

    1.安装python环境 由于测试环境只有内网,所以在外网同系统上安装python. wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5 ...

  2. Linux命令:readonly

    readonly [-aAf] [name[=value] ...] or readonly -p -A 表示后面的name变量都是关联数组 -a 表示后面的name变量都是index数组 -f 表示 ...

  3. python 的类装饰器

    我们都知道python的函数有装饰器,那么类也有装饰器吗?有的,为什么没有呢,来看下代码吧 def out(args): def inner(cls): cls._args = args return ...

  4. MariaDB ColumnStore初探(1):安装、使用及测试

    相信大家在对接BI数据报表部门有很深刻的体验,高大上的复杂SQL关联JOIN十几张表在InnoDB里跑起来,会让你酸爽到死.它的出现正是解决这个问题,DBA能不能轻松愉快地玩耍,就要靠它了,“神州行我 ...

  5. python04 列表 元祖 字典

    1.list 有序,元素可以被修改 li=[1,2,2,"am","123"] 列表中的元素可以是数字,字符串,列表等等 支持切片,切片结果为列表  li[3] ...

  6. redis主从复制详述

    一.主从复制详述 原理其实很简单,master启动会生成一个run id,首次同步时会发送给slave,slave同步命令会带上run id以及offset,显然,slave启动(初次,重启)内存中没 ...

  7. Python之ftp服务器

    今天把做的ftp服务器过程总结一下,先看看要求 一.需求 1. 用户加密认证 2. 允许同时多用户登录 3. 每个用户有自己的家目录 ,且只能访问自己的家目录 4. 对用户进行磁盘配额,每个用户的可用 ...

  8. window 安装mysql

    常见错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 密码输入错误:无法远程 ...

  9. wince sqlite c#

    无法找到 PInvoke DLL“SQLite.Interop.084.dll 按下面步骤操作

  10. java多线程系列15 设计模式 生产者 - 消费者模式

    生产者-消费者 生产者消费者模式是一个非常经典的多线程模式,比如我们用到的Mq就是其中一种具体实现 在该模式中 通常会有2类线程,消费者线程和生产者线程 生产者提交用户请求 消费者负责处理生产者提交的 ...