Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#向路径添加变量_C#_Sql - Fatal编程技术网

C#向路径添加变量

C#向路径添加变量,c#,sql,C#,Sql,我想知道是否可以在包含不同路径的C#代码中添加参数作为路径?这些路径来自SQL表, 作为path=“C:\y”我想添加我的参数 代码如下: public void Main() { // TODO: Add your code here string path; path = "C:\\y"; //string path = @"\\192.168.2.55\\xstation_log

我想知道是否可以在包含不同路径的C#代码中添加参数作为路径?这些路径来自SQL表, 作为path=“C:\y”我想添加我的参数

代码如下:

public void Main()
        {
            // TODO: Add your code here
            string path;
            path = "C:\\y";
            //string path = @"\\192.168.2.55\\xstation_logs\\REAL\\logs\\fra-XST5-REAL-N1\\2020\01";
            string[] dirs = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories);
            string newpath = "C:\\x";
            try
            {
                Directory.CreateDirectory(newpath);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            for (int j = 0; j < dirs.Length; j++)
            {
                try
                {
                    Directory.CreateDirectory(dirs[j].Replace(path, newpath));
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
            for (int j = 0; j < files.Length; j++)
            {
                try
                {
                    File.Copy(files[j], files[j].Replace(path, newpath));
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            Dts.TaskResult = (int)ScriptResults.Success;
        }```


public void Main()
{
//TODO:在此处添加代码
字符串路径;
path=“C:\\y”;
//字符串路径=@“\\192.168.2.55\\xstation\u logs\\REAL\\logs\\fra-XST5-REAL-N1\\2020\01”;
字符串[]dirs=Directory.GetDirectories(路径“***”,SearchOption.AllDirectories);
字符串newpath=“C:\\x”;
尝试
{
CreateDirectory(newpath);
}
捕获(IOEX异常)
{
控制台写入线(例如消息);
}
对于(int j=0;j
嗯,您可以在插值字符串中使用include表达式

例如:

string source = "y";
string target = "x";

string path = $@"C:\{source}";
string newpath = $@"C:\{target}";

希望这有帮助。

是的。它也可以参数化并从数据库中提取。在DB中,路径为single\且未转义;