How to Set up base path for ckfinder in .net - c#

I have installed CKEditor and CKFinder in the directory like root->NEWS->ckeditor &
root->NEWS->ckfinder but I want use ckeditor & ckfinder on this file
root->NEWS->International->AddNews.aspx
So problem here is how to load filebrowser using exact base path.
ex.
In code behind AddNews.aspx.cs
CKFinder.FileBrowser ckf = new FileBrowser();
ckf.BasePath = "ckfinder/"; /*default for root installation of ckeditor & ckfinder*/
ckf.SetupCKEditor(DetailsView1.FindControl("CKEditorControl1"));
What I am getting:
http://localhost:40407/NEWS/International/ckfinder/ckfinder.html?type=Images&CKEditor=ContentPlaceHolder1_DetailsView1_CKEditorControl1&CKEditorFuncNum=2&langCode=en
But It should be:
http://localhost:40407/NEWS/ckfinder/ckfinder.html?type=Images&CKEditor=ContentPlaceHolder1_DetailsView1_CKEditorControl1&CKEditorFuncNum=2&langCode=en
Please help me. What would be the exact BasePath for this?

If ckfinder/ is supposed to be a root directory, use either
ckf.BasePath = "/ckfinder/";
or
ckf.BasePath = Server.MapPath("~/ckfinder");
Your path, as you've shown it, is relative from the page in which you use it. ~ is interpreted by the ASP.NET runtime as the site's virtual root, which will be resolved by MapPath to the actual root directory's path.

Related

C# get absolute path from `~` on Unix-like systems

I'm building a C# app on Mac OS. It receives any path as an input and have to save a file by this path. The problem I faced is with paths relative to user directory, specified as ~.
I'm using following code using var s = File.Create(path); and there are few possibilities path parameter could be:
provided relative path e.g. filename.txt or ../filename.txt - it works and creates this file relative to current working directory.
provided absolute path e.g. /Users/username/Desktop/filename.txt - also works as expected
but providing path relative to user directory e.g. ~/Desktop/filename.txt - does not work. In this case File.Create is trying to combine absolute path like in case 1. It takes current working dir and simply adds my path like this /Users/username/project/~/Desktop/filename.txt. Which does not exist.
I tried to get absolute path from ~/Desktop with Path.GetFullPath("~/Desktop/filename.txt"). It results to the same /Users/username/project/~/Desktop/filename.txt. Same with Path.GetRelativePath("/", "~/Desktop/filename.txt");
Path.GetRelativePath("./", "~/Desktop/filename.txt"); returns not changed result.
Then tried Path.GetPathRoot("~/Desktop/filename.txt"). It gives just an empty string.
So the question, how in C# on Unix like host convert relative path like this ~/Desktop to absolute path like this /Users/username/Desktop?
You have to determine if the path starts with ~/ and, if it does, replace it with the path of the home directory. Here is a method that can do that:
public static string GetPath(string path) =>
(path.Length >= 2 && path.StartsWith("~/"))
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), path.Substring(2))
: path;
Use it like this: GetPath("~/Desktop/filename.txt"). This will return /Users/username/Desktop/filename.txt (provided that your home directory is /Users/username).

How to create PDF file links with relative file path and PDFsharp?

My App creates a Content.pdf file that links to Server.pdf and Client.pdf. This is the folder structure:
..\MyApp\Ressources\Content.pdf
..\MyApp\Ressources\Server\Server.pdf
..\MyApp\Ressources\Client\Client.pdf
Furthermore, the folders and files get burned on a CD/DVD or get backed up on a network drive - that causes me to use relative file paths. I use following code to create the Content.pdf:
relative WebLink:
var relativeFileLinkPath = "./" + Directory.GetParent(doc.Uri.LocalPath).Name + "/"+ doc.OutputFileName;
page.AddWebLink(pdfrect, relativeFileLinkPath);
relative FileLink:
var relativeFileLinkPath = "./" + Directory.GetParent(doc.Uri.LocalPath).Name + "/"+ doc.OutputFileName;
page.AddFileLink(pdfrect, relativeFileLinkPath);
Both work fine on local drive, but my issues are:
WebLinks do not work/open on network drive
FileLinks close the Content.pdf and replace it with the linked pdf file
Questions:
How can I modify the FileLinks to get opened in a new (PDF reader) instance/window?
Holding the CTRL-Key when clicking on the FileLink is an option but not a nice solution.
What’s the right syntax for relative PDF web links?
AddFileLink is the correct method for files sitting side by side in a folder.
To control whether a new window will be opened, a small modification of PDFsharp's PdfLinkAnnotation class will be needed. PDF supports a NewWindow attribute which PDFsharp cannot currently set.
Get the source for PDFsharp, locate "<</Type/Action/S/Launch/F<</Type/Filespec/F{0}>> >>" in PdfLinkAnnotation.cs and replace it with "<</Type/Action/S/Launch/NewWindow true/F<</Type/Filespec/F{0}>> >>".
Disclaimer: I did not test the proposed change - I hope it is syntactically correct and I hope it does what you want.
Disclaimer 2: This change is needed for PDFsharp 1.50 beta 3. Later versions may have support for that flag.

