Solucionado Compartir Directorios desde C#

25/05/2005 - 07:08 por Nathaly | Informe spam
Chavos... para todos aquellos que mencionaban que no se podia compartir
directorios desde programacion en C# les remito el codigo que si logra
hacer esto...por si lo necesitan algun dia...

using System;

using System.Diagnostics;

using System.Collections;

using System.Runtime.InteropServices;

namespace ADSI_Net

{

class NetApi32

{

public enum NetError

{

NERR_Success = 0,

NERR_BASE = 2100,

NERR_UnknownDevDir = (NERR_BASE + 16),

NERR_DuplicateShare = (NERR_BASE + 18),

NERR_BufTooSmall = (NERR_BASE + 23),

}

public enum SHARE_TYPE : ulong

{

STYPE_DISKTREE = 0,

STYPE_PRINTQ = 1,

STYPE_DEVICE = 2,

STYPE_IPC = 3,

STYPE_SPECIAL = 0x80000000,

}

[ StructLayout( LayoutKind.Sequential )]

public struct SHARE_INFO_502

{

[MarshalAs(UnmanagedType.LPWStr)]

public string shi502_netname;

public uint shi502_type;

[MarshalAs(UnmanagedType.LPWStr)]

public string shi502_remark;

public Int32 shi502_permissions;

public Int32 shi502_max_uses;

public Int32 shi502_current_uses;

[MarshalAs(UnmanagedType.LPWStr)]

public string shi502_path;

public IntPtr shi502_passwd;

public Int32 shi502_reserved;

public IntPtr shi502_security_descriptor;

}

[DllImport("Netapi32.dll")]

public static extern int NetShareAdd(

[MarshalAs(UnmanagedType.LPWStr)]

string strServer, Int32 dwLevel, IntPtr buf, IntPtr parm_err);

}


class AD_ShareUtil

{


public AD_ShareUtil()

{

}


public void CreateShareDirectory(string strServe,string
strShareFoldeWithPath,string strShareNam,string strShareDes)

{

string strServer = @strServe;

string strShareFolder = @strShareFoldeWithPath;

string strShareName = @strShareNam;

string strShareDesc = @strShareDes;

NetApi32.NetError nRetVal = 0;

nRetVal = CreateShare(strServer,strShareFolder,strShareName,strShareDesc,
false);

if (nRetVal == NetApi32.NetError.NERR_Success)

{

Console.WriteLine("Share {0} created", strShareName);

}

else if (nRetVal == NetApi32.NetError.NERR_DuplicateShare)

{

Console.WriteLine("Share {0} already exists",

strShareName);

}

}

NetApi32.NetError CreateShare(string strServer,

string strPath,

string strShareName,

string strShareDesc,

bool bAdmin)

{

NetApi32.SHARE_INFO_502 shInfo
new ADSI_Net.NetApi32.SHARE_INFO_502();

shInfo.shi502_netname = strShareName;

shInfo.shi502_type
(uint)NetApi32.SHARE_TYPE.STYPE_DISKTREE;

if (bAdmin)

{

shInfo.shi502_type
(uint)NetApi32.SHARE_TYPE.STYPE_SPECIAL;

shInfo.shi502_netname += "$";

}

shInfo.shi502_permissions = 0;

shInfo.shi502_path = strPath;

shInfo.shi502_passwd = IntPtr.Zero;

shInfo.shi502_remark = strShareDesc;

shInfo.shi502_max_uses = -1;

shInfo.shi502_security_descriptor = IntPtr.Zero;

string strTargetServer = strServer;

if (strServer.Length != 0)

{

strTargetServer = strServer;

if (strServer[0] != '\\')

{

strTargetServer = "\\\\" + strServer;

}

}

int nRetValue = 0;

// Call Net API to add the share..

int nStSize = Marshal.SizeOf(shInfo);

IntPtr buffer = Marshal.AllocCoTaskMem(nStSize);

Marshal.StructureToPtr(shInfo, buffer, false);

nRetValue = NetApi32.NetShareAdd(strTargetServer, 502,

buffer, IntPtr.Zero);

Marshal.FreeCoTaskMem( buffer );

return (NetApi32.NetError)nRetValue;

}

}

}

thank you JAIME solo se necesitan algunos ajustes para que el codigo
funcione...

Preguntas similare

Leer las respuestas

#1 Tristan
25/05/2005 - 14:27 | Informe spam
Me alegro de que hayas encontrado una solución.

Tenías otras formas, quizá más sencillas, por ejemplo:

