我如何解决这个问题我的c#程序在windows xp中没有正常工作,但在Windows 7中它正常工作.. [英] How can i solve this my c# program not properly work in windows xp but in windows 7 it is work proper ..

查看:95
本文介绍了我如何解决这个问题我的c#程序在windows xp中没有正常工作,但在Windows 7中它正常工作..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建文件和文件夹复制program.in所有功能在Windows 7中正常工作与复制功能代码,但当我在Windows XP中运行复制代码dos不起作用。错误来了

i was creating file and folder copy program.in that all function work proper in windows 7 with copy function code , but when i run in windows xp copy code dos not work. error was coming there "

Could not find a part of the path 'E:\\19-02-2016_01-04-19\2016-2017.accdb'"



我怎么能解决这个问题,我认为在XP中有些服务或者某些框架可能不合适。

请帮帮我怎么样我解决了..



我尝试了什么:




how can i solve this,i think in XP some services or may b some framework not proper.
please help me how can i solve..

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;
using System.IO;
using System.IO.Compression;
using System.Security.AccessControl;
namespace Parvati_Engineers
{
    public partial class BILLING_SYSTEM : Form
    {
        OleDbConnection cn;
        OleDbCommand cmd;
        OleDbDataAdapter ad;
        DataSet ds;

        FolderBrowserDialog fdlg = new FolderBrowserDialog();
        OpenFileDialog opd = new OpenFileDialog();
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        OleDbDataReader dr;
        //string strSelectedPath;

        public BILLING_SYSTEM()
        {
            InitializeComponent();
        }
 private void eXITToolStripMenuItem1_Click(object sender, EventArgs e)
        {
          
                string foldername = DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss");

                string targetdir = Convert.ToString(textBox5.Text);
                string direc = targetdir + @"\" + foldername;

                string source = Convert.ToString(textBox6.Text);
                DirectoryInfo dir = new DirectoryInfo(source);

                if (Directory.Exists(source))
                {
                    
                    foreach (string sourceSubFolder in Directory.GetDirectories(source, "*", SearchOption.AllDirectories))
                    {
                      
                       System.IO. Directory.CreateDirectory(sourceSubFolder.Replace(source, direc));

                    }

                  
                    foreach (string sourceFile in Directory.GetFiles(source, "*", SearchOption.AllDirectories))
                    {
                        string destinationFile = sourceFile.Replace(source, direc);
                       System.IO. File.Copy(sourceFile, destinationFile);
                    }

                    MessageBox.Show("copy Successfull.");
                   
                }
            }
    }
}

推荐答案

看看代码片段
string targetdir = Convert.ToString(textBox5.Text);

首先 Text TextBox的属性已经是字符串所以根本不需要使用转换。即使你确实需要将某些内容转换为字符串,通常最好使用对象的 ToString()方法(除非该对象可以为null - 请参阅Richard Deeming的评论下面)



接下来看看

Firstly the Text property of a TextBox is already a string so there is no need at all to use Convert. Even if you do need to "convert" something to a string it's often better to use the object's ToString() method (unless the object can be null - see comment from Richard Deeming below)

Next look at

string direc = targetdir + @"\" + foldername;



构造路径名考虑使用类似


When constructing pathnames consider using something like

string direc = Path.Combine(targetdir, foldername);

有助于解决缺少(或太多)反斜杠的问题。



关于这在Win7上工作但在XP上不起作用的说法...

你已经声明它是造成这个问题的文件副本,但实际上它是 Directory.CreateDirectory 调用正在生成错误。这意味着 E:驱动器未映射到您的XP设置和/或文件夹 19-02-2016_01-04-19



如果你是从用户输入构建路径那么你应该始终检查文件夹是否存在尝试使用它 - 你已经为源文件夹而不是targetdir完成了这个。您可以争辩说,当您创建文件夹时,它们应该在那里 - 但您必须确保驱动器首先存在!



[OP评论后编辑]

尝试打破

which helps take care of the lack of (or too many) backslashes.

As to the claim that this worked on Win7 but does not work on XP ...
You've stated that it is the file copy that is causing this issue but it is in fact the Directory.CreateDirectory call that is generating the error. Which means that the E: drive is not mapped on your XP setup and/or the folder 19-02-2016_01-04-19 does not exist on the XP side of things.

If you are constructing paths from user input then you should always check for the existence of the folders before attempting to use it - you've done this for the source folder but not the targetdir. You could argue that as you are creating the folders they should be there - but you have to make sure that the drive exists first!


Try breaking out the line

System.IO. Directory.CreateDirectory(sourceSubFolder.Replace(source, direc));

到一个变量,例如

string newFolder = sourceSubFoler.Replace(source, direc);
System.IO. Directory.CreateDirectory(newFolder);

并在CreateDirectory上放置一个断点。检查变量的内容,然后使用资源管理器检查直到子文件夹的路径确实存在并且您具有写入权限。还要确保您尝试创建的子文件夹尚不存在(我原本预计会出现不同的错误,但无论如何都要检查它。)



请记住,如果您尝试运行预先构建的可执行文件,那么如果它是在WinXP上构建的,它将在Win7上运行,但不是相反的方式

and put a break point on the CreateDirectory. Examine the contents of the variable then check using Explorer that the path up to the subfolder really does exist AND that you have write access to it. Also ensure that the subfolder you are trying to create doesn't already exist (I would have expected a different error, but it does no harm to check anyway).

Remember that if you are trying to run an pre-built executable then if it was built on WinXP it will run on Win7 but not the other way around


这篇关于我如何解决这个问题我的c#程序在windows xp中没有正常工作,但在Windows 7中它正常工作..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