当前位置:网站首页>Copy, cut and paste of C WinForm development files / folders
Copy, cut and paste of C WinForm development files / folders
2022-07-22 21:41:00 【A chicken with multiple senses】
ControlFileClass Class CopyFile and MoveFile function :
/// <summary>
/// Moving files
/// </summary>
/// <param name="dirPath"> File original path </param>
/// <param name="tarPath"> File destination path </param>
/// <param name="name"> file name </param>
public static void MoveFile(string dirPath, string tarPath, string name)
{
bool flag = false;
foreach (string d in Directory.GetFileSystemEntries(dirPath))
{
if (File.Exists(dirPath + @"\" + name))
{
flag = true;
}
}//end of for
if (!flag)
{
Console.WriteLine(" Target file " + name + " non-existent ");
return;
}
File.Move(dirPath + @"\" + name, tarPath + @"\" + name);
}//end of MoveFile
/// <summary>
/// Copy file
/// </summary>
/// <param name="dirPath"> File original path </param>
/// <param name="tarPath"> File destination path </param>
/// <param name="name"> file name </param>
public static void CopyFile(string dirPath, string tarPath, string name)
{
bool flag = false;
foreach (string d in Directory.GetFileSystemEntries(dirPath))
{
if (File.Exists(dirPath + @"\" + name))
{
flag = true;
}
}//end of for
if (!flag)
{
Console.WriteLine(" Target file " + name + " non-existent ");
return;
}
File.Copy(dirPath + @"\" + name, tarPath + @"\" + name);
}//end of CopyFile
Simple function use examples :
ControlFileClass.CopyFile(rootPath + @"\" + "wybnmsl", rootPath + @"\" + "moguwai", "1.txt");
ControlFileClass.MoveFile(rootPath + @"\" + "wybnmsl", rootPath + @"\" + "moguwai", "2.txt");
If it is a console application directly, then copy , Cutting is moving , It's easy to understand , Because the direct current path and target path are known . But in winform In development , This is different , Because clicking copy or cut on a control only knows the current path , Do not know the target path . Then at this time, we need to copy and move Process segmentation , Here is an example :
stay 345 Under the folder , I click item xxxxxxx.txt, At this time, I pass menupath When the click event is recorded ,listview Medium item The path of
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
ContextMenuStrip menu = (ContextMenuStrip)sender;
ListView livi = (ListView)menu.SourceControl;
//ToolStripItem toolStripItem = (ToolStripItem)livi.SelectedIndices;
ListView.SelectedIndexCollection c = livi.SelectedIndices;
menupath = livi.FocusedItem.Tag.ToString();// Record which item Next
// Determine whether the right button position has item
if (c.Count > 0)
{
// Yes item Right click menu will be displayed normally
}
// No, item There was no response
else
e.Cancel = true;
}
At this point, this click event is recorded for all functions in the right-click menu menupath, Then right click the menu (ToolStripMenuItem) Any operation in the needs to use the corresponding item All positions are ok
Such as cutting and copying , Then simply record menupath that will do
private void Copy ToolStripMenuItem_Click(object sender, EventArgs e)
{
originpath = menupath;
if (ControlFileClass.IsFolder(menupath))
{
MessageBox.Show(" Copying folders is not supported ");
}
else
{
MessageBox.Show(" Copy file succeeded ");
}
COM = 1;
}
private void shear ToolStripMenuItem_Click(object sender, EventArgs e)
{
originpath = menupath;
if (ControlFileClass.IsFolder(menupath))
{
MessageBox.Show(" Cutting folders is not supported ");
}
else
{
MessageBox.Show(" Copy and cut succeeded ");
}
COM = 2;
}
Then when pasting , At this time menupath It's the destination address , and originpath It is the original address recorded above , At this point, you can complete the move or copy from the original address to the target address
private void Paste ToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (!ControlFileClass.IsFolder(menupath))
{
MessageBox.Show(" Paste failed , The paste target should be folder ");
}
else
{
string filename = ControlFileClass.GetFileName(originpath);
originpath = ControlFileClass.GetFolderPath(originpath);
if (COM == 1)
{
ControlFileClass.CopyFile(originpath, menupath, filename);
COM = 0;
}
else if (COM == 2)
{
ControlFileClass.MoveFile(originpath, menupath, filename);
COM = 0;
}
else
{
MessageBox.Show(" There are no files in the clipboard ");
}
}
Knowledge base refresh (fenquname);
}
边栏推荐
猜你喜欢
【问题篇】IDEA运行war包项目
8.SSH高级命令
Automated test JSON general parser
6.echo 和重定向
Wonderful! The description of meituan Octo distributed service management system is too clear
On the characteristics of filter pseudo protocol
妙啊!美团 OCTO 分布式服务治理系统,这描述也太清晰了
acwing-飞行员兄弟
C# Winform开发 WinAppDriver 自动测试的使用
自动化测试Json通用解析器
随机推荐
Quickly find the source code and website materials of the thesis
Web编程入门 1.4 PHP连接数据库 PDO
Blue Bridge Cup - incremental triples
两台服务器之间拷贝文件-scp方法
Copying files between two servers SCP method
Significance learning record of FFT in MATLAB
Relationship and difference between session and cookie
蓝桥杯-K倍区间
extern “C“的作用
On the characteristics of filter pseudo protocol
【问题篇】关于上传图片到服务器指定位置失败问题
8. Memory and disk management
云报传媒广场数字电视转模拟系统设计分享
acwing-飞行员兄弟
Coinbase's top 10 forecasts for Web3 and encryption economy in 2022
Leetcode notes (I) backtracking
Blue bridge cup-k-fold range
Function of extern "C"
Cartesi March 2022 review
NVIDIA CLARA资料