Win32Exception when the file exists? - c#

In C# i got a random Win32Exception. I am a high experiened programmer with C# but this is random! The file "quickbms.exe" does exist!
When i did this:
commandPrompt.StartInfo.FileName = "start";
commandPrompt.StartInfo.Arguments = "\"" + Application.ExecutablePath + "\\src\\quickbms.exe\" -o \"src\\overworld.bms\" \"savegame_d.dat\" \"src\\xbla\\Savegame_Files\\regions\"";
commandPrompt.Start();
commandPrompt.WaitForExit();
(commandPrompt = System.Diagnostics.Process)
and this happened (Customized Exception Window used):
http://gyazo.com/9ebe6832f6200669d20c0c4e96e95a9c
A lot of you will say "The file don't exist" BUT IT DOES!
http://gyazo.com/a976d29109775512695c4bcc177ef5ad

The problem is your "start" argument.
commandPrompt.StartInfo.FileName = "start";
There is no windows-command named start. Win+R + "start" = error
You will get the exact same error if you remove the Arguments row.
You should call the file directly in Filename not inte the argument.
commandPrompt.StartInfo.FileName = "\"" + Application.ExecutablePath + "\\src\\quickbms.exe\";
commandPrompt.StartInfo.Arguments = "-o \"src\\overworld.bms\" \"savegame_d.dat\" \"src\\xbla\\Savegame_Files\\regions\"";
commandPrompt.Start();
commandPrompt.WaitForExit();

Related

Impossible to run C# WPF software (Error 0xc0000374)

I have a problem here and i need some help.
I work on a C# software in WPF, i have finished it, the program compile but do not run.
I've tried to search the problem using step by step run and it end on this line
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = Application.StartupPath + #"\Logs\" + DateTime.Now.DayOfYear + ".txt" };
and
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
and i got this error : " Attempt to read or write protected memory. This often indicates that another memory is corrupted "
that happen randomly during execution but mostly while running theses lines of code.
If you got a solution i'll take it !
The WPF Application class does not have a StartupPath property.
You could add System.Windows.Forms as project reference and use System.Windows.Forms.Application.StartupPath
Alternative:
string appPath = System.Reflection.Assembly.GetExecutingAssembly()
.GetModules()[0].FullyQualifiedName;
string appDir = Path.GetDirectoryName(appPath);
To debug your code with finer granularity, you could rewrite it:
var fileName = Application.StartupPath;
fileName += #"\Logs\";
fileName += DateTime.Now.DayOfYear
fileName += ".txt";
var logfile = new NLog.Targets.FileTarget("logfile");
logFile.FileName = fileName;

I am trying to add lines a textbox one after another in VS2019 - C#

I am trying to add 2 pieces of info to a textbox on consecutive lines based on whether files exist in a folder. After many attempts and not much success, I tried stepping through the program to find it does indeed print both lines to the textbox but the 2nd line seems to remove the first line for some reason. If I comment out the 2nd line then the first line is there but if I run with both lines enabled then only line 2 is visible on the top line of the textbox. The textbox has multiline and accepts return set in properties. I've tried using the Enviroment.NewLine and /r/n and combinations of both but it always does the same thing. Thanks for any help anyone can give with this.
if (File.Exists(SourcepathGLEXE))
{
LogBox.Text = "Game Loader EXE Found" + Environment.NewLine;
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(selectedPath + fileNameGLEXE);
versionSourceBox.Text = (myFileVersionInfo.FileVersion);
}
else LogBox.Text = "Game Loader EXE not found" + Environment.NewLine;
if (File.Exists(SourcepathCONEXE))
{
LogBox.Text = "Game Loader Config Found" + Environment.NewLine;
}
else LogBox.Text = "Game Loader Config not found" + Environment.NewLine;
The TextBox.Text is a property. Therefore, the following lines will work:
string conf = File.Exists(SourcepathCONEXE) ? "Game Loader Config Found" : "Game Loader Config not found";
LogBox.Text += conf + Environment.NewLine;

if(File.Exists()) statement isn't working

