I have a file (.doc file), that is required for the software to work properly. The program starts with the user opening the file manually (OpenFileDialog) and continuing on normally. Is there a way to have the file already within the project so the user doesn't have to manually open the file each time the project runs? Thanks!
Here's a link to the tutorial I used to make this project. https://www.youtube.com/watch?v=wYse5nh5nB8
You can Add your document with right click context menu Add->Existing Item, in your project.
Select Properties of document by right clicking and Change Build Action from Compile to Embedded Resource and Copy to Output Directory" of the report file to "Copy always".
You can get the path of this file as
string fileName = "YourDocument.doc";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
once you have file path you can read its contents.
Related
I've just made my program an exe via publish in visual studio. In that i included a usermanual.txt and a aboutus.txt file which are in bin>debug folder. After i published the program and run it. Those files are not viewing saying cannot find the file. How can i fix this
Make sure your files are included in Solution Explorer. If not, add them (Right click on project -> Add -> Existing item... then select them from disk).
This way your manuals will be part of your project.
Then, you should setup that those files are copied to same folder as your exe (bin\debug or bin\release). To to that right click on them, select Properties and notice Copy to output directory setting. It has to have "Always" or "Copy if newer" option selected.
In your code, to open file, use path like this:
string userManualPath = Path.Combine(Application.StartupPath, "usermanual.txt");
that will open file in same directory as application's .exe.
When editing your manual (adding some new text), edit the one in solution, and the changes will reflect to either debug or release or published version.
I'm creating an app that can take a text from one file, read it, edit the text via a string, then write the edited string in a new text file.
My problem is that the file can't be found. In the following example I placed the file in the folder indicated in the screenshot and in another attempt in the 'bin' file, both without success and identical error message.
What I was trying to a achieve is the the following (as I am aware I could write the whole path from "C:\"), the text file should be somewhere in the application's directory so that the whole app can be moved without having to re-write the path.
So I need a way to write a "relative" path, if such thing is possible. this should pls work with creating a file similarly as well.
screenshot of the error message
visual studio screenshot with code and file location
thanks in advance.
Summary / Explanation: The file is not found because the directory you have set is not the directory of the file. If the file is in the same directory as to the C# file you are currently working on, then there's no need to add a folder's directory into it but rather just type the filename. You can also consider what's written below this paragraph.
FIRST:
Click InputText.txt
SECOND:
Right Click and Choose "Properties" or just press Alt + Enter
THIRD:
Set the "Build Action" property to "Embedded Resource"
FOURTH:
Set the "Copy to Output Directory" property to "Copy always"
LASTLY:
Change the Readline's first parameter to -> #"InputText.txt"
...
foreach (string line in File.ReadLines(#"InputText.txt", Encoding.UTF8))
{
originalTextInLines.Add(line);
}
...
This is the sample output (in my case of trying this).
This is the file that the example application above
The file is in the same directory the Program.cs is found that the file may be addressed by typing its filename only.
I am uploading files in my test, but I need them to be in project directory, so I can upload from there, not in my local computer. How can I do that? Now it looks just like this:
IWebElement element = Driver.FindElement(By.Id("fileupload"));
element.SendKeys("C:\\test\\testing.csv");
To have a file in your project directory, add the file in your test project using Solution Explorer. Next, right click on the file in the Solution Explorer.
Set the following property :
"Copy to output directory" = "Copy if newer"
Upon building, Visual Studio will copy the file in the /bin/Debug (or Release if you compile as Release).
In your Selenium test the file will be available in the current working directory (.) since it's in the /bin/Debug.
From the code you wrote I don't know the specific of your test but, if you can access the file through normal C# File operations it will be in '.'
Otherwise you will have to drive the controls with the webdriver. Rather than using SendKeys I would have (if the control supports it) open the file upload dialog through the web driver and have it select the file from there.
I have an application in C# where I have to select a file and process it. I use Visual Studio's Publish option to generate a Click Once application . But if I want to bundle the file along with the application and set it as default instead of Open File dialog, where should I place the file? The file is an Excel file
first create a folder to hold your custom files in the project,put your excel file inside that folder using add as link in the dialog box.Mark all files as Copy if newer (Copy to output directory property)and just make sure your build action is content.
Thats it..
for more reference
Source 1
Source 2
I added a pdf file ("myfile.pdf") with "Add Existing Item ..." to my wpf project.
Then I set the properties of it to "Resource" and "Do not Copy" (similar to the properties of other resources that I have, e.g flowdocuments).
Then I tried to execute
System.Diagnostics.Process.Start("myfile.pdf")
and obtained a 'file not found' error.
When I change the file properties to "build: content" and "copy if newer" everything works fine. However, I would prefer not to have this file in my output directory.
I work with VisualStudio Community 2013.
Is it maybe possible that VisualStudio cannot include a pdf file because it cannot compile it?
It's impossible. You can try to write the file from resources to %Temp% and open this File. But you can't control when an external program releases this file and deletes it.