Avatar billede Slettet bruger
30. december 2006 - 10:55 Der er 20 kommentarer og
1 løsning

Åbne fil

Hej eksperter!


Jeg vil have et program til at åbne en fil for brugeren.

Når jeg anvender
System.IO.File.Open("C:\\en_fil.zip", FileMode.Open)
åbnes C:\en_fil.zip ikke...

Hvad er der galt?
Har jeg kun startet en strøm?
Hvordan får jeg det til at virke?
Avatar billede Spotgun Seniormester
30. december 2006 - 11:21 #1
Du skal bruge System.Diagnostics.Process i stedet for. Som du selv er inde på, så benyttes System.IO.File.Open til at åbne en fil for læsning internt i programmet.

Her er et hurtigt eksempel:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace ProcessOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            Process proc = new Process();
            proc.StartInfo = new ProcessStartInfo();
            proc.StartInfo.FileName = "testfil.doc";
            proc.StartInfo.CreateNoWindow = true;
            proc.Start();
        }
    }
}

Kig nærmere på Process.StartInfo. Der er en masse muligheder for at indstille hvordan filen/processen skal åbnes.
Avatar billede Slettet bruger
30. december 2006 - 11:30 #2
Okaj
Avatar billede Slettet bruger
30. december 2006 - 12:01 #3
Hov!

Error    1    The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    149    17    Acer Aspire 9410 series software package
Error    2    The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    149    39    Acer Aspire 9410 series software package
Error    3    The type or namespace name 'ProcessStartInfo' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    150    41    Acer Aspire 9410 series software package
Avatar billede Slettet bruger
30. december 2006 - 12:02 #4
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

Process PROCESS = new Process();
PROCESS.StartInfo = new ProcessStartInfo();
PROCESS.StartInfo.FileName = DESTINATION;
PROCESS.StartInfo.CreateNoWindow = true;
PROCESS.Start();

Hvor DESTINATION = filens placering (en string)


Retunerer:

Error    1    The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    149    17    Acer Aspire 9410 series software package
Error    2    The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    149    39    Acer Aspire 9410 series software package
Error    3    The type or namespace name 'ProcessStartInfo' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    150    41    Acer Aspire 9410 series software package
Avatar billede Spotgun Seniormester
30. december 2006 - 12:03 #5
Har du "Using System.Diagnostics;" med i toppen?
Avatar billede Spotgun Seniormester
30. december 2006 - 12:04 #6
Nå, det havde du så :D

