Переименовать файлы в папке - C# (177222)
Формулировка задачи:
Здравствуйте.
Как можно выставить путь - место где лежит программа и сами действия производить в этой же папке?
И добавить исключение для exe (саму программу тоже переименовывает) либо что еще лучше оставить исходное расширение файла.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Переименовать_файлы
{
class Program
{
static void Main(string[] args)
{
int number = 1;//переменная для добавления номера к файлу
string added = Console.ReadLine();//переменная для имени файлов формата added+number+extention
string path = @"C:\Новая папка";//как указать папку где лежит exe?
DirectoryInfo my = new DirectoryInfo(path);
foreach (FileInfo o in my.GetFiles())
{
number++;//увеличиваем каждый раз номер
//Console.WriteLine(o.Name);//пишем в консоль названия найденных файлов
string name = o.Name;
File.Move(name, added + number+".jpg");//само переименование
Console.WriteLine(name + "переименован в " + added + number + "расширение");
}
Console.ReadLine();
}
}
}Решение задачи: «Переименовать файлы в папке»
textual
Листинг программы
using System;
using System.IO;
namespace RenameFile
{
class Program
{
static void Main(string[] args)
{
int number = 1;
string Folder = @"C:\TestFrom\";
string Delimeter = "_";
string FileExtension = ".jpg";
try
{
foreach (FileInfo file in (new DirectoryInfo(Folder)).GetFiles())
{
if (file.Extension == ".exe")
{
Console.WriteLine($"{file.Name} не трогаем");
continue;
}
if (file.Extension == "")
{
File.Move(file.FullName, $"{Folder}{file.Name}{Delimeter}{number++.ToString()}{FileExtension}");
continue;
}
File.Move(file.FullName, $"{Folder}{file.Name.Remove(file.Name.LastIndexOf(file.Extension))}{Delimeter}{number++.ToString()}{FileExtension}");
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
Console.ReadKey(true);
}
}
}