I have two asp.net projects in one solution. One is WebForms and the another is a WebApi project.
I am trying to save an image in the root folder of the WebForms using a method in the WebApi. Unfortunately, I am unable to get the BaseDirectory of the WebForms from the WebApi method.
This is what I have tried:
//this returns me only the root folder of the API i.e; D:\Projects\MyApp\API\)
string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
Is it possible to do what I am trying?
I guess you can navigate to you related app if it destination meet some folder tree position requirement using this:
string relativePath = "..\\Web\\";
string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
string webPath = Path.GetFullPath(baseDirectory + relativePath);
Related
I'm trying to get a specific application base ASP.NET Temp folder. I know we can use HttpRuntime.CodegenDir property to get the actual temp asp folder that the application is writing to. Is there any way I can get location to the base folder for that application in C#? Example:
<server>\c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\<AppName>
For any other who stumble on this question,
I couldnt find any direct way to get it. the way i resolved this is to get executing assembly location using below line
Assembly.GetExecutingAssembly().Location;
And then get to the parent folder which is the application name recursively using
var dir = new DirectoryInfo(path);
while (dir.Parent.Name != folderName)
{
dir = dir.Parent;
}
return dir.Parent.FullName;
which will give me the required path im looking for.
I'm trying not to hard code my path, but I have not been able to figure our a way to get to an xml file that I have included in my project under a folder labeled Datasource. Here is my latest code that I have tried which still doesn't work.
public static string myAssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
string fileName = xmlFileName;
string path = Path.Combine(myAssemblyDirectory, #"DataSource\" + fileName);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
Here is the output for the path that I'm getting which is putting it in my test results output folder.
"C:\MyAutomation\Automated_Test_Projects\AutomationProjects\MiserReleaseTestSuites\TestResults\marcw_ISD2005M 2016-02-05 10_15_17\Out\DataSource\Miser_Login_Dts.xml"
If possible I'd like to point it to
"C:\MyAutomation\Automated_Test_Projects\AutomationProjects\MiserReleaseTestSuites\MiserReleaseTestSuites\DataSource\Miser_Login_DTs.xml"
".." Can be used to go to the relative parent directory. "." Refers to the current directory.
You can combine these to form a relative path that starts higher up in the directory tree.
In your example you need to go 3 directories higher than the out folder and then into the MiserReleaseTestSuites\DataSource folder. Combining this produces
#"..\..\..\MiserReleaseTestSuites\DataSource\"
You can deploy the file in the same manner as you would when data driving the tests. See https://stackoverflow.com/a/25742114/546871
The TestContext class contains several fields with "directory" in their names. These can be used to access the various directories associated with running the tests. See also https://stackoverflow.com/a/19682311/546871
How to get root path of current working project in Xamarin.Android or Xamarin.Forms project? Forms project is PCL, hence I am trying out in Xamarin.Android project. Tried following things:
string rootPath = AppDomain.CurrentDomain.RelativeSearchPath;
string rootPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
string rootPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string rootPath = System.Environment.CurrentDirectory;
string rootPath = System.IO.Directory.GetCurrentDirectory();
Everything is returning null. I want to dynamically create file in particular folder, for that I need root path of my project. How to get it?
Is there another way to get the root of a wpf application as a string?
Now I'm still using
string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, (AppDomain.CurrentDomain.BaseDirectory.Length - 10));
This gives me the root as a string, but I assume this is not the right way to do it.
I also tried
string txt = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
but this sends me to root/bin/debug.
I only need the root as a string
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
Another way is:
Environment.CurrentDirectory
if it was not changed.
You can find the file path of the root folder of the startup project in a WPF App like this:
string applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string rootPath = Directory.GetParent(applicationDirectory).Parent.FullName;
Or to complete your example by getting the file path of the parent folder of the parent folder, you can do this:
string rootPath = Directory.GetParent(txt).Parent.FullName;
UPDATE >>>
In order to access your project Images folder, you can do this:
Path.Combine(Directory.GetParent(applicationDirectory).Parent.FullName, "Images");
You should put the images in a folder of your Visual Studio project called "Images" and set their Build Action to Resource (as shown here).
If you then get a relative image path from your DB, you would create a Pack URI and load a BitmapImage like this:
var imagePath = "Images/SomeImage.jpg"; // actually from DB
var uri = new Uri("pack://application:,,,/" + imagePath);
var bitmap = new BitmapImage(uri);
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.