我有简单的 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 { }
}
}