File.Delete isn't working to delete image from sub folder

In our ASP.NET program a user can upload an image to a folder. The location of the image (including the name of the upload folder which is in the root directory) is stored as a variable called "path", ie. "Uploads/fileName.jpg".
To remove the image:
if (File.Exists("~/" + path))
{
File.Delete("~/" + path);
}
However, it fails to run because it can't verify that the file exists. Through some testing we noticed it's looking for "path" in the "system32" directory. Why would this be?
You need to use Server.Map path to ensure that the Tilde is resolved correctly.
MSDN Article is here -> http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
Your code would become
var fixedPath = Server.MapPath("~/" + path);
if (File.Exists(fixedPath))
{
File.Delete(fixedPath);
}
The File class is not aware of the IIS directory mapping, so it won't understand ~ correctly. You have to first use a method to map the app relative path to a local path with Server.MapPath

How to load css into html dynamically?

My C# program uses a web browser control and I programmatically set its html property by laoding it from a html string variable. This almost works well ,but I noticed it lost the reference to the css file. I think a simplest solution is to make the path of the css file absolute,but I want it remains relative to the C# executable.So let me ask how to let the html reference to the css file in such a context.
Thank you in advance.
Edit: I am sorry . My c# application is a desktop one ,although it uses a web browser control.
Edit: Let me put some code. I first load the document from an html file then store it in a variable then for the 2nd time or later I load the document from the variable.
//first load
web_browser_control.Url = new Uri( dir + #"\HTML\default1.html" );
void wB2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ html_string = web_browser_control.DocumentText; }
//second load or later
web_browser_control.DocumentText = html_string;
You need to have a look at what path it THINKS its resolving, it would be best if you could please include some code, chances are its not resolving to where you think it is as its executing out of the bin directory (is your path correct relative to the path of the executable and the html),
For example is the html its looking at still where you think it is or is it now in a different directory so the relative paths are now obsolete ?
More code would be good and an indication of the control you are using.
Seems you can change property of css file to be "content" and make property "copy to output directory" - "always", so the wep app dll will be in the same directory with css file.
Or you can use MapPath method.

how can i resolve a relative path in both a web/windows app in c#

I have an assmebly that will be used in both a desktop app and an asp.net website.
I need to deal with relative paths (local files, not urls) in either situation.
How can i implement this method?
string ResolvePath(string path);
Under a web envronment, id expect the method to behave like this (where d:\wwwroot\mywebsite is the folder iis points at):
/folder/file.ext => d:\wwwroot\mywebsite\folder\file.ext
~/folder/file.ext => d:\wwwroot\mywebsite\folder\file.ext
d:\wwwroot\mywebsite\folder\file.ext => d:\wwwroot\mywebsite\folder\file.ext
for a desktop environment: (where c:\program files\myprogram\bin\ is the path of the .exe)
/folder/file.ext => c:\program files\myprogram\bin\folder\file.ext
c:\program files\myprogram\bin\folder\file.ext => c:\program files\myprogram\bin\folder\file.ext
I'd rather not inject a different IPathResolver depending on what state its running in.
How do I detect which environment I'm in, and then what do i need to do in each case to resolve the possibly-relative path?
Thanks
I don't think the original question was answered.
Let assume you want "..\..\data\something.dat" relative to, lets say the executable in "D:\myApp\source\bin\". Using
System.IO.Path.Combine(Environment.CurrentDirectory, relativePath);
will simply return "D:\myApp\source\bin..\..\data\something.dat" which is also easily obtained by simply concatenating strings. Combine doesn't resolve paths, it handles trailing backslashes and other trivialities. He probably wants to run:
System.IO.Path.GetFullPath("D:\myApp\source\bin..\..\data\something.dat");
To get the a resolved path: "D:\myApp\data\something.dat".
The website binaries are copied to a temp folder when the application runs - so you usually can't get the relative path from the excecuting assembly.
This may not be a sensible way of doing this - but what I did to solve this problem was something like this:
if (filepath.StartsWith("~"))
{
filepath = HttpContext.Current.Server.MapPath(filepath);
}
else
{
filepath = System.IO.Path.Combine(Environment.CurrentDirectory, filepath);
}
This is because on the web version - a relative path has a ~ at the front - so I can tell if it's come from the web.config or the App.config.
As mentioned in John's comment, relative to what? You can use System.IO.Path.Combine method to combine a base path with a relative path like:
System.IO.Path.Combine(Environment.CurrentDirectory, relativePath);
You can replace Environment.CurrentDirectory in the above line with whatever base path you want.
You could store the base path in the configuration file.

Categories

Resources