What I want the script to do is show a test saying "Currently Editing: " and then the file path of the folder the if statement is seeing if it exists or not. Even when the file exists, the text still won't pop up. No error messages.
I tried a File.Exists if statement, but that won't work, so I don't know what else to try.
if (File.Exists(#textBox2.Text + textBox1.Text))
{
currentlyediting.Text = "Currently Editing: " + textBox2.Text + textBox1.Text;
currentlyediting.Visible = true;
}
You should use recommended Path.Combine() method instead of concatenating two or more strings.
Try this instead:
var path = Path.Combine(textBox2.Text, textBox1.Text);
if (File.Exists(path))
{
currentlyediting.Text = $"Currently Editing: {path}";
currentlyediting.Visible = true;
}

GStreamer-sharp failed to create pipeline

I try to display RTSP stream using Gstreamer in my WPF application.
So I did so far:
installed GStreamer into loal folder F:/gstreamer
Created new WPF application
Added glib-sharp and gstreamer-sharp as dependencies.
The code below I use to init the library:
Gst.Application.Init(); // (1)
mainLoop = new GLib.MainLoop();
mainGLibThread = new System.Threading.Thread(mainLoop.Run);
mainGLibThread.Start();
Element uriDecodeBin = ElementFactory.Make("playbin", "uriDecodeBin0"); // (2)
Unable to load DLL 'libgstreamer-1.0-0.dll': The specified module could not be found.
on line (1). If I copy all the gstreamer dlls into bin/Debug folder the exception gone but ElementFactory.Make in line (2) always returns null without any exception. If I try to do something like
Parse.Launch(#"videotestsrc ! videoconvert ! autovideosink")
to test the library functionality I get error:
no element "videotestsrc"
but if I run it from command line:
gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink
that works as expected.
So my question - how to get GStreamer-sharp work?
Change path to GStream and add this code before start
string path = #"G:\gstreamer\1.0\x86\"; // path to your gstream
string pluginpath = #"G:\gstreamer\1.0\x86\lib\gstreamer-1.0\"; // path to your gstream
string registry = System.IO.Path.Combine(path, "registry.bin");
Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + path);
Environment.SetEnvironmentVariable("GST_PLUGIN_PATH", pluginpath + ";" + Environment.GetEnvironmentVariable("GST_PLUGIN_PATH"));
Environment.SetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH_1_0", pluginpath + ";" + Environment.GetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH_1_0"));
Environment.SetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH", pluginpath + ";" + Environment.GetEnvironmentVariable("GST_PLUGIN_SYSTEM_PATH"));
Environment.SetEnvironmentVariable("GST_DEBUG", "*:4");
Environment.SetEnvironmentVariable("GST_DEBUG_FILE", System.IO.Path.Combine(path, "gstreamer.log"));
Environment.SetEnvironmentVariable("GST_REGISTRY", registry);

Trying to get my SSIS to continue running if a file doesn't exist

So, I am running SSIS (through VS) and I have two segments that hang me up when my clients don't send in the exact files every day. I have a task that deletes old files, and then renames the current files to the filename with _OLD at the end of it.
The issue is: If the files that are in there aren't the exact same, it crashes, failing the entire thing.
An example:
A client sends in on Monday files: Names, Addresses, Grades, Schools
The same client, on Tuesday sends in: Names, Addresses, Schools
Since the Grades file doesn't exist, it still gets renamed to Grades_OLD but the SSIS fails.
The scripts are:
del Names_OLD.csv
bye
This will then go to the Rename Script:
ren Names.csv Names_OLD.csv
bye
and will then go on to Addresses, to do the same thing. It is super frustrating that these fail when a single file doesn't exist the next day, and there doesn't seem to be a need for it.
We have two scripts that generate the archive data to process:
public void Main()
{
Dts.Variables["ARCHIVEFILE"].Value = Path.GetFileNameWithoutExtension(Dts.Variables["FTPFILE"].Value.ToString()) + "_OLD" + Path.GetExtension(Dts.Variables["FTPFILE"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
and
public void Main()
{
/*PSFTP_DEL_script.txt
del %1
bye
PSFTP_REN_script.txt
ren %1 %2
bye
*/
var lineOut = String.Empty;
var File1 = Dts.Variables["User::FTPWORKINGDIR"].Value.ToString() + "\\SSIS_PSFTP_DEL_script.txt";
var File2 = Dts.Variables["User::FTPWORKINGDIR"].Value.ToString() + "\\SSIS_PSFTP_REN_script.txt";
lineOut = "del " + Dts.Variables["User::ARCHIVEFILE"].Value.ToString() + Environment.NewLine + "bye";
System.IO.File.WriteAllText(File1, lineOut);
lineOut = "ren " + Dts.Variables["User::FTPFILE"].Value.ToString() + " " + Dts.Variables["User::ARCHIVEFILE"].Value.ToString() + Environment.NewLine + "bye";
System.IO.File.WriteAllText(File2, lineOut);
Dts.TaskResult = (int)ScriptResults.Success;
}
Researching it doesn't really give anything helpful, and kind of just leads me back to where I am right now.
Try using a foreach loop on files for each file that can be processed and put all the processing of the file inside it. And do not put any precendence constraints between the foreach loops.
This will process the files that are there an not fail when the others aren't there.
The foreach loop essentially works as a check if the file exists.
This assumes you do not need all the files to properly process them.
Why not checking if the file exists before writing the script:
if (System.IO.File.Exists(Dts.Variables["User::ARCHIVEFILE"].Value.ToString())){
lineOut = "del " + Dts.Variables["User::ARCHIVEFILE"].Value.ToString() + Environment.NewLine + "bye";
System.IO.File.WriteAllText(File1, lineOut);
}
if (Dts.Variables["User::FTPFILE"].Value.ToString())){
lineOut = "ren " + Dts.Variables["User::FTPFILE"].Value.ToString() + " " + Dts.Variables["User::ARCHIVEFILE"].Value.ToString() + Environment.NewLine + "bye";
System.IO.File.WriteAllText(File2, lineOut);
}

Categories