GetRelativePath() public static method

Create a relative path from one path to another. Paths will be resolved before calculating the difference. Default path comparison for the active platform will be used (OrdinalIgnoreCase for Windows or Mac, Ordinal for Unix).
Thrown if or is null or an empty string.
public static GetRelativePath ( string relativeTo, string path ) : string
relativeTo string The source path the output should be relative to. This path is always considered to be a directory.
path string The destination path.
return string
示例#1
0
        private static string GetPath(bool makeAbsolute, string filePath, string absBasePath)
        {
            string absFilePath = Path.IsPathRooted(filePath)
                ? filePath
                : new FileInfo(Path.Combine(absBasePath, filePath)).FullName;

            return(makeAbsolute
                ? absFilePath
                : Path.GetRelativePath(absBasePath, absFilePath));
        }
示例#2
0
        private static void PushToFTP(string variant, string localFolder, string filterName)
        {
            var ftpHost = "ftp://ftp.cluster023.hosting.ovh.net/" + variant + "/datafiles/filters/" + filterName;

            using (var client = new WebClient())
            {
                client.Credentials = new NetworkCredential(Environment.GetEnvironmentVariable("FbFtpLoginName"), Environment.GetEnvironmentVariable("FbFtpLoginPw"));
                MakeDir(ftpHost, client.Credentials);

                foreach (var styleFolder in Directory.EnumerateDirectories(localFolder))
                {
                    var styleName = Path.GetFileName(styleFolder).Replace("(STYLE) ", "");
                    styleName = styleName.Substring(0, 1).ToUpper() + styleName.Substring(1).ToLower();
                    if (styleName == "Customsounds")
                    {
                        styleName = "CustomSounds";
                    }
                    MakeDir(ftpHost + "/" + styleName, client.Credentials);

                    foreach (var file in Directory.EnumerateFiles(styleFolder))
                    {
                        var relativePath = Path.GetRelativePath(localFolder, file);
                        relativePath = relativePath.Replace(Path.GetFileName(styleFolder), styleName);
                        UploadFile(client, ftpHost + "/" + relativePath, file);
                    }
                }

                ftpHost += "/Normal";
                MakeDir(ftpHost, client.Credentials);

                foreach (var file in Directory.EnumerateFiles(localFolder))
                {
                    var relativePath = Path.GetRelativePath(localFolder, file);
                    UploadFile(client, ftpHost + "/" + relativePath, file);
                }
            }

            void UploadFile(WebClient client, string path, string file)
            {
                var request = WebRequest.Create(path);

                request.Method      = WebRequestMethods.Ftp.DeleteFile;
                request.Credentials = client.Credentials;
                try
                {
                    using (var resp = (FtpWebResponse)request.GetResponse())
                    {
                        Console.WriteLine(resp.StatusCode);
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("File already deleted");
                }

                client.UploadFile(path, WebRequestMethods.Ftp.UploadFile, file);
            }

            void MakeDir(string dir, ICredentials credential)
            {
                var request = WebRequest.Create(dir);

                request.Method      = WebRequestMethods.Ftp.MakeDirectory;
                request.Credentials = credential;
                try
                {
                    using (var resp = (FtpWebResponse)request.GetResponse())
                    {
                        Console.WriteLine(resp.StatusCode);
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Folder already exists");
                }
            }
        }
示例#3
0
        public async Task <int> OnExecute(IConsole console)
        {
            var cts = new CancellationTokenSource();

            console.CancelKeyPress += (o, e) =>
            {
                console.WriteLine("Shutting down...");
                cts.Cancel();
            };

            var address = IPAddress.Parse(Address);
            var path    = Path != null
                ? IOPath.GetFullPath(Path)
                : Directory.GetCurrentDirectory();

            var host = new WebHostBuilder()
                       .ConfigureLogging(l =>
            {
                l.SetMinimumLevel(LogLevel.Information);
                l.AddConsole();
            })
                       .PreferHostingUrls(false)
                       .UseKestrel(o =>
            {
                o.Listen(address, Port);
            })
                       .UseSockets()
                       .UseWebRoot(path)
                       .UseContentRoot(path)
                       .UseEnvironment("Production")
                       .Configure(app =>
            {
                app.UseStatusCodePages("text/html",
                                       "<html><head><title>Error {0}</title></head><body><h1>HTTP {0}</h1></body></html>");
                app.UseFileServer(new FileServerOptions
                {
                    EnableDefaultFiles      = true,
                    EnableDirectoryBrowsing = true,
                    StaticFileOptions       =
                    {
                        ServeUnknownFileTypes = true,
                    },
                });
            })
                       .Build();

            console.ForegroundColor = ConsoleColor.DarkYellow;
            console.Write("Starting server, serving ");
            console.ResetColor();
            console.WriteLine(IOPath.GetRelativePath(Directory.GetCurrentDirectory(), path));

            await host.StartAsync(cts.Token);

            var addresses = host.ServerFeatures.Get <IServerAddressesFeature>();

            console.WriteLine("Listening on:");
            console.ForegroundColor = ConsoleColor.Green;
            foreach (var a in addresses.Addresses)
            {
                console.WriteLine("  " + a);
            }

            console.ResetColor();
            console.WriteLine("");
            console.WriteLine("Press CTRL+C to exit");

            if (OpenBrowser)
            {
                LaunchBrowser(addresses.Addresses.First());
            }

            await host.WaitForShutdownAsync(cts.Token);

            return(0);
        }
 private string GetRelativePathToProject(string toProject)
 {
     Project.Identifier toId = projectIdLookup[toProject];
     return(Path.GetRelativePath(Reader.SolutionConfigDir, toId.AbsoluteSourcePath));
 }
示例#5
0
        public async Task <int> OnExecute(IConsole console)
        {
            var cts = new CancellationTokenSource();

            console.CancelKeyPress += (o, e) =>
            {
                console.WriteLine("Shutting down...");
                cts.Cancel();
            };

            var address = IPAddress.Parse(Address);
            var path    = Path != null
                ? IOPath.GetFullPath(Path)
                : Directory.GetCurrentDirectory();

            if (!string.IsNullOrEmpty(PathBase) && PathBase[0] != '/')
            {
                PathBase = "/" + PathBase;
            }

            var host = new WebHostBuilder()
                       .ConfigureLogging(l =>
            {
                l.SetMinimumLevel(LogLevel.Information);
                l.AddConsole();
            })
                       .PreferHostingUrls(false)
                       .UseKestrel(o =>
            {
                if (IPAddress.Any.Equals(address))
                {
                    o.ListenLocalhost(Port);
                    o.ListenAnyIP(Port);
                }

                o.Listen(address, Port);
            })
                       .UseSockets()
                       .UseWebRoot(path)
                       .UseContentRoot(path)
                       .UseEnvironment("Production")
                       .Configure(app =>
            {
                app.UseStatusCodePages("text/html",
                                       "<html><head><title>Error {0}</title></head><body><h1>HTTP {0}</h1></body></html>");

                if (!string.IsNullOrEmpty(PathBase))
                {
                    app.Map(PathBase, Configure);
                }
                else
                {
                    Configure(app);
                }
            })
                       .Build();

            console.ForegroundColor = ConsoleColor.DarkYellow;
            console.Write("Starting server, serving ");
            console.ResetColor();
            console.WriteLine(IOPath.GetRelativePath(Directory.GetCurrentDirectory(), path));

            await host.StartAsync(cts.Token);

            var addresses = host.ServerFeatures.Get <IServerAddressesFeature>();

            console.WriteLine("Listening on:");
            console.ForegroundColor = ConsoleColor.Green;
            foreach (var a in addresses.Addresses)
            {
                console.WriteLine("  " + a + PathBase);
            }

            console.ResetColor();
            console.WriteLine("");
            console.WriteLine("Press CTRL+C to exit");

            if (OpenBrowser)
            {
                var url = addresses.Addresses.First();
                // normalize to loopback if binding to IPAny
                url = url.Replace("0.0.0.0", "localhost");
                url = url.Replace("[::]", "localhost");

                if (!string.IsNullOrEmpty(PathBase))
                {
                    url += PathBase;
                }

                LaunchBrowser(url);
            }

            await host.WaitForShutdownAsync(cts.Token);

            return(0);
        }