I was wondering how I would go about doing this.
Normally I don't worry about this because I just stick the CSV file in the Debug Folder and reference it using:
filePath = Application.StartupPath + "\\blah.csv";
But I want do to this a bit differently now because I want to create a good baseline template project that contains this CSV file whenever I make a new project from the template.
But I still want the file path to be relative, IE) I don't want to have to edit the path to that CSV for each new project that I make.
Does anyone know how to set this up?
If I understand what you are needing you could go about it like this.
string appPath;
appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
appPath = appPath.Replace("file:\\", "");
appPath = appPath + #"\" + "filename.csv";
Related
Currently in my solution I have 3 projects. I want to keep only one Excel file in one of them, and read from it in the other two projects. However I am unable to get the dynamic path at run time to keep it in one place..
For now I am using reflection to get the path of it from the calling project using GetCallingAssembly.
How can I can keep it under one project and not hard code the path and use it in the other two projects?
string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
string actualPath = path.Substring(0, path.LastIndexOf("bin"));
string projectPath = new Uri(actualPath).LocalPath;
string fileToRead = projectPath + #"TestData.xlsx";
I'm attempting to do two things. I want to embed a text file into my project so that I can utilise it and modify it, but at the same time I don't want to have to package it when I send the project out to users (I.E included in the exe file).
I've had a look around and there's been multiple questions already but I just cant seem to get any to work. Here's the steps I've taken so far;
Added the text file to my "Resources Folder"
Build action to "Content" and output directory to "Do not copy"
I then try to access the file in my code;
if (File.Exists(Properties.Resources.company_map_template))
{
MessageBox.Show("Test");
var objReader = new StreamReader(Properties.Resources.company_map_template);
string line = "";
line = objReader.ReadToEnd();
objReader.Close();
line = line.Replace("[latlong]", latitude + ", " + longitude);
mapWebBrowser.NavigateToString(line);
}
The MessageBox never appears which to me means that it cannot find the file and somewhere somehow I've done something wrong. How can I add the file into my project so I don't need to distribute with an exe whilst being able to access it in code?
I would use the following:
BuildAction to None (not needed)
and add your file to Resources.resx under files (using DragAndDrop from SolutionExplorer to opened Resources.resx)
Access to your Text:
using YOURNAMESPACE.Configuration.Properties;
string fileContent = Resources.company_map_template;
Then you're done. You don't need to access through StreamReader
How do I programmically get the File Path of a File In my project?
string fileContent = File.ReadAllText(LocalConstants.EMAIL_PATH);
The EMAIL_PATH I added to my project as a text file. This line of code throws an exception because by default it is looking in the system32 folder for some reason. I need to set it to the projects directory.
You could use Path.Combine and AppDomain.CurrentDomain.BaseDirectory:
string fileName = "SampleFile.txt";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LocalConstants.EMAIL_PATH, fileName);
Returns in a test project in debug mode this path(when LocalConstants.EMAIL_PATH="Emails"):
C:\****\****\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsFormsApplication1\bin\Debug\Emails\SampleFile.txt
You can use Environment.CurrentDirectory
See: http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx
For further reading.
edit*
string temp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Will do the same and is perhaps a little safer.
This worked for me System.AppDomain.CurrentDomain.BaseDirectory.ToString()
There is a text file that I have created in my project root folder. Now, I am trying to use Process.Start() method to externally launch that text file.
The problem I have got here is that the file path is incorrect and Process.Start() can't find this text file. My code is as follows:
Process.Start("Textfile.txt");
So how should I correctly reference to that text file? Can I use the relative path instead of the absolute path? Thanks.
Edit:
If I change above code to this, would it work?
string path = Assembly.GetExecutingAssembly().Location;
Process.Start(path + "/ReadMe.txt");
Windows needs to know where to find the file, so you need somehow specify that:
Either using absolute path:
Process.Start("C:\\1.txt");
Or set current directory:
Environment.CurrentDirectory = "C:\\";
Process.Start("1.txt");
Normally CurrentDirectory is set to the location of the executable.
[Edit]
If the file is in the same directory where executable is you can use the code like this:
var directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var file = Path.Combine(directory, "1.txt");
Process.Start(file);
The way you are doing this is fine. This will find the text file that is in the same directory as your exe and it will open it with the default application (probably notepad.exe). Here are more examples of how to do this:
http://www.dotnetperls.com/process-start
However, if you want to put a path in, you have to use the full path. You can build the full path while only caring about the relative path using the method listed in this post:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e763ae8c-1284-43fe-9e55-4b36f8780f1c
It would look something like this:
string pathPrefix;
if(System.Diagnostics.Debugger.IsAttached())
{
pathPrefix = System.IO.Path.GetFullPath(Application.StartupPath + "\..\..\resources\");
}
else
{
pathPrefix = Application.StartupPath + "\resources\";
}
Process.Start(pathPrefix + "Textfile.txt");
This is for opening a file in a folder you add to your project called resources. If you want it in your project root, just drop off the resources folder in the above two strings and you will be good to go.
You'll need to know the current directory if you want to use a relative path.
System.Envrionment.CurrentDirectory
You could append that to your path with Path
System.IO.Path.Combine(System.Envrionment.CurrentDirectory, "Textfile.txt")
Try using Application.StartupPath path as default path may point to current directory.
This scenario has been explained on following links..
Environment.CurrentDirectory in C#.NET
http://start-coding.blogspot.com/2008/12/applicationstartuppath.html
On a windows box:
Start notepad with the file's location immediately following it. WIN
process.start("notepad C:\Full\Directory\To\File\FileName.txt");
I use the line below in my C# winform app, this works great but occasionally if the program is being run from the command line I get an error that the config.xml file cannot be found. This is because the 'working directory' is different (I think), I need to say "load config.xml from current directory", how would I do this?
docXML.Load("config.xml");
Thanks
Jonathan
string fileName = Path.Combine(Application.StartupPath, "config.xml");
string Path = "";
string Filename = ConfigurationManager.AppSettings("Filename");
for loading from current directory
Path = System.Web.HttpContext.Current.Server.MapPath(Filename);
for loading from Base directory
Path = AppDomain.CurrentDomain.BaseDirectory + Filename;