C#向路径添加变量
我想知道是否可以在包含不同路径的C#代码中添加参数作为路径?这些路径来自SQL表, 作为path=“C:\y”我想添加我的参数 代码如下: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
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\且未转义;