Har du lavet en reference til System.Diagnostics dll'en?
Avatar billede Slettet bruger
30. december 2006 - 12:45 #7
Hvad er det? Og hvordan? Og nej...
Avatar billede Slettet bruger
30. december 2006 - 12:45 #8
System.Diagnostics.Process PROCESS = new Process(); ?
Avatar billede Spotgun Seniormester
30. december 2006 - 12:48 #9
Prøv lige at paste hele din .cs fil ind her, så er det lidt nemmere at debugge ;)
Avatar billede Slettet bruger
30. december 2006 - 12:51 #10
Hele min fil?
Avatar billede Spotgun Seniormester
30. december 2006 - 12:52 #11
ja, bare den .cs fil, hvor du vil bruge System.Diagnostics...
Avatar billede Slettet bruger
30. december 2006 - 12:53 #12
Eh, ok...
Avatar billede Slettet bruger
30. december 2006 - 12:53 #13
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Acer_Aspire_9410_series_software_package
{
    class Program
    {

        static string SOURCE_FILE = "acer_aspire_9410_series_software.zip";
        static string DESTINATION_FILE = "acer_aspire_9410_series_software.zip";
       
        static string SOURCE = Path.GetFullPath(SOURCE_FILE);

        static void Main()
        {
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            Console.WriteLine("Welcome to the Acer Aspire 9410 series software package installer!\nThis tool will copy a complete collection of drivers and other basic software\nfor Acer Aspire 9410 Notebooks to a selectable destination on you hard drive.");
            Console.SetCursorPosition(1, 8);
            Console.WriteLine("- Press <ENTER> to proceed.");
            Console.SetCursorPosition(1, 10);
            Console.WriteLine("- Press any other key to cancel.");
            Console.CursorVisible = false;
            if (Console.ReadKey().Key.ToString() == "Enter")
            {
                GET_DESTINATION("");
            }
        }

        static void GET_DESTINATION(string ERROR)
        {
            string DESTINATION;

            Console.Clear();
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            if (ERROR == "")
            {
                Console.WriteLine("You must now choose the destination directory of the package.");
            }
            else
            {
                Console.WriteLine(ERROR);
            }
            Console.SetCursorPosition(1, 6);
            Console.WriteLine("- Enter the path of the destination directory and press <ENTER> to proceed.");
            Console.SetCursorPosition(3, 7);
            Console.WriteLine("Destination directory:");
            Console.SetCursorPosition(26, 7);
            Console.CursorVisible = true;
            DESTINATION = Console.ReadLine();
            if (DESTINATION != "")
            {
                if (DESTINATION.Substring(DESTINATION.Length-1, 1) != "/" && DESTINATION.Substring(DESTINATION.Length-1, 1) != "\\" && DESTINATION.IndexOf(".") == -1)
                {
                    CONFIRM_DESTINATION(DESTINATION + "\\" + DESTINATION_FILE);
                }
                else
                {
                    GET_DESTINATION("Error: The destination string must not end with \'/\' or \'\\\' or contain \'.\'.");
                }
            }
            else
            {
                GET_DESTINATION("Error: You must choose the destination directory of the package.");
            }

        }

        static void CONFIRM_DESTINATION(string DESTINATION)
        {
            string DISPLAY_SOURCE;
            string DISPLAY_DESTINATION;

            if (SOURCE.Length <= 50)
            {
                DISPLAY_SOURCE = SOURCE;
            }
            else
            {
                DISPLAY_SOURCE = SOURCE.Substring(0, 50) + " (...)";
            }
            if (DESTINATION.Length <= 50)
            {
                DISPLAY_DESTINATION = DESTINATION;
            }
            else
            {
                DISPLAY_DESTINATION = DESTINATION.Substring(0, 50) + " (...)";
            }

            Console.Clear();
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            Console.WriteLine("The Acer Aspire 9410 series software package will be copied to this directory:\n" + DISPLAY_DESTINATION);
            Console.SetCursorPosition(1, 7);
            Console.WriteLine("- Press <ENTER> to start copying to the selected directory.");
            Console.SetCursorPosition(1, 9);
            Console.WriteLine("- Press any other key to select another destination.");
            Console.CursorVisible = false;
            if (Console.ReadKey().Key.ToString() == "Enter")
            {
                COPY_PACKAGE(DESTINATION, DISPLAY_SOURCE, DISPLAY_DESTINATION);
            }
            else
            {
                GET_DESTINATION("");
            }
        }

        static void COPY_PACKAGE(string DESTINATION, string DISPLAY_SOURCE, string DISPLAY_DESTINATION)
        {
            Console.Clear();
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            Console.WriteLine("Copying files now... Please wait.");
            Console.SetCursorPosition(0, 5);
            Console.WriteLine("Source:");
            Console.SetCursorPosition(13, 5);
            Console.WriteLine(DISPLAY_SOURCE);
            Console.SetCursorPosition(0, 6);
            Console.WriteLine("Destination:");
            Console.SetCursorPosition(13, 6);
            Console.WriteLine(DISPLAY_DESTINATION);

            System.IO.File.Copy(SOURCE, DESTINATION, true);

            Console.Clear();
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            Console.WriteLine("Copying files now... Success!");
            Console.SetCursorPosition(0, 5);
            Console.WriteLine("Source:");
            Console.SetCursorPosition(13, 5);
            Console.WriteLine(DISPLAY_SOURCE);
            Console.SetCursorPosition(0, 6);
            Console.WriteLine("Destination:");
            Console.SetCursorPosition(13, 6);
            Console.WriteLine(DISPLAY_DESTINATION);
            Console.SetCursorPosition(1, 9);
            Console.WriteLine("- Press <ENTER> to finish and open the package.");
            Console.SetCursorPosition(1, 11);
            Console.WriteLine("- Press any other key to finish without opening the package.");
            if (Console.ReadKey().Key.ToString() == "Enter")
            {
                Process PROCESS = new Process();
                PROCESS.StartInfo = new ProcessStartInfo();
                PROCESS.StartInfo.FileName = DESTINATION;
                PROCESS.StartInfo.CreateNoWindow = true;
                PROCESS.Start();
            }
        }

    }
}
Avatar billede Slettet bruger
30. december 2006 - 12:53 #14
Error    1    The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    149    17    Acer Aspire 9410 series software package
Error    2    The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    149    39    Acer Aspire 9410 series software package
Error    3    The type or namespace name 'ProcessStartInfo' could not be found (are you missing a using directive or an assembly reference?)    C:\Documents and Settings\Toke\Dokumenter\Visual Studio 2005\Projects\Acer Aspire 9410 series software package\Acer Aspire 9410 series software package\Main.cs    150    41    Acer Aspire 9410 series software package
Avatar billede Spotgun Seniormester
30. december 2006 - 13:03 #15
Kig i Solution Explorer i Visual Studio, og fold mappen "References" ud.
Skriv så her hvilke referencer der findes derunder.
Avatar billede Slettet bruger
30. december 2006 - 13:10 #16
Okaj, kikker lige om lidt...
Avatar billede Spotgun Seniormester
30. december 2006 - 13:25 #17
Du skal i hvert fald som minimum have en reference til "System". Hvis den ikke er der, så højreklik på References og vælg "Add Reference" og scroll ned til "System" under fanebladet ".Net" og tryk "OK".
Avatar billede Slettet bruger
30. december 2006 - 13:25 #18
Øh, ingen?
Avatar billede Spotgun Seniormester
30. december 2006 - 13:32 #19
Tilføj så en reference til System, så burde det virke.
Avatar billede Slettet bruger
30. december 2006 - 14:27 #20
Ok, prøver...
Avatar billede Slettet bruger
30. december 2006 - 14:28 #21
ja!


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Acer_Aspire_9410_series_software_package
{
    class Program
    {

        static string SOURCE_FILE = "acer_aspire_9410_series_software.zip";
        static string DESTINATION_FILE = "acer_aspire_9410_series_software.zip";
       
        static string SOURCE = Path.GetFullPath(SOURCE_FILE);

        static void Main()
        {
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            Console.WriteLine("Welcome to the Acer Aspire 9410 series software package installer!\nThis tool will copy a complete collection of drivers and other basic software\nfor Acer Aspire 9410 Notebooks to a selectable destination on you hard drive.");
            Console.SetCursorPosition(1, 8);
            Console.WriteLine("- Press <ENTER> to proceed.");
            Console.SetCursorPosition(1, 10);
            Console.WriteLine("- Press any other key to cancel.");
            Console.CursorVisible = false;
            if (Console.ReadKey().Key.ToString() == "Enter")
            {
                GET_DESTINATION("");
            }
        }

        static void GET_DESTINATION(string ERROR)
        {
            string DESTINATION;

            Console.Clear();
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            if (ERROR == "")
            {
                Console.WriteLine("You must now choose the destination directory of the package.");
            }
            else
            {
                Console.WriteLine(ERROR);
            }
            Console.SetCursorPosition(1, 6);
            Console.WriteLine("- Enter the path of the destination directory and press <ENTER> to proceed.");
            Console.SetCursorPosition(3, 7);
            Console.WriteLine("Destination directory:");
            Console.SetCursorPosition(26, 7);
            Console.CursorVisible = true;
            DESTINATION = Console.ReadLine();
            if (DESTINATION != "")
            {
                if (DESTINATION.Substring(DESTINATION.Length-1, 1) != "/" && DESTINATION.Substring(DESTINATION.Length-1, 1) != "\\" && DESTINATION.IndexOf(".") == -1)
                {
                    CONFIRM_DESTINATION(DESTINATION + "\\" + DESTINATION_FILE);
                }
                else
                {
                    GET_DESTINATION("Error: The destination string must not end with \'/\' or \'\\\' or contain \'.\'.");
                }
            }
            else
            {
                GET_DESTINATION("Error: You must choose the destination directory of the package.");
            }

        }

        static void CONFIRM_DESTINATION(string DESTINATION)
        {
            string DISPLAY_SOURCE;
            string DISPLAY_DESTINATION;

            if (SOURCE.Length <= 50)
            {
                DISPLAY_SOURCE = SOURCE;
            }
            else
            {
                DISPLAY_SOURCE = SOURCE.Substring(0, 50) + " (...)";
            }
            if (DESTINATION.Length <= 50)
            {
                DISPLAY_DESTINATION = DESTINATION;
            }
            else
            {
                DISPLAY_DESTINATION = DESTINATION.Substring(0, 50) + " (...)";
            }

            Console.Clear();
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            Console.WriteLine("The Acer Aspire 9410 series software package will be copied to this directory:\n" + DISPLAY_DESTINATION);
            Console.SetCursorPosition(1, 7);
            Console.WriteLine("- Press <ENTER> to start copying to the selected directory.");
            Console.SetCursorPosition(1, 9);
            Console.WriteLine("- Press any other key to select another destination.");
            Console.CursorVisible = false;
            if (Console.ReadKey().Key.ToString() == "Enter")
            {
                COPY_PACKAGE(DESTINATION, DISPLAY_SOURCE, DISPLAY_DESTINATION);
            }
            else
            {
                GET_DESTINATION("");
            }
        }

        static void COPY_PACKAGE(string DESTINATION, string DISPLAY_SOURCE, string DISPLAY_DESTINATION)
        {
            Console.Clear();
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            Console.WriteLine("Copying files now... Please wait.");
            Console.SetCursorPosition(0, 5);
            Console.WriteLine("Source:");
            Console.SetCursorPosition(13, 5);
            Console.WriteLine(DISPLAY_SOURCE);
            Console.SetCursorPosition(0, 6);
            Console.WriteLine("Destination:");
            Console.SetCursorPosition(13, 6);
            Console.WriteLine(DISPLAY_DESTINATION);

            System.IO.File.Copy(SOURCE, DESTINATION, true);

            Console.Clear();
            Console.WriteLine("ACER ASPIRE 9410 SERIES SOFTWARE PACKAGE");
            Console.SetCursorPosition(0, 3);
            Console.WriteLine("Copying files now... Success!");
            Console.SetCursorPosition(0, 5);
            Console.WriteLine("Source:");
            Console.SetCursorPosition(13, 5);
            Console.WriteLine(DISPLAY_SOURCE);
            Console.SetCursorPosition(0, 6);
            Console.WriteLine("Destination:");
            Console.SetCursorPosition(13, 6);
            Console.WriteLine(DISPLAY_DESTINATION);
            Console.SetCursorPosition(1, 9);
            Console.WriteLine("- Press <ENTER> to finish and open the package.");
            Console.SetCursorPosition(1, 11);
            Console.WriteLine("- Press any other key to finish without opening the package.");
            if (Console.ReadKey().Key.ToString() == "Enter")
            {
                Process PROCESS = new Process();
                PROCESS.StartInfo = new ProcessStartInfo();
                PROCESS.StartInfo.FileName = DESTINATION;
                PROCESS.StartInfo.CreateNoWindow = true;
                PROCESS.Start();
            }
        }

    }
}


Og så en refference til System :)

Takker!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester