且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c#重命名文件 - 抛弃MoveTo,而引用vc 中rename生成rename.dll

更新时间:2022-08-13 12:12:55

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[1000], newname[1000];  
    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();
        }
    }
}




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2011/04/16/2018574.html,如需转载请自行联系原作者