Autor Beitrag
raffster
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Di 10.03.09 14:50 
hallo zusammen.

ich habe im internet ein programm gefunden dass mir ein andere programm als administrator ausführt. jetzt wenn ich den pfad angebe vom programm das gestartet werden soll sagt er mir in der fehlermeldung: The directory name is invalid.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
using System;
using System.Security;
using System.ComponentModel;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace RunAsDifferentUser
{
    class Program
    {
        static SecureString getSecureString(String pass)
        {
            SecureString password = new SecureString();
            foreach (Char c in pass)
                password.AppendChar(c);

            return password;
        }

        /* This function is to mask our 
         * password input in the console
         */

        public static string ReadPassword() {
            Stack<string> pass = new Stack<string>();

            for (ConsoleKeyInfo consKeyInfo = Console.ReadKey(true);
              consKeyInfo.Key != ConsoleKey.Enter; consKeyInfo = Console.ReadKey(true))
            {
                if (consKeyInfo.Key == ConsoleKey.Backspace)
                {
                    try
                    {
                        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                        Console.Write(" ");
                        Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                        pass.Pop();
                    }
                    catch (InvalidOperationException ex)
                    {
                        /* Nothing to delete, go back to previous position */
                        Console.SetCursorPosition(Console.CursorLeft + 1, Console.CursorTop);
                    }
                }
                else {
                    Console.Write("*");
                    pass.Push(consKeyInfo.KeyChar.ToString());
                }
            }
            String[] password = pass.ToArray();
            Array.Reverse(password);
            return string.Join(string.Empty, password);
        }



        static void Main(String[] args)
        {
            String domain = null;
            String user = "coeAdmin";
            string sApplicationPfad;

            sApplicationPfad = getApplicationPfad();

            Console.Write("Enter your administrator password: ");
            SecureString password = getSecureString(ReadPassword());

            //string filename = sApplicationPfad + "\\OfflineTempAdmin\\TempAdmin.exe";
            string filename = "C:\\Program Files\\OfflineTempAdmin\\TempAdmin.exe";

            try
            {
                System.Diagnostics.Process.Start(filename, user, password, domain);
                Console.WriteLine("\nOur program is being succesfully run! Exiting.");
            }
            catch (Win32Exception ex)
            {
                Console.Error.WriteLine("\n{0}. Exiting.", ex.Message.ToString());
            }
        }


kann mir da jemand helfen?

grüsse
raffster
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Di 10.03.09 15:25 
Benutze doch Assembly.GetExecutingAssembly().Location (für eine Consolen-Anwendung) bzw. Application.StartUpPath (für eine WinForms-Anwendung) und Path.Combine zum Zusammensetzen eines Dateinamens. (Ich vermute, das Leerzeichen bei "Program Files" zerstört die Pfadangabe.) Jürgen
raffster Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Di 10.03.09 15:39 
user profile iconJüTho hat folgendes geschrieben Zum zitierten Posting springen:
Benutze doch Assembly.GetExecutingAssembly().Location (für eine Consolen-Anwendung) bzw. Application.StartUpPath (für eine WinForms-Anwendung) und Path.Combine zum Zusammensetzen eines Dateinamens. (Ich vermute, das Leerzeichen bei "Program Files" zerstört die Pfadangabe.) Jürgen


hallo jürgen,

danke für deine antwort. ich habe mir auch schon überlegt ob es wegen dem abstand ist bei program files.
nur jetzt hab ich das problem dass ich mit deinen codes nciht anfangen kann. bzw ich weis nicht wo ich die einfügen kann

also ich hab probiert:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
try
{
  System.Diagnostics.Process.Start(Application.StartUpPath(filename), user, password, domain);
  Console.WriteLine("\nOur program is being succesfully run! Exiting.");
}

ausblenden C#-Quelltext
1:
filename = Application.StartUpPath(filename)					


beides geht nicht. ich nehme jetztm al stark an dass ich bei der einsetzung einen fehler mache.

achja: ich starte eine winforms-anwendung.

gruss
raffster
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Di 10.03.09 15:46 
Hallo,

solche Fehler solltest Du selbst in der SDK-Doku/MSDN suchen.

Beispielsweise wirst Du feststellen, dass Application.StartUpPath eine Eigenschaft ist (keine Methode) und einen Pfad (Verzeichnis) angibt. Du benötigst einen Dateinamen; der ist vom Typ string. Also musst Du dort etwas vom Typ string angeben. Das bekommst Du beispielsweise als Ergebnis der Methode Path.Combine; diese benötigt zwei Argumente vom Typ string (Pfad, Datei).

Vielleicht brauchst Du ein paar Grundlagen über Klassen, Eigenschaften, Methoden. Siehe z.B. unter OpenBook Visual C#.

Gruß Jürgen
raffster Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Di 10.03.09 15:49 
aha ok.

ok ich werde mal danach sucher. ich danke dir für di hinweise.

gruss
raffster