I am using VSTO to build an Excel AddIn. In this project, I also added some python .py scripts to do some data manipualtion.
The whole project is a C# project, all the .py files are taken as content files of the project, in detail, what I did is setting Properities-Build Action as 'Content', setting Properities-Copy to Output Directory as 'Copy always'.
However, after publish with clickonce, when executing, I couldn't find where the content files are. I already tried like: Application.StartupPath, but did't work. I really need to find the content files path and step into them.
Thanks in advance to anyone who take time to see my question.
Publish page of an office add-in project doesn't have an Application Files button which means you can not include some files in the click once installer in the way that you do it for applications. As an option, you can put your files as embedded resources and then at startup of the add-in, extract them from resources and copy them to add-in output directory.
To do so, you can add your file to Resources.resx and then at StartUp of your add-in, extract the file from resources and save it to the deployment directory and use it.
var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var assemblyFolder = System.IO.Path.GetDirectoryName(assemblyLocation);
var file = System.IO.Path.Combine(assemblyFolder , "test.py");
if (!System.IO.File.Exists(file))
System.IO.File.WriteAllBytes(file, Properties.Resources.test);
Now the file is in the path specified in file variable.
Related
I have a very simple .NET console application in Visual Studio. I am trying to write some words into a text file.
using (StreamWriter file = File.AppendText("log1.txt"))
{
file.WriteLine("Hello from the text file");
}
If the file does not exist, the application creates it in the autogenerated folder bin/Debug.
Is there a way to create this file in the project's directory, where I have .csproj file?
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes, but this can only be done while you are working on your project. Once you are done developing it and try to publish it you won't have access to the location where you have .csproj file, because after publishing you can install it on any PC and it wont have the project you are working on.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug?
No, I assume by real-world applications in your context you mean a published project '.exe' that you can run on any PC. Windows provides you three Data folders that you should use when writing your program so that it works smoothly after publishing:
User Data
Roaming User Data
All User Data
You can acess the above folders in .NET application using the Environment.SpecialFolder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
As per your given code, try this :
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"log1.txt");
using (StreamWriter file = File.AppendText(fileName))
{
file.WriteLine("Hello from the text file");
}
This way you will be able to publish your program and it will still work smoothly without hard-coding the path as you were doing previously.
That's why .NET creates them there firstly?
If you don't specify a complete path, and just the file name .NET looks into the working directory of the executable, which in this case is bin/Debug
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes. As explained here (second answer) you can use the post-build event to write down the value of $(ProjectDir) in a text file (using command echo $(ProjectDir) > ..\..\projectdir.txt). This macro contains the directory of your .csproj. This command will create the file projectdir.txt with your project directory after a build process so you read this file contents in your code and use what is inside it to pass to File.AppendText as the base directory to create your file log1.txt.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
That depends on what you want to do. In your case the code creates the file at bin/Debug because that is where your executable are being executed. When you omit the full path to File.AppendText and just pass "log1.txt" as argument, it will create the file in the same folder as the executable are at. If you want a different folder you should specify the folder here (e.g. File.AppendText("C:/log1.txt") will create the file at C:/.
You can create the text file in the root of your project and use copy always to have them in the same place as your executable. If this is just a readonly text file then it's OK because windows doesn't allow you to modify the files reside in Programs folder in OS drive.
If you want your code to modify these text file then you need to put them in appdata folder. In real world example I did this on many project. All the database work my winforms, WPF application need goes in AppData folder.
I have an excel plugin where I want to read an xml file on form load from a config folder, but I can't figure out how to read from that folder after I deploy the application.
On the excel ribbon, when the user clicks a button to open my plugin, on form load, I want to read from a specified directory. I am deploying the application with ClickOnce and using VS 2017. I set the xml file build to Content and set Copy to output directory as Always. When I deploy, it puts the xml file in some ClickOnce dll folder when the user installs it.
Try InstallDir = Environment.CurrentDirectory; at startup of you plugin.
In ThisAddIn_Startup or in your ribbon constructor.
I had a folder on my desktop with files in it. I copied that into the folder of my solution and in the solution explorer I referenced that folder into the solution. However, Im not able to open files in that folder with a relative path.
The relative path from the cs-file would be "../FolderIAdded/blabla" as seen in the solution explorer. But in the windows explorer, the path is differen of course:
Solutionfolder
- SolutionFolder.sln
- Solutionfolder.v11.suo
- SolutionFolder
-- bin
-- obj
-- Properties
-- TheFolderIAdded
-- App.config
-- Form1.cs
-- etc.
Here, it would be "FolderIAdded/blabla"
Where do I have to put that folder?
My goal: I want to be able to open files from that folder in my c#-code with a relative path.
You're assuming that your program runs in the directory where your source code is located. That's not the case. Depending on your configuration, your program will execute from a directory inside Solutionfolder\bin.
One possible solution is to copy the file(s) to the output directory when you build your project.
Another alternative is to embed the files into your application's assembly at compile time, although this precludes editing of them after deployment. To do that, set Build Action to 'Embedded Resource', then you can access them using the GetManifestResourceStream method of the Assembly class. The filename you need to give it will be derived from the path within the project structure, so in your example it would be "TheFolderIAdded.Filename.ext".
Yes, that's a dot, not a backslash.
Assuming the files are embedded in the same assembly the code that wants to read them is in, the code will look something like
var assembly = Assembly.GetExecutingAssembly();
using (var stream =
assembly.GetManifestResourceStream("TheFolderIAdded.Filename.ext"))
using (var reader = new StreamReader(stream)) {
string fileContents = reader.ReadToEnd();
}
I don't think it's a good idea to write relative path from .cs file. Better build the path base on where the application is executed:
One example, there are plenty other on the web: How can I get the application's path in a .NET console application?
(Your application is not running in the solution's root folder but where the .exe file is locatated. For example when you debug a desktop application, it runs typically from [solution folder]/bin/debug/ )
Then make sure the file you want to open property Copy to Output Directory is set to Copy Always or Copy if newer. (Right click on the file in your Solution Explorer and click on "Properties" to be sure to access it.)
I am making a console program in VS2010. The structure of as layed out in the solution explorer is Project file, Properties folder, References, and Program.cs.
I want to make use of a text file for reading/writing. What is the correct way of adding this file to the project? If I just do StreamWriter sw = new StreamWriter("maze.txt") then it'll create the file in the output folders (bin/debug or bin/release). But it won't show up in the solution explorer.
Now if I right-click on the solution explorer and Add New Item, I can create a text file in the root project folder (same level as Program.cs). This will show up in the solution explorer.
Is there a way for me to access this newly created file? Other than doing something like StreamWriter sw = new StreamWriter("../../maze.txt") by specifying the path to be two parents up?
How am I supposed to manage external files in a Visual Studio console applicatoin? In a WinForms application, there's a resources folder where I can add these things and a Resources.resx file to manage it. I can access it with Properties.Resources.someres.
If you want to create a file at design time and have it included in your bin folder then add the text file, go to properties, and select "copy always" or "copy if newer" for the copy property.
Note that this will copy the file from the project to the bin folder, but changes in the bin folder when debugging won't be copied up into the project itself. I doubt you want the program to interact with the version of the file in the project itself. If you do something like that then anytime you run the program outside of VS (i.e. when you copy it to the machine of an actual user) it won't work.
As for your resources question, you can still use resources. Just right-click and "Add" -> "New Item" (aka keyboard shortcut Ctrl+Shift+A) and choose "Resources File". You could also set the text file to be an Embedded Resource. MSDN has a lot of information on using resources on their Managing Application Resources page.
You can still embed resources to a console application. I've used this technique in quick apps so we don't need a installer or deploy dependencies such as these type of files. This question discusses a similar topic.
Everything in Visual Studio seems to lead one to putting data files with the application.The app.config goes there, when I create an .XML data file, there is a Copy to Output property that will automatically copy that file to the exe folder. Howerver, it seems that under Vista and Win7 UAC doesn't want the application to be able to write data to any file in the application directory. So I'm changing my evil ways so that I use the LocalApplicationData folder for files I want to read and write. (I just read the app.config so I'm leaving it alone)
I'm using a VS2010 Visual Studio Installer project to create the installer for this app and I can't seem to find a way to target the folder for my .xml file to the LocalApplicationData folder. I can click on the file and see a Folder property but the dialog only has options for Application Folder, User's Desktop and User's Program Menu. Is there some way to do this in the installer or do I have to write code that checks for the file and copies it over from the .exe folder when it doesn't exist? I figure I'm late to this particular party and there must be a canonical way of handling this.
Also, I'm wondering about debugging, is there something similar to the copy if newer functionality in the build process that will now copy this .xml file automatically over to the LocalApplicationData folder whenever I update it?
The Setup project doesn't expose LocalApplicationData in the Special Folders list. You can use it anyway by doing this:
Add a Custom Folder and set the DefaultLocation property to [LocalAppDataFolder]