ManagementClass win32Share = new ManagementClass("Win32_Share");
win32Share.InvokeMethod("Create", new object[] {
@"c:\Compartida", "Compartida", 0, 5, "Carpeta compartida", null});


Juan Carlos Badiola Saiz
MVP - C#


"Nathaly" wrote:

Chavos... para todos aquellos que mencionaban que no se podia compartir
directorios desde programacion en C# les remito el codigo que si logra
hacer esto...por si lo necesitan algun dia...

using System;

using System.Diagnostics;

using System.Collections;

using System.Runtime.InteropServices;

namespace ADSI_Net

{

class NetApi32

{

public enum NetError

{

NERR_Success = 0,

NERR_BASE = 2100,

NERR_UnknownDevDir = (NERR_BASE + 16),

NERR_DuplicateShare = (NERR_BASE + 18),

NERR_BufTooSmall = (NERR_BASE + 23),

}

public enum SHARE_TYPE : ulong

{

STYPE_DISKTREE = 0,

STYPE_PRINTQ = 1,

STYPE_DEVICE = 2,

STYPE_IPC = 3,

STYPE_SPECIAL = 0x80000000,

}

[ StructLayout( LayoutKind.Sequential )]

public struct SHARE_INFO_502

{

[MarshalAs(UnmanagedType.LPWStr)]

public string shi502_netname;

public uint shi502_type;

[MarshalAs(UnmanagedType.LPWStr)]

public string shi502_remark;

public Int32 shi502_permissions;

public Int32 shi502_max_uses;

public Int32 shi502_current_uses;

[MarshalAs(UnmanagedType.LPWStr)]

public string shi502_path;

public IntPtr shi502_passwd;

public Int32 shi502_reserved;

public IntPtr shi502_security_descriptor;

}

[DllImport("Netapi32.dll")]

public static extern int NetShareAdd(

[MarshalAs(UnmanagedType.LPWStr)]

string strServer, Int32 dwLevel, IntPtr buf, IntPtr parm_err);

}


class AD_ShareUtil

{


public AD_ShareUtil()

{

}


public void CreateShareDirectory(string strServe,string
strShareFoldeWithPath,string strShareNam,string strShareDes)

{

string strServer = @strServe;

string strShareFolder = @strShareFoldeWithPath;

string strShareName = @strShareNam;

string strShareDesc = @strShareDes;

NetApi32.NetError nRetVal = 0;

nRetVal = CreateShare(strServer,strShareFolder,strShareName,strShareDesc,
false);

if (nRetVal == NetApi32.NetError.NERR_Success)

{

Console.WriteLine("Share {0} created", strShareName);

}

else if (nRetVal == NetApi32.NetError.NERR_DuplicateShare)

{

Console.WriteLine("Share {0} already exists",

strShareName);

}

}

NetApi32.NetError CreateShare(string strServer,

string strPath,

string strShareName,

string strShareDesc,

bool bAdmin)

{

NetApi32.SHARE_INFO_502 shInfo >
new ADSI_Net.NetApi32.SHARE_INFO_502();

shInfo.shi502_netname = strShareName;

shInfo.shi502_type >
(uint)NetApi32.SHARE_TYPE.STYPE_DISKTREE;

if (bAdmin)

{

shInfo.shi502_type >
(uint)NetApi32.SHARE_TYPE.STYPE_SPECIAL;

shInfo.shi502_netname += "$";

}

shInfo.shi502_permissions = 0;

shInfo.shi502_path = strPath;

shInfo.shi502_passwd = IntPtr.Zero;

shInfo.shi502_remark = strShareDesc;

shInfo.shi502_max_uses = -1;

shInfo.shi502_security_descriptor = IntPtr.Zero;

string strTargetServer = strServer;

if (strServer.Length != 0)

{

strTargetServer = strServer;

if (strServer[0] != '\\')

{

strTargetServer = "\\\\" + strServer;

}

}

int nRetValue = 0;

// Call Net API to add the share..

int nStSize = Marshal.SizeOf(shInfo);

IntPtr buffer = Marshal.AllocCoTaskMem(nStSize);

Marshal.StructureToPtr(shInfo, buffer, false);

nRetValue = NetApi32.NetShareAdd(strTargetServer, 502,

buffer, IntPtr.Zero);

Marshal.FreeCoTaskMem( buffer );

return (NetApi32.NetError)nRetValue;

}

}

}

thank you JAIME solo se necesitan algunos ajustes para que el codigo
funcione...





email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida