Process.Start, WorkingDirectory, start one exe from another - c#

I hope you will get my problem, dont know if I can describe it properly in english, but I will try :)
Situation:
Folder Structure: MainFolder/Sub1/Sub2
2 exe files: MainFolder/MainProg.exe and /Sub2/SecondProg.exe
MainProg.exe is not from me, I wrote an .cs file inside /Sub1, kind of a plugin. (I think MainProg.exe kind of works like a compiler, cause it has a log window where I can see errors from my .cs file.. I think the author somewhere mentioned a JIT compiler ?!)
Start MainProg.exe -> Click a button -> Start SecondProg.exe
SecondProg.exe reads values from an xml file
Both are .NET 4.5
Problem:
I want to have the xml file inside /Sub2
If I do that and start SecondProg.exe the normal way, double clicking it, everything is working fine
If I try to start SecondProg.exe via the button in MainProg.exe, I get "SecondProg is not working anymore" from windows.
If I copy the xml file to MainFolder, its working..
So, I am still learning C#, but could there be a problem with the workingdirectory? I am so confused, because the MainProg.exe has nothing to do with my xml file, it doesnt even know its there, the only point, where I use it is when loading values into SecondProg.exe...
Inside my .cs file, I start the SecondProg via
public override void Button()
{
Process.Start("Sub1\\Sub2\\SecondProg.exe");
}
So its like, MainProg has the button, in my .cs file I tell him what to onClick.
well.. its hard to describe if you're not using your native language, but I hope you get what I mean ;)

That SecondProg.exe apparently looks in its working directory for that file and fails if it doesn't find it. As such, you have to set the working directory of the new process. To do that, create a ProcessStartInfo object, set the FileName to the path of the EXE file and the WorkingDirectory to the path of the folder containing that EXE. You then pass that object as an argument when you call Process.Start.

Related

Txt file not opening

**Hello, my Visual C# is running but when I enter a person's name in Form1.cs[Design] it won't open the file. My code is below and I'll also attach how I saved my txt and what errors are showing. I been trying to figure this out for hours. My goal is to learn how to save txt in my directory folder and how to call it in the .cs code.
`` ```
Errors
enter image description here
tldr;
Your filename is 'BoysNames.txt.txt'.
From the image it is clear that the exception occured, because there is no file called 'BoysNames.txt' in the current directory. I see what the problem is. You have hidden the extension of files and still the text file is displayed with its extension. (Note that since the 'exe' file doesn't show its extension but 'txt' is showing. It means it has double extensions). So, you need to do this:
boysFile = File.OpenText('D:\\visual c#\\pp22\\bin\\Debug\\BoysNames.txt.txt');
To avoid this type of problems in the future, try these steps.
Go to Control Panel.
Click on File Explorer Options -> View
Uncheck this Option 'Hide extensiobns for known file types'
Well it looks like it will not open the file because it is not where you are saying it is. Look in the error, it tells you where it is looking for the file, is it really there?

C# Open a process without know location path

I have to open a process.exe from a button_click event by known only the filename and the .exe extension.
Is It possible? In two words i need to make a game.exe library ,and if the filename.exe will be found the process filename.exe,
need to be started.
I want to avoid to ask the costumer the full path of the program location, and i dont want to use OpenFileDialog()for browse. Im three days stuck here. So
filename.exe=textbox1.text
SystemDiagnostic.Process(..).Start(filename.exe)
this will open only the file if the file and the debug program are in the same directory.
Do i need a recursive search, and if the file is found open the process?How do i search for the filename.exe in all the #"C:\?
Thank everyone Sorry for bad english.
If that file is deployed by you, like it is up to you to make an installer for it, you can add the containing directory of that file to the PATH environment variable, then you can simply launch it by
Process.Start(“filename.exe”)
Without having to specify the full path to that file.
If it is out of your control, but there is some trace about the location of that file, for example, if it is from a known vendor and the file has a default location or there is something like “InstallLocation” in registry for that file, you can query registry to get the full path then launch it.
Your last resort will be scanning the whole disk for that file. But this is too bad, unreliable and slow...and things can go wrong easily. I strongly warn you against this approach.
If you have to choose the last approach, you might find this post useful. Remember to check other attributes like version number, publisher, etc., in case you end up calling a wrong file happens to have that name.

C# add Embedded Resources to project while running

This is a weird question because i tought about it for some time and it doesnt really make sence to me because the program would need to re-compile, but, because if it is possible it would make my program much simpler, i need to know it there is a way to do it.
Basicly i have a .exe program.
The program has a combobox, that gets its value off a .txt file.
Its pretty simple to edit the values on the combobox, all the user has to do is to edit the .txt file and restart the program, but I need to make so that, if we move the .exe to another computer, it brings the .txt with him, so i added the .txt to my project.
The problem:
I need the user to, if needed, edit my .txt file.
If he edits the .txt file, it means i would have to update the file embedded in my .exe and from what i have searched, its not possible to do that.
My idea to solve this:
When the program starts, i copy the .txt out of the program, delete the embedded file on the project, and when the user closes my program, it copies the .txt that's on the outside, inside, like if i were to manually add it to the program again, that way if the user did edit it, if he moved the .exe to another computer, it would have its combobox values updated. It makes some sence to me, thats why im curious to see if it is possible, and if so, how do i add a external file to my project while it is running.
Thanks!
You could try the other way around.
You can add an application configuration file and in that you can maintain the values for your comboBox inside appSettings section with comma separated values. During the load it will take from config and during closure of the application you just need to update the appSettings section with the updated value.
Code to update the configuration file:
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["comboKey"].Value = "comboValues with comma separated";
config.Save(ConfigurationSaveMode.Minimal);
When you are copying your application to some other system then you should copy the app.exe.config along with the app.exe file.
If you are trying with any installer file in different system and you would to have a user settings, then you could also use the Settings.setting file with User level scope variables.

Not able to Convert .dwg to .pdf using .Net and True View (AcCtrl)

What I am trying to do is:
I have a Asp Mvc website. In it I need to create a function that does the following:
converts a .dwg (AutoCad) file to a .pdf file
converts a .dwg file to a .dwf file
I started with the, what I tought at the time, easier task to have the .dwg to a .pdf. After some research I found out that a way to do that (without buying a 3rd party license) is to install TrueView on the server and using it (or actually a AcCtrl.dll) to convert the file. Well, I did so. I installed the True View program and added a reference to AcCtrl Component (ACCTRLLib). Then I added a reference to the Dll inside the class file I am working on:
using ACCTRLLib;
So far so good. After that I followed the instructions on this post: PDF conversion using dwg true viewer in VB6
First, this is my code:
public static void ConvertFile()
{
IAcCtrl contrl = new AcCtrl();
contrl.PutSourcePath(#"D:\MMA\Autocat\File1.dwg");
string[] pdfPath = new string[1] { #"D:\MMA\Autocat\File1.dwg" };
contrl.SilentPublish(pdfPath);
}
Then, according to the post, I went to see if there is a registry with the specified path. The path that I have as a registry is the following:
HKEY_CURRENT_USER\Software\Autodesk\DWG TrueView\R13\DWGVIEWR-E001:409\Profiles\<<\Unnamed Profile>>\Dialogs\AcPublishDlg
So, I created a 'String Value' in it with ValueName: Location and ValueData: D:\MMA\Autocat\Testing
Alright. So, thats it. After all that I ran the application and called the function. The debugger goes through the code and everything executes (or at least looks like so) but nothing happens. I don't get a file in the D:\MMA\Autocat\Testing folder. I get no exception, no warning nothing. It just executes and nothing happens.
So what else did I tried. Some of those things might be a little naive or silly to try but nevertheless I did as I happen to be a little desperate.
I tried everything in a console application. I wasnt completely sure that this method is suitable for ASP MVC so I tried the same code with a console application unfortunatelly to the same result.
I added a file name inside the location string value. I changed the Location ValueData from 'D:\MMA\Autocat\Testing' to 'D:\MMA\Autocat\Testing\testFile.pdf' as I noticed that nowhere in the whole process a file name is asked for the converted file.
Following my thought from the previous conclusion I tried to supply the path to the 'result' file to the SilentPublish function.
string[] pdfPath = new string[1] { #"D:\MMA\Autocat\Testing\testFile.pdf" };
contrl.SilentPublish(pdfPath);
Again to no avail. So, my question, or rather questions are:
Is it possible to do it this way and if it is what am I doing wrong?
If its not than can you suggest a way?
Also if a .dwg to .dwf conversion is possible (with the same or different method I am all ears)
Thank you very much
True View does not expose the Autodesk.AutoCAD.PlottingServices namespace. You're going to need either a licensed copy of AutoCAD, RealDWG, or another third party API.
I'm not sure if you are still having this issue, but you should be able to do this with the Autodesk Forge API. Calls can be made from any language with a simple HTTP call. You will have to first convert to SVF and then to DXF and PDF from there.

How to open a file using a specified third party program in C#?

so what I'm trying to do is open a file (well, actually two folders, but I figure I'll start with a single file for now) using a third party comparison tool called UltraCompare. I'm working in a C# website project in Visual Studio 2010 (Express edition). I've seen how to open a file using a different program, here: Open a file with Notepad in C#.
Problem is, this only lets you open it using the default program for that file type. But I want to open it in a specified program. For example, a text file should open in UltraCompare, not notepad. Here's the code which does this:
string textBoxContents1 = TextBox1.Text;
Process.Start(textBoxContents1);
The textbox on the webform accepts a string, in which the user types the file's full path (not the most user-friendly design I know, but I'm not sure how to allow them to browse for a folder using a GUI interface in asp.NET). The file is then passed into the Process.Start() method, which opens it using the default program for that file type.
Is there any way to modify this to make it open using UltraCompare??
You can specify the program you want to open the file in:
Process.Start("yourprogram.exe", textBoxContents1);
Update
To open two files in Ultracompare, you'd probably do something like that:
Process.Start("yourprogram.exe", "file1.txt file2.txt");
Keep in mind that the second parameter of Process.Start method are the arguments passed to the program.
I said this is probably going to work because I assumed to be very likely that Ultracompare expects 2 arguments, but this might not be the case.
Quick question: Are you trying to do this for the client machine? Hope not
And I guess it looks into the PATH variable for finding your exe

Categories