mboost-dp1

Visual Basic: Kode der skriver en fil under load


Gå til bund
Gravatar #1 - xXgmzorXx
26. feb. 2010 05:52
Jeg har brug for en kode, der skriver en "Drive.vbs" fil C:\

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub


Drive.vbs skal også have det her kodet ind:
Set oWMP = CreateObject("WMPlayer.OCX.7" )
Set colCDROMs = oWMP.cdromCollection
colCDROMs.Item(d).Eject
colCDROMs.Item(d).Eject
set owmp = nothing
set colCDROMs = nothing


Koden skal IKKE åbnes i starten af programmet, den skal bare skrives.
Gravatar #2 - Windcape
26. feb. 2010 06:11
Din første codesnippet er VB.NET , den anden er VBScript.

Hvad vil du have svaret i?
Gravatar #3 - Windcape
26. feb. 2010 06:19
VB.NET


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim vbs As String
vbs = "Set oWMP = CreateObject(""WMPlayer.OCX.7"")" & vbCrLf & _
"colCDROMs = oWMP.cdromCollection" & vbCrLf & _
"colCDROMs.Item(d).Eject()" & vbCrLf & _
"colCDROMs.Item(d).Eject()" & vbCrLf & _
"owmp = Nothing" & vbCrLf & _
"colCDROMs = Nothing"

System.IO.File.WriteAllText("C:\\Drive.vbs", vbs)

End Sub
End Class
Gravatar #4 - xXgmzorXx
26. feb. 2010 06:21
(VB.net skal skrive en fil i VBScript)

-Thanks for that ;)
Gravatar #5 - Windcape
26. feb. 2010 06:23
Bemærk at du ikke kan skrive til C:\ uden elevated permissions på maskiner med UAC aktiveret! (Vista og Windows 7)

Og der skal en del mere kode til at spørge om administrative rettigheder direkte fra dit program.

Gravatar #6 - Windcape
26. feb. 2010 06:33
Eksempel med UAC promt:

VB.NET


Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Diagnostics
Imports System.IO

Namespace WindowsFormsApplication4
    Public Partial Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub
       
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsAdmin() Then
                AddShieldToButton(WriteButton)
            End If
        End Sub
       
        Private Sub WriteButton_Click(ByVal sender As Object, ByVal e As EventArgs)
            If IsAdmin() Then
                Dim vbs As String = "Set oWMP = CreateObject(""WMPlayer.OCX.7"")" & vbCr & vbLf & "Set colCDROMs = oWMP.cdromCollection" & vbCr & vbLf & "colCDROMs.Item(d).Eject" & vbCr & vbLf & "colCDROMs.Item(d).Eject" & vbCr & vbLf & "set owmp = nothing" & vbCr & vbLf & "set colCDROMs = nothing"
               
                File.WriteAllText("C:\Drive.vbs", vbs)
            Else
                RestartElevated()
            End If
        End Sub
       
        <DllImport("user32")> _
        Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As UInt32, ByVal wParam As UInt32, ByVal lParam As UInt32) As UInt32
        End Function
       
        Friend Const BCM_FIRST As Integer = &H1600
        'Normal button
        Friend Const BCM_SETSHIELD As Integer = (BCM_FIRST + &Hc)
        'Elevated button
        Friend Shared Function IsAdmin() As Boolean
            Dim id As WindowsIdentity = WindowsIdentity.GetCurrent()
            Dim p As New WindowsPrincipal(id)
           
            Return p.IsInRole(WindowsBuiltInRole.Administrator)
        End Function
       
        Friend Shared Sub AddShieldToButton(ByVal b As Button)
            b.FlatStyle = FlatStyle.System
            SendMessage(b.Handle, BCM_SETSHIELD, 0, &Hffffffff)
        End Sub
       
        Friend Shared Sub RestartElevated()
            Dim startInfo As New ProcessStartInfo()
           
            Try
                Dim p As Process = Process.Start(startInfo)
            Catch generatedExceptionName As Win32Exception
                Exit Sub
            End Try
           
            Application.[Exit]()
        End Sub
    End Class
End Namespace
Gravatar #7 - Windcape
26. feb. 2010 06:34
C#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Diagnostics;
using System.IO;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
if (!IsAdmin())
{
AddShieldToButton(WriteButton);
}
}

private void WriteButton_Click(object sender, EventArgs e)
{
if (IsAdmin())
{
string vbs = "Set oWMP = CreateObject(\"WMPlayer.OCX.7\")\r\n" +
"Set colCDROMs = oWMP.cdromCollection\r\n" +
"colCDROMs.Item(d).Eject\r\n" +
"colCDROMs.Item(d).Eject\r\n" +
"set owmp = nothing\r\n" +
"set colCDROMs = nothing";

File.WriteAllText("C:\\Drive.vbs", vbs);
}
else
{
RestartElevated();
}
}

[DllImport("user32")]
public static extern UInt32 SendMessage(
IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam
);

internal const int BCM_FIRST = 0x1600; //Normal button
internal const int BCM_SETSHIELD = (BCM_FIRST + 0x000C); //Elevated button

static internal bool IsAdmin()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal p = new WindowsPrincipal(id);

return p.IsInRole(WindowsBuiltInRole.Administrator);
}

static internal void AddShieldToButton(Button b)
{
b.FlatStyle = FlatStyle.System;
SendMessage(b.Handle, BCM_SETSHIELD, 0, 0xFFFFFFFF);
}

internal static void RestartElevated()
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Application.ExecutablePath,
Verb = "runas"
};

try
{
Process p = Process.Start(startInfo);
}
catch (Win32Exception)
{
return;
}

Application.Exit();
}
}
}

Gravatar #8 - Windcape
26. feb. 2010 06:35
VB.NET delen er konverteret via. http://www.developerfusion.com/tools/convert/cshar...

Jeg var for doven til at lære hvordan man laver interop i VB.NET, så jeg skrev det i C# først ;-)
Gravatar #9 - xXgmzorXx
26. feb. 2010 23:49
#5

Ellers kan man bare gå ind i egenskaber, kompatibilitet og clicke på "Kør dette program som Administrator"
-Hvis man under debug i Visual Studio ville have admin rettigheder gør man bare det samme med Visual Studio.
Gå til top

Opret dig som bruger i dag

Det er gratis, og du binder dig ikke til noget.

Når du er oprettet som bruger, får du adgang til en lang række af sidens andre muligheder, såsom at udforme siden efter eget ønske og deltage i diskussionerne.

Opret Bruger Login