Panelen



Forumnavigering
Senast inloggade
RSS
Sponsorer

Forum > ASP.NET 1.x och 2.x > Språk - C#

Hur tar jag bort en fil från servern?

hejsan, jag har en till liten fråga :P
Hur tar jag bort en fil från servern?
Jag har ingen aning om hur man gör.
Jag har googlat lite men inte hittat något jag förstår mig på.

Its not about how hard you can hit, its how hard you can get hit, and still go on.
ID: #14561 | Skapades: 2009-02-20 18:29:20 | Tråden har 2 svar och har lästs 279 gånger.

Shadi
Shadi

Gradering

#14563

Du kan använda dig av System.IO.DirectoryInfo och med det leka sedan. Om du läser dokumentationen så hittar du information om hur man bl.a. hämtar och raderar filer.

Här är en kopierad kod från MSDN som hanterar mappar:

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");
        try
        {
            // Determine whether2irhe directory exists.
            if (di.Exists)
            {
                // Indicate that the directory already exists.
                Console.WriteLine("That path exists already.");
                return;
            }

            // Try to create the directory.
            di.Create();
            Console.WriteLine("The directory was created successfully.");

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");

        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        finally {}
    }
}

Och här är det ett kodexempel som hanterar filer, även detta är från MSDN:

using System;
using System.IO;

class Test
{
   
    public static void Main()
    {
        string path = Path.GetTempFileName();
        FileInfo fi1 = new FileInfo(path);

        if (!fi1.Exists)
        {
            //Create a file to write to.
            using (StreamWriter sw = fi1.CreateText())
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }   
        }

        //Open the file to read from.
        using (StreamReader sr = fi1.OpenText())
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }

        try
        {
            string path2 = Path.GetTempFileName();
            FileInfo fi2 = new FileInfo(path2);

            //Ensure that the target does not exist.
            fi2.Delete();

            //Copy the file.
            fi1.CopyTo(path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            //Delete the newly created file.
            fi2.Delete();
            Console.WriteLine("{0} was successfully deleted.", path2);

        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

Hoppas det hjälpte!

Administratör på AspKoll.se!
ID: #14563 | Skapades: 2009-02-20 18:47:36

hmm, tack.
ska kika mer på det sen.

Its not about how hard you can hit, its how hard you can get hit, and still go on.
ID: #14564 | Skapades: 2009-02-20 18:50:50
© Copyright 2007-2009 Shadi Domat | Version 3.0 | Sidkarta | Policy