c#重命名文件 - 抛弃MoveTo,而引用vc 中rename生成rename.dll
时间:2011-04-16 来源:94cool
1:vc6中新建win32 简单动态链接库
2:添加Rename.cpp文件,如下:
#include <string>
#include <stdlib.h>
using namespace std;
extern "C" int _declspec(dllexport) renamefile(char *_oldname,char * _newname);
int renamefile(char* _oldname,char* _newname)
{
char oldname[200], newname[200];
strcpy(oldname,_oldname);
strcpy(newname,_newname);
if (rename(oldname, newname) == 0)
return 0;
else
return 1;
}
3:生成Rename.dll
4:在C#中调用
复制Rename.dll到应用程序BIN目录下
C#程序:
using System.Runtime.InteropServices;
namespace WinApp
{
public partial class Form4 : Form
{
[DllImport("Rename.dll", EntryPoint = "renamefile")]
public static extern int renamefile(string source, string dest);
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Text = renamefile(@"c:\1.txt", @"c:\2.txt").ToString();
}
}
}
附件:vc6 Rename程序/Files/94cool/Rename.rar
Rename.dll/Files/94cool/Rename.dll.rar