I have two projects in my solution and project A has a reference to project B. Project A calls a function in project B that should load a document that is within Project B:
return XDocument.Load(#"Mock\myDoc.html");
The problem is that XDocument.Load is using the path from Project A (where there is no Mock\myDoc.html).
I have tried using
string curDir = Directory.GetCurrentDirectory();
var path = String.Format("file:///{0}/Mock/myDoc.html", curDir);
but this as well gives me a path to ProjectA\Mock\myDoc.html when instead it should be ProjectB\Mock\myDoc.html. What am I missing?
EDIT: "Copy to Output" for the file "myDoc.html" is set to "Copy always" and the file is available in the Output folder of Project B.
There is only one current working Directory for all of your code at runtime. You'll have to navigate up to the solution directory, then back down to the other Project.
string solutiondir = Directory.GetParent(
Directory.GetCurrentDirectory()).Parent.FullName;
// may need to go one directory higher for solution directory
return XDocument.Load(solutiondir + "\\" + ProjectBName + "\\Mock\\myDoc.html");
string str = Path.GetDirectoryName(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName) + "\\->ProjectName<-";
I think this idea can help you.
Temporarily update the application current directory:
string saveDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory("the_projectB_defaultDirectory");
ProjectBfunction() ;
Directory.SetCurrentDirectory(saveDir) ;
If your directory architecture permits it, you may deduce the projectB directory from ProjectA current directory or from the .exe directory (i.e. Application.StartupPath).
Related
get current date and make directory and second when directory is created, in that directory I have to store excel file and also save file as current date.
String Todaysdate = DateTime.Now.ToString("dd-MM-yyyy");
if (!Directory.Exists("C:\\Users\\Krupal\\Desktop\\" + Todaysdate))
{
Directory.CreateDirectory("C:\\Users\\Krupal\\Desktop\\" + Todaysdate);
}
This code have made directory with current date.
But when I want to store file in that directory, it generates the error:
Could not find a part of the path
'D:\WORK\RNSB\RNSB\bin\Debug\22-01-2020\22-01-2020.XLS
Belove path is store excel file that i have to store.
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Todaysdate+"\\"+DateTime.Now.ToString("dd/MM/yyyy") +".XLS"))
Actually you are making the directory in a path then you are saving the .xls in another path.
You are making the directory using this path:
"C:\\Users\\Krupal\\Desktop\\" + Todaysdate
Then, here the path where you are trying to save the .xls:
Todaysdate+"\\"+DateTime.Now.ToString("dd/MM/yyyy") +".XLS"
The error shows the problem clearly, it could not fin this path:
D:\WORK\RNSB\RNSB\bin\Debug\22-01-2020\22-01-2020.XLS
While creating the .xls you are omitting the root path, so the process looks for the path 22-01-2020\22-01-2020.XLS in his working directory D:\WORK\RNSB\RNSB\bin\Debug.
You just need to align those paths: I sugget you to use relative paths, so here how you should fix your code:
String Todaysdate = DateTime.Now.ToString("dd-MM-yyyy");
if (!Directory.Exists(Todaysdate))
{
Directory.CreateDirectory(Todaysdate);
}
//then
using (System.IO.StreamWriter file = new System.IO.StreamWriter(Todaysdate+"\\"+DateTime.Now.ToString("dd/MM/yyyy") +".XLS"))
I presume you are running your WinForms application in Debug mode. This means that your current path is [your application path]\bin\Debug. If you look in file explorer, you will find that an executable has been created there. When using StreamWriter without an absolute file name, the file it tries to create is relative to the current execution path (in your case 'D:\WORK\RNSB\RNSB\bin\Debug'). StreamWriter will create a new file, if one does not exist, but it will not create a new folder, and you are passing it Todaysdate + "\\" which is effectively a new folder. Hence you are getting the error message.
To fix your problem, you need to provide the absolute path to your newly created directory thus:
using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Krupal\\Desktop\\" + Todaysdate+"\\"+DateTime.Now.ToString("dd/MM/yyyy") +".XLS"))
Winforms always expect directories inside Debug Folder, since it's EXE file is inside Debug and try to find it inside Debug folder.
In error it clearly shows that it is looking inside "Debug" folder.
Can you check whether File Exists in the mentioned folder created by you in C Drive.
// To Write File
System.IO.File.WriteAllLines(#"C:\Users\Public\TestFolder\WriteLines.txt", lines);
You can follow this MSDN Post, hope it helps, if Yes, please Upvote it
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file
I can't seem to find an answer anywhere on how to get the name of the first folder of my project.
So for Example I have a BaseSite folder and inside that BaseSite folder is where my solution file sits, in there I have many projects. I need to retrive the "BaseSite" name and only that.
I have found various examples with AppDomain.CurrentDomain.GetData("DataDirectory").ToString(); but that gives you the full path and my folder name is buried in there.
Same with: Server.MapPath("~/App_Data/somedata.xml");
Please check. It will return the base directory.
var BaseDirectory = new DirectoryInfo(Server.MapPath("~")).Parent.Name;
And
new DirectoryInfo(Server.MapPath("~/App_Data/somedata.xml")).Parent.Name;
I hope this will help you.
//this path will return current project folder
string path = AppDomain.CurrentDomain.BaseDirectory;
DirectoryInfo info = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
string infy = info.Parent.FullName; //this will give you the full path of project folder.
if you have to go one level up then add
info.Parent.Parent.FullName
And if you have to go one level down to subdirectory. Then use
DirectoryInfo[] directoryinfo = info.GetDirectories("SearchPattern or your subdirectory name");
string subdirectory = directoryinfo.FirstOrDefault().FullName;
I'm writing to a text folder which is in a folder within my project but I can't seem to get to it without writing the absolute complete path as it is on my computer which is fine on this computer but when I want to take it elsewhere I can't have that as the drives are different etc.
Here is a screenshot of the lines I'm using to get it to post to the directory on the right.
The file I'm trying to access is in a folder called AdminAccount and is called User.txt. it works fine as you can see from the commented directory link as a direct path but when I try with the directory string in use it does not work.
http://i.imgur.com/hAV55W0.png
Any help how to get around this? I tried all sorts, I tried doing
private string[] getLines = System.IO.File.ReadAllLines(#"\AdminAccount\User.txt");
private string[] getLines = System.IO.File.ReadAllLines(#"..\AdminAccount\User.txt");
No joy.
You can use,
string rootPath = Environment.CurrentDirectory;
string filePath = Path.Combine(rootPath,#"..\..\AdminAccount\User.txt");
private string[] getLines = System.IO.File.ReadAllLines(#filePath);
..\ is used to access a top level folder in the hierarchy. you can keep on adding ..\ to move up in the hierarchy.
Ex:
string path1 = #"C:\Users\Documents\Visual Studio 2010\Projects\Test\Test\bin\Debug"
string newPath = Path.Combine(path1, #"..\..\AdminAccount\User.txt");
new path would return
C:\Users\Documents\Visual Studio 2010\Projects\Test\Test\AdminAccount\User.txt
You just have to set the property "Copy to Output Directory" of the "User.txt" file to "Copy allways" or "Copy if newer".
Now you can read the lines as below
string[] getLines = File.ReadAllLines(
Path.Combine(Application.StartupPath, "AdminAccount", "User.txt"));
I am trying to create a project item programmatically. i have this code
string itemPath = currentSolution.GetProjectItemTemplate("ScreenTemplate.zip", "csproj");
currentSolution.Projects.Item(1).ProjectItems.AddFromTemplate(itemPath, name);
currentSolution.Projects.Item(1).Save();
But I would like to create the item in a specified directory inside the project (this creates the item in the root of the project). Is it possible? Thanks for help!
That's roughly how I add my cpp file, should be no different in your case.
The code will add the file under "SourceFiles\SomeFolder" in the project and also in the "Source Files" folder in the project view tree (it should be already there).
Project project = null; // you should get the project from the solution or as active project or somehow else
string fileName = "myFileName.cpp";
string fileRelativePath = "SourceFiles\\SomeFolder\\" + fileName;
// First see if the file is already there and delete it (to create an empty one)
string fileFullPath = Path.GetDirectoryName(project.FileName) + "\\" + fileRelativePath;
if (File.Exists(fileFullPath))
File.Delete(fileFullPath);
// m_applicationObject here is DTE2 or DTE2
string templatePath = (m_applicationObject.Solution as Solution2).ProjectItemsTemplatePath(project.Kind);
ProjectItem folderItem = project.ProjectItems.Item("Source Files");
ProjectItem myFileItem = folderItem.ProjectItems.AddFromTemplate(templatePath + "/newc++file.cpp", fileRelativePath);
Please don't expect the code to compile straight away and run - some checks for invalid state aren't performed here.
I am using the FileInfo class. However, the file info cannot find the file.
The file is called log4Net.config and I have added it to my project. I have set the properties to build action = 'Content' and copy output = 'copy always'
When I run the following code:
FileInfo logfileInfo = new FileInfo("Log4Net.config");
if (!logfileInfo.Exists)
{
Console.WriteLine("Cannot find file: " + logfileInfo.FullName);
return;
}
else
{
XmlConfigurator.Configure(logfileInfo);
}
Exists is always false. The exception is: Could not find file 'Log4Net.config'.
I have checked on my PDA and the Log4Net.config has been copied the PDA and is in the same directory as the executable. So not sure why it cannot find it.
Just some extra info the configure method expects a FileInfo as a parameter.
Am I doing something wrong.
Many thanks for any advice,
Steve
You have to point to the root of your project. FileInfo points to the root of the OS not the root of the exe. So you should change the code like this :
FileInfo logfileInfo = new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + #"\Log4Net.config");
A file not found exception is also thrown when the program does not have the security permission to access the file, so perhaps that's the case?
btw, there's a contradiction in your Post: First you say the file is called "logPDA.config", then it's suddenly called "Log4Net.config". Just to make sure, is the file always named the same?
You are assuming that the Program Folder is the current directory. Afaik that is not the case. You can investigate starting with System.IO.Directory.GetCurrentDirectory()
string logIOFilePath =
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase),
"Log4Net.config");