这里是我的代码if,用于导航文件和复制文件(如果存在)
本文关键字:文件 复制 如果 存在 导航 我的 代码 if 用于 这里 | 更新日期: 2023-09-27 17:59:24
我有一个错误,请帮帮我。
错误:
1 expected catch or finally
代码:
class Program
{
static void Main()
{
string sourceDir = @"c:'source";//folder directory path//
try
{
var txtFiles = Directory.EnumerateFiles(sourceDir, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".jpg") || s.EndsWith(".bmp") || s.EndsWith(".png") || s.EndsWith(".gif") || s.EndsWith("jpeg"));//identify extension name//
foreach (string currentFile in txtFiles)
{
string fileName = currentFile.Substring(sourceDir.Length + 1);
Directory.GetFiles(currentFile, Path.GetExtension(fileName));
Directory.GetFiles(currentFile, Path.GetFileName(fileName));
Directory.GetFiles(currentFile, Path.GetFileNameWithoutExtension(fileName));
}
}
}
}
}//get files//
您在try-catch中丢失了catch{}
块。
您需要在Try块的末尾添加catch块
try
{
var txtFiles = Directory.EnumerateFiles(sourceDir, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".jpg") || s.EndsWith(".bmp") || s.EndsWith(".png") || s.EndsWith(".gif") || s.EndsWith("jpeg"));//identify extension name//
foreach (string currentFile in txtFiles)
{
string fileName = currentFile.Substring(sourceDir.Length + 1);
Directory.GetFiles(currentFile, Path.GetExtension(fileName));
Directory.GetFiles(currentFile, Path.GetFileName(fileName));
Directory.GetFiles(currentFile, Path.GetFileNameWithoutExtension(fileName));
}
}catch (IOException e)
{
//TODO Something
}
添加捕获块
class Program
{
static void Main()
{
string sourceDir = @"c:'source";//folder directory path//
try
{
var txtFiles = Directory.EnumerateFiles(sourceDir, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".jpg") || s.EndsWith(".bmp") || s.EndsWith(".png") || s.EndsWith(".gif") || s.EndsWith("jpeg"));//identify extension name//
foreach (string currentFile in txtFiles)
{
string fileName = currentFile.Substring(sourceDir.Length + 1);
Directory.GetFiles(currentFile, Path.GetExtension(fileName));
Directory.GetFiles(currentFile, Path.GetFileName(fileName));
Directory.GetFiles(currentFile, Path.GetFileNameWithoutExtension(fileName));
}
}
catch(Exception ex) // Added
{
}
}
}
您缺少catch
块
http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.110).aspx
static void Main()
{
string sourceDir = @"c:'source";//folder directory path//
try
{
//code here
}
catch (Exception ex)
{
//if an error occurs with in the try block, it will handled here.
}
}