您的位置:首页 > 编程语言 > C#

在C#中利用WMI从快捷方式中复制目标文件

2008-01-16 15:00 351 查看
在C#中利用WMI从快捷方式中复制目标文件

电子信箱:ThreeTorches@163.com

前一段时间,因为单位要做几个展板,领导在做的时候误将快捷方式文件当成了目标文件,在进行文件拷贝时才发现文件的大小不对。此时所有选用的图片文件都被用在了快捷方式上,这些文件有几百个之多,通过查看快捷方式得到目标文件,并将目标文件的主名改成快捷方式的主名,扩展名不变。一个一个做,又要大半天的时间。同事求助于我,我考虑了一下,便用C#和WMI写了一个程序,希望能帮助遇到类似问题的人。
主要用到命名空间System.Management和WMI中的Win32_ShortcutFile类及其四个属性:
属性
说明
Name
获取快捷方式文件的完整路径名及文件名
Target,
获取快捷方式文件的所指向的目标文件的完整路径名及文件名
Drive
快捷方式文件所在的盘符,如C:
Path
快捷方式文件所在的路径,不包括盘符前后均有“/”,如/user/sub/,在具体应用时要注意,在WMI中“/”是转义字符,所以在传递时要用“//”。
在VS.NET新建一个工程CopyFromShortcut,将Form1.cs改名为“CopyFile.cs”,并在项目菜单中添加对System.Management的引用。最终运行图如下:

具体文件代码如下:
CopyFile.Designer.cs:
namespace CopyFromShortcut
{
partial class CopyFile
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(306, 28);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(70, 23);
this.button1.TabIndex = 0;
this.button1.Text = "浏览...";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(27, 28);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(263, 21);
this.textBox1.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(10, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(167, 12);
this.label1.TabIndex = 2;
this.label1.Text = "请选择快捷方式所在的文件夹:";
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(12, 142);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(364, 148);
this.listBox1.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 123);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(179, 12);
this.label2.TabIndex = 4;
this.label2.Text = "当前文件夹下的快捷方式及目标:";
//
// textBox2
//
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox2.Location = new System.Drawing.Point(27, 77);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(263, 21);
this.textBox2.TabIndex = 1;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 62);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(71, 12);
this.label3.TabIndex = 2;
this.label3.Text = "文件复制到:";
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(306, 75);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(70, 23);
this.button2.TabIndex = 0;
this.button2.Text = "浏览...";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button3.Location = new System.Drawing.Point(281, 112);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(95, 23);
this.button3.TabIndex = 5;
this.button3.Text = "开始复制";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// 从快捷方式中复制文件
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(388, 302);
this.Controls.Add(this.button3);
this.Controls.Add(this.label2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "从快捷方式中复制文件";
this.Text = "从快捷方式中复制文件";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}

CopyFile.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.IO;

namespace CopyFromShortcut
{
public partial class CopyFile : Form
{
public CopyFile()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string s=folderBrowserDialog1.SelectedPath;
textBox1.Text = s;
GetShortcut(s);
}
}

private void GetShortcut(string path)
{
string driver=path.Substring(0,2);
path=path.Substring(2) +"//"; //WQL中的文件夹前后都有"/"
path=path.Replace("//","////"); //WQL中的文件夹分隔符应是"//"

string wsql="Select * From Win32_ShortcutFile Where Drive='"+driver+"' and Path='"+path+"'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wsql);
if (searcher == null) return;
foreach (ManagementObject o in searcher.Get())
{
listBox1.Items.Add(o.GetPropertyValue("Name").ToString() +" => "+ o.GetPropertyValue("Target").ToString());
}
}

private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string s=folderBrowserDialog1.SelectedPath;
textBox2.Text = s;
}
}

private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0)
{
MessageBox.Show("请选择快捷方式所在文件夹和复制到文件夹.");
return;
}int i;
for (i = 0; i < listBox1.Items.Count; i++)
{
Cursor = Cursors.WaitCursor;
try
{
string fileName = listBox1.Items[i].ToString();
string target = fileName.Substring(fileName.IndexOf(" => ") + 4);

//新文件名=快捷方式主名+目标文件扩展
fileName = fileName.Substring(0, fileName.IndexOf(" => "));
fileName = fileName.Substring(fileName.LastIndexOf("//") + 1);
fileName = fileName.Substring(0, fileName.LastIndexOf(".")) + target.Substring(target.LastIndexOf("."));
File.Copy(target, textBox2.Text + fileName);
}
catch { }
}
Cursor = Cursors.Default;
MessageBox.Show(" 文件复制完成,共有" + i + " 个文件被复制! ");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: