1

我有简单的 c# 控制台应用程序,它将文件从闪存盘复制到另一个闪存盘。如果我在 Visual Studio 中运行这个应用程序,一切都很好,但是如果我想通过 .exe 文件运行它,应用程序不会启动并且什么也没有发生。我已经尝试过调试模式、发布模式和更改框架以降低一个。

还有源代码:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("");
        Helper hlp = new Helper();
        for (int i = 0; i < hlp.GetDevices().Count; i++)
        {
            Console.WriteLine(hlp.GetDevices()[i]);
        }

        if (hlp.GetDevices().Count.Equals(2))
        {
            if (Directory.GetCurrentDirectory().Equals(hlp.GetDevices()[0]))
            {
                hlp.DirectoryCopy(hlp.GetDevices()[1].ToString(), ".", true);
            }
            else
            {
                hlp.DirectoryCopy(hlp.GetDevices()[0].ToString(), ".", true);
            }
            Console.WriteLine("Operace dokoncena.");
        }
        else
        {
            Console.WriteLine("Vlozte do PC 2 flashdisky.");
        }
        Console.ReadLine();
    }
}

和 Helper.cs:

class Helper
{
    public List<string> GetDevices()
    {
        List<string> devices = new List<string>();
        var driveList = DriveInfo.GetDrives();

        foreach (DriveInfo drive in driveList)
        {
            if (drive.DriveType == DriveType.Removable)
            {
                devices.Add(drive.Name);
            }
        }
        return devices;
    }

    public void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        try
        {
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException("Zdrojovy adresar neexistuje!" + sourceDirName);
            }

            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                File.SetAttributes(file.DirectoryName, FileAttributes.Normal);
                file.CopyTo(temppath, true);
            }

            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }
        catch { }
    }
}
4

3 回答 3

0

您是否尝试在管理员模式下运行它?

并且您的程序的一项改进存储设备列表比它更快;-)

List<string> deviceList = hlp.GetDevices();

当连接很多设备时,您的程序会更快。

于 2015-12-04T08:40:44.957 回答
0

我尝试关闭 Windows Smart 屏幕,应用程序开始工作 :)

于 2015-12-08T13:14:26.673 回答
-1

您是否尝试过不使用任何闪存驱动器?只是为了获得“Vlozte do PC 2 flashdisky”。信息。因为您的代码在 .NET 框架 4.5.2 的 Visual Studio 2015 中为我正常工作

于 2015-12-04T08:47:14.607 回答