Forum > ASP.NET 1.x och 2.x > Språk - C#
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å.
| Skriv utDu 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!
hmm, tack.
ska kika mer på det sen.