windows下创建网络共享文件例子

    技术2022-05-19  34

    /*********************************************************************************  * author: hjjdebug  * date: 2011  * description:  * 这个例子演示如何创建网络共享文件。它需要逐级创建网络目录  * 分两个步骤:  * 1. 获取网络目录的名称到一个数组  * 2. 依据数组信息逐级创建目录  * 补充: windows 下网络共享采用的是netbios 协议。在此程序中,它是透明的。不用关心它 *********************************************************************************/ #include <windows.h> #include <iostream> #include <vector> #include <string> #include <io.h> #include <direct.h> using namespace std; #pragma warning(disable:4996) string s="192.168.25.155//share//cbs//1//src//1.ts"; //创建网络共享文件。 网络路径要保证逐级创建(当有访问和创建权限时) void MySplit(string &s, char delim, vector<string> &rets); BOOL MyCreateDir(vector<string> &strs); BOOL MyCreateFile(string &s); int main(int argc, char *argv[]) {     if(MyCreateFile(s))     {         cout << "create net share file succeed!" << endl;     }     else     {         cout << "create net share file failed!" << endl;     }     system("pause");     return 0; } BOOL MyCreateFile(string& s) {     vector<string> dirs;     char delim='//';     MySplit(s,delim,dirs);     if(!MyCreateDir(dirs))     {         cout << "error create directory" << endl;     }     FILE *fp = fopen(s.c_str(),"w");     if(!fp)     {         cout << "error create file!" <<endl;     }     return TRUE; } void MySplit(string &s, char delim, vector<string> &rets) {     string subs;     int last=2; // skip first "//";     int index=s.find_first_of(delim,last);     if(index!=string::npos)     {         rets.push_back(s.substr(0,index));         last=index+1;         index=s.find_first_of(delim,last);     }     while(index!=string::npos)     {         subs=s.substr(last,index-last);         rets.push_back(subs);         last=index+1;         index=s.find_first_of(delim,last);     } } //attention: dir="//192.168.25.155"; 网络共享的第一级是不能直接创建的。 // 需要开放共享权限 BOOL MyCreateDir(vector<string> &strs) {     string dir;     dir=strs[0];     for(unsigned int i=1; i<strs.size(); i++)     {         dir+="//"+strs[i];         if(_access(dir.c_str(),0)==-1)         {             int no=GetLastError();             if(no==ERROR_INVALID_NAME)             {                 cout << "error invalid name!" <<endl;             }             if(_mkdir(dir.c_str())!=0)             {                 return FALSE;             }         }     }     return TRUE; }

     

     


    最新回复(0)