Algunas de las rutinas que en algún momento me han sido de gran utilidad ... ahí os las dejo ...
DETECTAR IMPRESORAS
Imports System.Drawing.Printing
Dim Impresoras As String
'recorre las impresoras instaladas y añade el name a un combo
For Each Impresoras In PrinterSettings.InstalledPrinters
cmbImp.Items.Add(Impresoras.ToString)
Next
CONVERTIR IMAGE A STRING / STRING A IMAGE
Public Function imageToByteArray(ByVal imageIn As System.Drawing.Image, ByVal pformato As System.Drawing.Imaging.ImageFormat) As Byte()
Dim ms As New IO.MemoryStreamTry
imageIn.Save(ms, pformato)
Catch ex As Exception
End Try
Return ms.ToArray()
End Function
Dim returnImage As Image = Nothing
Try
Dim ms As New IO.MemoryStream(byteArrayIn)
returnImage = Image.FromStream(ms)
Catch ex As Exception
End Try
Return returnImage
End Function
Private Function ArrayToString(ByVal bytes() As Byte, Optional ByVal format As String = Nothing) As String
If bytes.Length = 0 Then Return String.Empty
Dim sb As New System.Text.StringBuilder(bytes.Length * 4)
For Each b As Byte In bytes
sb.Append(b.ToString(format))
sb.Append(","c)
Next
sb.Length -= 1
Return sb.ToString()
End Function
Private Function StringToArray(ByVal s As String, Optional ByVal style As System.Globalization.NumberStyles = Nothing) As Byte()
If s.Length = 0 Then Return New Byte() {}
Dim values() As String = s.Split(","c)
Dim bytes(values.Length - 1) As Byte
For index As Integer = 0 To values.Length - 1
bytes(index) = Byte.Parse(values(index), style)
Next
Return bytes
End Function
'Ejemplo Imagen a String-->
dim strImage as string=""
strImage = ArrayToString(imageToByteArray(My.Resources.miImagen, System.Drawing.Imaging.ImageFormat.Png), Nothing)
FORMULARIO COLOR DEGRADADO
Public Sub paintForm(ByVal e As System.Windows.Forms.PaintEventArgs, ByVal direccionGradiente As String, ByVal color1 As Color, ByVal color2 As Color, ByVal frm As Form)
Dim y As Integer = 0
Dim x As Integer = 0
Dim ancho As Integer = frm.Width
Dim alto As Integer = frm.Height
Dim Y_ As Integer = 0, X_ As Integer = 0
Dim _Y As Integer = 0, _X As Integer = 0
Try
Select Case UCase(direccionGradiente)
Case "TOP-BOTTOM"
_Y = y + alto
_X = x
Y_ = y
X_ = x
Case "LEFT-RIGHT"
_Y = y
_X = x + ancho
Y_ = y
X_ = x
Case "BOTTOM-TOP"
_Y = y
_X = x
Y_ = y + alto
X_ = x
Case "RIGHT-LEFT"
_Y = y
_X = x
Y_ = y
X_ = x + alto
Case Else
_Y = y
_X = x
Y_ = y + alto
X_ = x
End Select
Dim colorear As New LinearGradientBrush(New Point(X_, Y_), New Point(_X, _Y), color1, color2)
e.Graphics.FillRectangle(colorear, x, y, ancho, alto)
Catch ex As Exception
End Try
End Sub
'Degradar desde el centro del form
Public Sub paintForm2(ByVal e As System.Windows.Forms.PaintEventArgs, ByVal color1 As Color, ByVal color2 As Color, ByVal frm As Form)
Dim y As Integer = 0
Dim x As Integer = 0
Dim ancho As Integer = frm.Width
Dim alto As Integer = frm.Height
Dim _Y As Integer = 0, _X As Integer = 0
Try
_Y = y + alto
_X = x
Dim colorear As New LinearGradientBrush(New Point(0, 0), New Point(0, _Y / 2), color1, color2)
Dim colorear2 As New LinearGradientBrush(New Point(0, _Y / 2 - 1), New Point(0, _Y), color2, color1)
Dim MED As Integer = alto / 2
e.Graphics.FillRectangle(colorear, x, y, ancho, MED)
e.Graphics.FillRectangle(colorear2, x, MED, ancho, MED)
Catch ex As Exception
End Try
End Sub
Dim y As Integer = 0
Dim x As Integer = 0
Dim ancho As Integer = frm.Width
Dim alto As Integer = frm.Height
Dim Y_ As Integer = 0, X_ As Integer = 0
Dim _Y As Integer = 0, _X As Integer = 0
Try
Select Case UCase(direccionGradiente)
Case "TOP-BOTTOM"
_Y = y + alto
_X = x
Y_ = y
X_ = x
Case "LEFT-RIGHT"
_Y = y
_X = x + ancho
Y_ = y
X_ = x
Case "BOTTOM-TOP"
_Y = y
_X = x
Y_ = y + alto
X_ = x
Case "RIGHT-LEFT"
_Y = y
_X = x
Y_ = y
X_ = x + alto
Case Else
_Y = y
_X = x
Y_ = y + alto
X_ = x
End Select
Dim colorear As New LinearGradientBrush(New Point(X_, Y_), New Point(_X, _Y), color1, color2)
e.Graphics.FillRectangle(colorear, x, y, ancho, alto)
Catch ex As Exception
End Try
End Sub
'Degradar desde el centro del form
Public Sub paintForm2(ByVal e As System.Windows.Forms.PaintEventArgs, ByVal color1 As Color, ByVal color2 As Color, ByVal frm As Form)
Dim y As Integer = 0
Dim x As Integer = 0
Dim ancho As Integer = frm.Width
Dim alto As Integer = frm.Height
Dim _Y As Integer = 0, _X As Integer = 0
Try
_Y = y + alto
_X = x
Dim colorear As New LinearGradientBrush(New Point(0, 0), New Point(0, _Y / 2), color1, color2)
Dim colorear2 As New LinearGradientBrush(New Point(0, _Y / 2 - 1), New Point(0, _Y), color2, color1)
Dim MED As Integer = alto / 2
e.Graphics.FillRectangle(colorear, x, y, ancho, MED)
e.Graphics.FillRectangle(colorear2, x, MED, ancho, MED)
Catch ex As Exception
End Try
End Sub
DETECTAR SI UN PUERTO ESTÁ ABIERTO
Imports System.Net.Sockets
Public Function IsPortOpen(ByVal Host As String, ByVal Port As Integer) As Boolean
Dim m_sck As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
m_sck.Connect(Host, Port)
Return True
Catch ex As SocketException
'Código para manejar error del socket (cerrado, conexión rechazada)
Catch ex As Exception
'Código para manejar otra excepción
End Try
Return False
End Function
http://www.elguille.info/colabora/2007/thepirat_HilosYpuertos.htm
Public Function IsPortOpen(ByVal Host As String, ByVal Port As Integer) As Boolean
Dim m_sck As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
m_sck.Connect(Host, Port)
Return True
Catch ex As SocketException
'Código para manejar error del socket (cerrado, conexión rechazada)
Catch ex As Exception
'Código para manejar otra excepción
End Try
Return False
End Function
http://www.elguille.info/colabora/2007/thepirat_HilosYpuertos.htm
RELLENAR FORMULARIO PDF (C#) CON itextsharp.dll
http://itextpdf.com/
'relCAMPOSxVALOR = nombrecampo&valorcampo|nombrecampo2&valorcampo2|...
public void writePDF(string relCAMPOSxVALOR, string PDForigen, string PDFfin)
{
string pdfTemplate = @PDForigen;
string newFile = @PDFfin;
string fileREL = @relCAMPOSxVALOR;
try
{
System.IO.StreamReader sw = new System.IO.StreamReader(fileREL, Encoding.Default);
relCAMPOSxVALOR = sw.ReadToEnd();
sw.Close();
System.IO.File.Delete(fileREL);
}
catch
{
}
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
try
{
string CAMPO;
string VALOR;
relCAMPOSxVALOR = relCAMPOSxVALOR.Replace("\r\n", "");
CAMPO = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("&", 1));
relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("&", 1) + 1);
VALOR = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("|", 1));
relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("|", 1) + 1);
pdfFormFields.SetField(CAMPO, VALOR);
if (relCAMPOSxVALOR.Length != 0)
{
writePDF_sub(relCAMPOSxVALOR, PDForigen, PDFfin, pdfStamper.AcroFields);
}
}
catch (InvalidCastException e)
{
throw (e); // Rethrowing exception e
}
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
pdfStamper.FormFlattening = false;
// close the pdf
pdfStamper.Close();
}
private void writePDF_sub(string relCAMPOSxVALOR, string PDForigen, string PDFfin, AcroFields pdfFormFields)
{
string pdfTemplate = @PDForigen;
string newFile = @PDFfin;
try
{
string CAMPO;
string VALOR;
if (relCAMPOSxVALOR.IndexOf("&") > 0 && relCAMPOSxVALOR.IndexOf("|") > 0)
{
CAMPO = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("&", 1));
relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("&", 1) + 1);
VALOR = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("|", 1));
relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("|", 1) + 1);
pdfFormFields.SetField(CAMPO, VALOR);
if (relCAMPOSxVALOR.Length != 0)
{
writePDF_sub(relCAMPOSxVALOR, PDForigen, PDFfin, pdfFormFields);
}
}
}
catch (InvalidCastException e)
{
throw (e); // Rethrowing exception e
}
}
'relCAMPOSxVALOR = nombrecampo&valorcampo|nombrecampo2&valorcampo2|...
public void writePDF(string relCAMPOSxVALOR, string PDForigen, string PDFfin)
{
string pdfTemplate = @PDForigen;
string newFile = @PDFfin;
string fileREL = @relCAMPOSxVALOR;
try
{
System.IO.StreamReader sw = new System.IO.StreamReader(fileREL, Encoding.Default);
relCAMPOSxVALOR = sw.ReadToEnd();
sw.Close();
System.IO.File.Delete(fileREL);
}
catch
{
}
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
try
{
string CAMPO;
string VALOR;
relCAMPOSxVALOR = relCAMPOSxVALOR.Replace("\r\n", "");
CAMPO = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("&", 1));
relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("&", 1) + 1);
VALOR = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("|", 1));
relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("|", 1) + 1);
pdfFormFields.SetField(CAMPO, VALOR);
if (relCAMPOSxVALOR.Length != 0)
{
writePDF_sub(relCAMPOSxVALOR, PDForigen, PDFfin, pdfStamper.AcroFields);
}
}
catch (InvalidCastException e)
{
throw (e); // Rethrowing exception e
}
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
pdfStamper.FormFlattening = false;
// close the pdf
pdfStamper.Close();
}
private void writePDF_sub(string relCAMPOSxVALOR, string PDForigen, string PDFfin, AcroFields pdfFormFields)
{
string pdfTemplate = @PDForigen;
string newFile = @PDFfin;
try
{
string CAMPO;
string VALOR;
if (relCAMPOSxVALOR.IndexOf("&") > 0 && relCAMPOSxVALOR.IndexOf("|") > 0)
{
CAMPO = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("&", 1));
relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("&", 1) + 1);
VALOR = relCAMPOSxVALOR.Substring(0, relCAMPOSxVALOR.IndexOf("|", 1));
relCAMPOSxVALOR = relCAMPOSxVALOR.Substring(relCAMPOSxVALOR.IndexOf("|", 1) + 1);
pdfFormFields.SetField(CAMPO, VALOR);
if (relCAMPOSxVALOR.Length != 0)
{
writePDF_sub(relCAMPOSxVALOR, PDForigen, PDFfin, pdfFormFields);
}
}
}
catch (InvalidCastException e)
{
throw (e); // Rethrowing exception e
}
}
WinScp (Ejemplos de uso de la herramienta)
http://winscp.net
'Sincronizar directorio FTP con directorio local
Dim Linea As String = " /command " & Chr(34) & "option batch on" & Chr(34) & " " & Chr(34) & "option confirm off" & Chr(34) & " " & Chr(34) & "reconnecttime 10" & Chr(34) & " " & Chr(34) & "open ftp://" & FTPuser & ":" & FTPpwd & "@" & FTP & Chr(34) & " " & Chr(34) & "synchronize both " & Chr(34) & Chr(34) & LOCALfolder & Chr(34) & Chr(34) & " " & FTPfolder & Chr(34)
Dim exe As New Process
exe.StartInfo = New ProcessStartInfo("winscp.exe", Linea)
exe.Start()
'Envío varíos archivos creando directorio en el sitio FTP
Dim T As String = ""
Dim Linea As String = ""
If Trim(RUTAARCHIVO1) <> "" Then T = T & " " & Chr(34) & "put " & Chr(34) & Chr(34) & RUTAARCHIVO1 & Chr(34)
If Trim(RUTAARCHIVO2) <> "" Then T = T & " " & Chr(34) & "put " & Chr(34) & Chr(34) & RUTAARCHIVO2 & Chr(34)
If Trim(RUTAARCHIVO3) <> "" Then T = T & " " & Chr(34) & "put " & Chr(34) & Chr(34) & RUTAARCHIVO3 & Chr(34)
If Trim(RUTAARCHIVO4) <> "" Then T = T & " " & Chr(34) & "put " & Chr(34) & Chr(34) & RUTAARCHIVO4 & Chr(34)
Linea = " /console /command " & Chr(34) & "option batch on" & Chr(34) & " " & Chr(34) & "option confirm off" & Chr(34) & " " & Chr(34) & "open ftp://" & FTPuser & ":" & FTPpwd & "@" & FTP & Chr(34) & " " & Chr(34) & "mkdir " & FTPdirectorio & " " & Chr(34) & "cd " & FTPdirectorio & Chr(34) & " " & Chr(34) & T & " " & Chr(34) & " exit" & Chr(34) & Chr(34) & Chr(34)
Dim exe As New Process
exe.StartInfo = New ProcessStartInfo("winscp.exe", Linea)
exe.Start()
'Sincronizar directorio FTP con directorio local
Dim Linea As String = " /command " & Chr(34) & "option batch on" & Chr(34) & " " & Chr(34) & "option confirm off" & Chr(34) & " " & Chr(34) & "reconnecttime 10" & Chr(34) & " " & Chr(34) & "open ftp://" & FTPuser & ":" & FTPpwd & "@" & FTP & Chr(34) & " " & Chr(34) & "synchronize both " & Chr(34) & Chr(34) & LOCALfolder & Chr(34) & Chr(34) & " " & FTPfolder & Chr(34)
Dim exe As New Process
exe.StartInfo = New ProcessStartInfo("winscp.exe", Linea)
exe.Start()
'Envío varíos archivos creando directorio en el sitio FTP
Dim T As String = ""
Dim Linea As String = ""
If Trim(RUTAARCHIVO1) <> "" Then T = T & " " & Chr(34) & "put " & Chr(34) & Chr(34) & RUTAARCHIVO1 & Chr(34)
If Trim(RUTAARCHIVO2) <> "" Then T = T & " " & Chr(34) & "put " & Chr(34) & Chr(34) & RUTAARCHIVO2 & Chr(34)
If Trim(RUTAARCHIVO3) <> "" Then T = T & " " & Chr(34) & "put " & Chr(34) & Chr(34) & RUTAARCHIVO3 & Chr(34)
If Trim(RUTAARCHIVO4) <> "" Then T = T & " " & Chr(34) & "put " & Chr(34) & Chr(34) & RUTAARCHIVO4 & Chr(34)
Linea = " /console /command " & Chr(34) & "option batch on" & Chr(34) & " " & Chr(34) & "option confirm off" & Chr(34) & " " & Chr(34) & "open ftp://" & FTPuser & ":" & FTPpwd & "@" & FTP & Chr(34) & " " & Chr(34) & "mkdir " & FTPdirectorio & " " & Chr(34) & "cd " & FTPdirectorio & Chr(34) & " " & Chr(34) & T & " " & Chr(34) & " exit" & Chr(34) & Chr(34) & Chr(34)
Dim exe As New Process
exe.StartInfo = New ProcessStartInfo("winscp.exe", Linea)
exe.Start()
Suscribirse a:
Entradas (Atom)