PDA

View Full Version : اطلاع از وجود داشتن فایل در سرور



ehsan3030
شنبه 18 مهر 1388, 09:20 صبح
می خواستم بدانم که با چه کدی میشود فهمید یک فایلی در سرور وجود دارد یا نه و به آن دسترسی داشت؟
ممنون

raziee
شنبه 18 مهر 1388, 15:24 عصر
DirectoryInfo dirInfo = new DirectoryInfo(path);
DirectoryInfo[] dirs = dirInfo.GetDirectories();
FileInfo[] files = dirInfo.GetFiles();
با این کد ها میتونی فایل های موجود در پوشه رو بدست بیاری.
یادت باشه که باید از کتابخانه ی System.IO استفاده کنی.

Saber_Fatholahi
شنبه 18 مهر 1388, 16:06 عصر
سلام دوست من این نمونه از MSDN گرفتم امیدوارم به کارت بیاد

using System;
using System.IO;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}

// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } try
{
string path2 = path + "temp";
// Ensure that the target does not exist.
File.Delete(path2);

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

// Delete the newly created file.
File.Delete(path2);
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}