Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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#_Substring - Fatal编程技术网

C# 动态更改主机名

C# 动态更改主机名,c#,substring,C#,Substring,我在本地服务器上有文件,地址为\\localServerAddress\Folder\Program.exe。我需要动态删除服务器地址,并将其替换为表单中其他位置选择的其他服务器地址。服务器名称的长度可以不同,因此,我不能使用string.Substring函数 因此,考虑到输入 \\localServerAddress\Folder\Program.exe 我想知道结果 \\differentServerAddress\Folder\Program.exe 如果您总是与UNCs合作 然后

我在本地服务器上有文件,地址为
\\localServerAddress\Folder\Program.exe
。我需要动态删除服务器地址,并将其替换为表单中其他位置选择的其他服务器地址。服务器名称的长度可以不同,因此,我不能使用
string.Substring
函数

因此,考虑到输入

\\localServerAddress\Folder\Program.exe
我想知道结果

\\differentServerAddress\Folder\Program.exe

如果您总是与UNCs合作

然后

使用此方法:

    string changeServerInPathString(string originalString, string newServer)
    {
        List<string> stringParts = originalString.TrimStart('\\').Split('\\').ToList();
        stringParts.RemoveAt(0);
        stringParts.Insert(0, newServer);
        return string.Join("\\", stringParts.ToArray()).Insert(0, "\\\\");
    }
string changeServerInPathString(string originalString,string newServer)
{
List stringParts=originalString.TrimStart('\\').Split('\\').ToList();
stringParts.RemoveAt(0);
stringParts.Insert(0,newServer);
返回string.Join(“\\”,stringParts.ToArray()).Insert(0,“\\\”;
}

您可以使用以下内容:

void Main()
{
    string path = @"\\localServerAddress\Folder\Program.exe";

    UriBuilder bld = new UriBuilder(path);
    bld.Host = "NewServer";
    Console.WriteLine(bld.Uri.LocalPath);
}

结果:
\\newserver\Folder\Program.exe

那么,如果不总是
\test
,您如何知道要剥离的内容?你的要求很不清楚。这是我需要更改的服务器位置。我们有多个具有相同文件结构的服务器。我需要将文件从一台服务器移动到另一台服务器,从而删除服务器名称,并将其替换为新的服务器地址在您的示例中有两个“\\test”。应该选择哪一种?这实际上没有给我们提供要求…也许可以研究一下?UNC代表什么?Windows操作系统中的通用命名约定,UNC名称格式是:\\servername\sharename\path\FileName让我们假设它不是UNC,因为我真的不知道它是否是。我会尽快尝试,看看它是否有效。这非常接近我所需要的。我不得不稍微调整一下。这是结束结果
string removeName=newuri(SourcePathString).Host;string fullNewSourcePath=SourcePathString.Replace(string.Format(@“\{0}\”,removeName),string.Format(@“{0}”,NewSourcePathString));NewSourcePath String=fullNewSourcePath;返回NewSourcePathString无法从“System.Collections.Generic.List”转换为“string[]”Oops-修复了错误。现在就试试。这很好,只是它没有添加文件夹名和其他名称。示例:localServer\FolderName\Test.program将移动到NYCServer\SameFolderName\Test.program。唯一需要做的是去掉服务器名,但保留其余信息
    string text =   @"\\test\FolderName\foo.exe";
    text = text.Replace('\\', '-'); \\ this is done as I was not able to make the regex **\\\\\\(.)*?\\** , work.
    Regex rg = new Regex("--.*?-"); \\ if in case the above mentioned regex is made to work correctly please replace the regex with the same.
    text  = rg.Replace(text, "");
    Console.WriteLine(text.Replace('-', '\\'));

    Console.Read();
void Main()
{
    string path = @"\\localServerAddress\Folder\Program.exe";

    UriBuilder bld = new UriBuilder(path);
    bld.Host = "NewServer";
    Console.WriteLine(bld.Uri.LocalPath);
}