0 امتیاز
سلام.من میخاستم یک فایل در یک درایو با سی شارپ کپی کنم واگر به هر دلیلی فایل کپی نشد برنامه خطا نگیرد و پیغام کپی نشد بدهد و برنامه بسته نشود

2 پاسخ

0 امتیاز

برای کپی کردن :

using System;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			try
			{
				File.Copy("c:\\temp.txt", "c:\\copytemp.txt", true);
				File.Delete("c:\\copytemp.txt");
			}
			catch (System.IO.FileNotFoundException ex)
			{
				MessageBox.Show(ex.StackTrace );
			}

		}
	}
}

برای جابجا کردن (move ) :


// Simple synchronous file move operations with no user interface.
public class SimpleFileMove
{
    static void Main()
    {
        string sourceFile = @"C:\Users\Public\public\test.txt";
        string destinationFile = @"C:\Users\Public\private\test.txt";

        // To move a file or folder to a new location:
        System.IO.File.Move(sourceFile, destinationFile);

        // To move an entire directory. To programmatically modify or combine
        // path strings, use the System.IO.Path class.
        System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");
    }
}

برای حذف کردن :

public class SimpleFileDelete
{
    static void Main()
    {
        // Delete a file by using File class static method...
        if(System.IO.File.Exists(@"C:\Users\Public\DeleteTest\test.txt"))
        {
            // Use a try block to catch IOExceptions, to
            // handle the case of the file already being
            // opened by another process.
            try
            {
                System.IO.File.Delete(@"C:\Users\Public\DeleteTest\test.txt");
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }

        // ...or by using FileInfo instance method.
        System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\Users\Public\DeleteTest\test2.txt");
        try
        {
            fi.Delete();
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }

        // Delete a directory. Must be writable or empty.
        try
        {
            System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest");
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }
        // Delete a directory and all subdirectories with Directory static method...
        if(System.IO.Directory.Exists(@"C:\Users\Public\DeleteTest"))
        {
            try
            {
                System.IO.Directory.Delete(@"C:\Users\Public\DeleteTest", true);
            }

            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        // ...or with DirectoryInfo instance method.
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\Users\Public\public");
        // Delete this dir and all subdirs.
        try
        {
            di.Delete(true);
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }

    }
}


 

0 امتیاز
برای اطلاعات بیشتر به آدرسهای زیر هم میتونید رجوع کنید :

http://csharp.net-informations.com/file/csharp-filestream-class.htm

http://www.homeandlearn.co.uk/csharp/csharp_s11p4.html
سوال جدید

2,337 سوال

2,871 پاسخ

3,725 دیدگاه

3,924 کاربر

دسته بندی ها

...