C#文件不会复制

时间:2018-09-08 22:07:00

标签: c# file copy

嗨,我正在尝试将文件从一台PC(我的服务器)复制到另一台PC(客户端PC),我已经建立了连接,并且一切正常,建立了连接,但是一旦我尝试复制文件,得到一个错误。

        this.Invoke((MethodInvoker)delegate
        {
          /*  DirectoryInfo dInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
             DirectorySecurity dSecurity = dInfo.GetAccessControl();
             dSecurity.AddAccessRule(new FileSystemAccessRule("everyone", FileSystemRights.FullControl,
                                                              InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
                                                              PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
             dInfo.SetAccessControl(dSecurity);*/
            if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)+"\\Database"))
            {
                if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb"))
                {
                    File.Copy(@"C:\Program Files\Group 27\Bookstore Bargainers\new itrw225 data1.mdb", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
                }

            } 
            else
            {
                Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database");
                File.Copy(@"C:\Program Files\Group 27\Bookstore Bargainers\new itrw225 data1.mdb", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
            }
        });

        Size = new Size(456, 292);
        this.Invoke((MethodInvoker)delegate
        {
            try
            {
                mydb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.0.8.100\\Database\\new itrw225 data1.mdb");
                mydb.Open();
                mydb.Close();
                Properties.Settings.Default.dbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.0.8.100\\Database\\new itrw225 data1.mdb";
                File.Copy("\\192.0.8.100\\Database\\new itrw225 data1.mdb", @"C:\ProgramData\Database\new itrw225 data1.mdb");
                Properties.Settings.Default.Save();
            }
            catch (Exception ex )
            {
                MessageBox.Show(ex + "");
                Properties.Settings.Default.ServerOnline = false;
                mydb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
                MessageBox.Show("Server not detected. Program will function as normally with the database stored on this pc");
                Properties.Settings.Default.dbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb";
                Properties.Settings.Default.Save();
            }
        });

我的错误发生在下一行

File.Copy("\\192.0.8.100\\Database\\new itrw225 data1.mdb", @"C:\ProgramData\Database\new itrw225 data1.mdb");

这是错误消息

System.IO.DirectoryNotFoundException: Could not find a part of the path '\192.0.8.100\Database\new itrw225 data1.mdb

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCopy(String sourceFileName, String        destFileName, Boolean overwrite, Boolean checkHost)
at System.IO.File.Copy(String sourceFileName, String destFileName)
at BookstoreBargainerSystem.Form1.<Form1_Load>b__13_1() in C:\Users\ipadc\Desktop\projek\Bookstore Bargainer System (8 Sept)\BookstoreBargainerSystem\Login.cs:line 79

任何帮助将不胜感激。我100%确定这两个文件都存在

更新的代码

 this.Invoke((MethodInvoker)delegate
        {
          /*  DirectoryInfo dInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
             DirectorySecurity dSecurity = dInfo.GetAccessControl();
             dSecurity.AddAccessRule(new FileSystemAccessRule("everyone", FileSystemRights.FullControl,
                                                              InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
                                                              PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
             dInfo.SetAccessControl(dSecurity);*/
            if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)+"\\Database"))
            {
                if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb"))
                {
                    File.Copy(@"C:\Program Files\Group 27\Bookstore Bargainers\new itrw225 data1.mdb", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
                }

            } 
            else
            {
                Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database");
                File.Copy(@"C:\Program Files\Group 27\Bookstore Bargainers\new itrw225 data1.mdb", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
            }
        });

        Size = new Size(456, 292);
        this.Invoke((MethodInvoker)delegate
        {
            try
            {
                mydb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.0.8.100\\Database\\new itrw225 data1.mdb");
                mydb.Open();
                mydb.Close();
                Properties.Settings.Default.dbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.0.8.100\\Database\\new itrw225 data1.mdb";
                File.Copy("\\\\192.0.8.100\\Database\\new itrw225 data1.mdb", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
                Properties.Settings.Default.Save();
            }
            catch (Exception ex )
            {
                Properties.Settings.Default.ServerOnline = false;
                mydb = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb");
                MessageBox.Show("Server not detected. Program will function as normally with the database stored on this pc");
                Properties.Settings.Default.dbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Database\\new itrw225 data1.mdb";
                Properties.Settings.Default.Save();
            }
        });

0 个答案:

没有答案