从数据库中重命名1个文件多个不同的名称

时间:2014-09-22 16:58:38

标签: database file console-application rename

所以我知道如何重命名多个文件甚至单个文件。我知道如何增加名字。但我正在努力的是重命名1个单独的文件,从数据库中获取许多不同的名称。一个示例是将textFile.txt重命名为来自db strings的100个不同("file", "dog", "cat", "etc...")。在我建立了与数据库的连接后,我需要帮助,我从哪里开始?我感觉我需要某种IEnumerable,然后像forearch一样迭代。只是不确定如何写。仅供我使用ConsoleApp。谢谢!

class Program
    {
        public static void Main(string[] args)
        {
            SqlConnection Locations = new SqlConnection(ConsoleApplication1.Properties.Settings.Default.locationsDB);

            string sourcePath = @"C:\Users\1\Desktop\a";
            string targetPath = @"C:\Users\1\Desktop\b";

            CopyDirectory(sourcePath, targetPath);
        }

        public static void CopyDirectory(string sourcePath, string targetPath)
        {
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
            if (System.IO.Directory.Exists(sourcePath))
            {
                SqlConnection Locations = new SqlConnection(ConsoleApplication1.Properties.Settings.Default.locationsDB);

                string[] files = System.IO.Directory.GetFiles(sourcePath);

                foreach (string sourceFilePath in files)
                {
                    for (int i = 1; i <= 4; ++i)
                    {
                        string fileExt = System.IO.Path.GetExtension(sourceFilePath);
                        string sourceFileName = System.IO.Path.GetFileName(sourceFilePath);
                        string targetFilePath = System.IO.Path.Combine(targetPath, "10000" + i + fileExt);
                        System.IO.File.Copy(sourceFilePath, targetFilePath);
                    }
                    break;
                }
            }
        }
    }

输出看起来像(

100001
100002
100003
100004
110001
110002
110003
110004
111001
111002
111003
111004
etc...

1 个答案:

答案 0 :(得分:0)

你应该摆脱数据库连接,只需使用.txt文件,然后设置string[] = File.ReadAllLines(file)。然后使用foreach循环而不是for循环遍历每一行。

class Program
    {
        public static void Main(string[] args)
        {
            string sourcePath = @"C:\Users\1152092\Desktop\a";
            string targetPath = @"C:\Users\1152092\Desktop\c";

            CopyDirectory(sourcePath, targetPath);
        }

        public static void CopyDirectory(string sourcePath, string targetPath)
        {
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                foreach (string sourceFilePath in files)
                {
                    string[] entityNames = System.IO.File.ReadAllLines(@"C:\Users\1152092\Desktop\Locations.txt"); 

                    foreach ( string entity in entityNames)
                    {
                        string fileExt = System.IO.Path.GetExtension(sourceFilePath);
                        string sourceFileName = System.IO.Path.GetFileName(sourceFilePath);
                        string targetFilePath = System.IO.Path.Combine(targetPath, entity + fileExt);
                        System.IO.File.Copy(sourceFilePath, targetFilePath);
                    }
                }
            }
        }
    }