C# WPF Debug start exe from external associated file - c#

Trying to debug an application that is opened by opening a text-file associated to open the application being debugged.
Is there a way to start the debug and wait for the application to be called without the "Start external program" start action?
Ultimately I'm trying to get the file information of the text-file that opens the application, so that it can be used in the application as a "saved project" file.
I have a text-file named "myFile.cats", I've associated this file extension to open with my executable solution made by the visual studio application in the debug bin.
I've tried using the StartupEventArgs, but it doesn't come back with anything obviously since it's not being called from an external file. So I don't seem to have a way of testing this to make sure it works...
Any help would be greatly appreciated.
using Caliburn.Micro;
using ApplicationWPFUI.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ApplicationLibrary;
namespace ApplicationWPFUI
{
public class Bootstrapper : BootstrapperBase
{
public Bootstrapper()
{
Initialize();
}
//myFile.cats File opens this exe and the 'OnStartup' runs, where is the myFile.cats information being passed in?
protected override void OnStartup(object sender, StartupEventArgs e)
{
if (e.Args.Count() != 0)
{
//Save the startupEventArgs to a variable
GlobalConfigs.FileList.Files = e.Args.ToList();
}
DisplayRootViewFor<MainViewModel>();
}
}
}

I was doing it correctly already, with one slight change that needed to be made.
The information needed for the file that opens the application is in the GlobalConfigs.FileList.Files = e.Args.ToList();. But, I only need the first variable in that list.
So calling the following does the trick:
GlobalConfigs.FileList.Files = e.Args[0].ToString();
Obviously, the StartupEventArgs will not always have information in there, since I'll be calling the application by itself as well as opening it with associated files. So I just wrapped that in an if statement:
if (e.Args.Count() != 0) {
GlobalConfigs.FileList.File = e.Args[0].ToString();
}
And Bobs your uncle.

Related

System.IO.FileNotFoundException In multi-project solution

So I've been stuck on this error for a good week now, and it has been very frustrating
(Impossible to load the file or assembly 'Audio.Default.Switcher.Wrapper.dll' or one of its dependencies. The specified module wasn't found)
I'm working with Visual Studio 2017 and I've downloaded the SoundSwitch project, and everything is working perfectly fine. I'm able to run "SoundSwitch" without any errors.
The way the solution works (as I'm able to understand) is that the "SoundSwitch" C# project is the "master" and it has references to "Audio.Default.Switcher.Wrapper" as well as "SoundSwitch.UI.UserControls"
"Audio.Default.Switcher.Wrapper" as a reference to "AudioDefaultSwitcher"
Both "Audio.Default.Switcher.Wrapper" and "AudioDefaultSwitcher" seem to compile as .dll files
"SoundSwitch.UI.UserControls" cannot be started and is used for display stuff I guess
"SoundSwitch" is the only startable project (not counting mine of course)
The K005_test is MY project, I've added it and it has references (only) to "SoundSwitch" and I'm trying to use the functions from the SoundSwitch project inside K005_test. And so far my K005_test project consists only of
Program.cs (where I get the error by the way)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace K005_test
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //Exception thrown here
}
}
}
Form1.cs (where I put my code for the test)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace K005_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("This is a test");
var test = SoundSwitch.Model.AppModel.Instance.AvailablePlaybackDevices; //Error this line exists
}
}
}
And when I run my K005_test project, it compiles and runs just fine, except when I click the button Then I get the error I posted above. Also the message box doesn't open.
What I've tried so far :
Flipping everything to "x86" or "Win32", it seems that it might be related a problem with 64bit something
Using references to both "Audio.Default.Switcher.Wrapper.dll" and "SoundSwitch" in K005_test
copy "Audio.Default.Switcher.Wrapper.dll" pretty much everywhere and praying the gods
Trying to understand where it looks for "Audio.Default.Switcher.Wrapper.dll" but I wasn't able to find anything when looking in the exception details
Nothing has helped in any way.
For anybody wondering, I've uploaded my entire project folder here
https://drive.google.com/open?id=1OABjDiZyF0B-1-b15_9lzMrPZ4UpyKqU

Extracting frames from image in C#

I`ve seen some stackover flow on how to do this but i cant get it to work for myself in visual studio.
What is wrong with code? I have downloaded FFMpeg and im using it as reference. yet, I get the error
"Could not load file or assembly Aforge.Video.FFMPEG. dll or one of
its dependencies"
Here is the code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Video;
using AForge.Video.FFMPEG;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// create instance of video reader
VideoFileReader reader = new VideoFileReader( );
// open video file
reader.Open( "test.avi" );
// read 100 video frames out of it
for ( int i = 0; i < 100; i++ )
{
Bitmap videoFrame = reader.ReadVideoFrame( );
videoFrame.Save(i + ".bmp");
// dispose the frame when it is no longer required
videoFrame.Dispose( );
}
reader.Close( );
}
}
}
The program stop when i click the button and comes with an error.
It looks like the dll might have been moved / deleted (or) probably not checked-in. Usually the best practice is to add them in a library folder (say lib or bin) within your project. So when you check-in your stuff, all the library files get checked in too, and your references are intact.
Since the dll seems to be external, I'm hoping you browsed to it and added the reference.
Can you copy the dll file (preferably within a lib folder) within your project and then add the reference again?
It looks like Visual Studio added the reference but it's not able to find the file.
I'm guessing doing this will solve your problem.
You may need to check the way you build your project as well. Some DLLs may only work under x86 or x64 build configuration. Maybe not the parent dll directly but the references it uses internally.
You need to add to your bin folder the dlls from AForge.NET\Framework\Externals\ffmpeg and make sure you're running 32 bit (project properties -> debug -> platform -> x86

c# simple skype app throws COM exception

I am making my first Skype app that can simply message a user but when I debug I get a exception that crashes my app.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using skype_app;
using SKYPE4COMLib;
namespace skype_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
var oskype = new SKYPE4COMLib.Skype();
oskype.PlaceCall(textBox1.Text);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var oskype = new SKYPE4COMLib.Skype();
oskype.SendMessage(textBox1.Text, textBox2.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
i have use some extra references
references list:
Microsoft.Csharp
SKYPE4COMlib
SkypeDialoglib
system
system.core
system.data
system.data.DataSetEXTensions
system.deployment
system drawing
System.Windows.forms
System.xml.linq
Here is the exception i get:
System.RUntime.InteropServices.ComException : {"connection refused"}
So I guess my main question is why does my connection get refused when Skype dose not even open the dialogue asking if I want to allow the connection ?
The issue is that you're trying to debug in Visual Studio. Unfortunately, according to Skype themselves, they do not support using this API & debugging in VS:
Per the link:
The most comment cause for this is you are trying to debug the program
in Visual Studio. Going forward we will not be able to support using
the visual studio hosting process for debugging. You can turn it off
by:
Open your project in VS
Open your projects properies
click the debug tab
untick "use visual studio hosting process"
rebuild your application and begin debugging and it should work ok.
i face the same issue. this way i solved it.
here is my code
Skype skype;
skype = new SKYPE4COMLib.Skype();
Call call = skype.PlaceCall(txtPhonenNo.Text);
first thing login to skype and go to Tools > option > advanced settings
your screen would look like
click on manage other program's access to skype
then another window will come which will show all program name which try to access skype. if any exist just select all and remove it.
then run your program again and go to that screen where this option was available called click on manage other program's access to skype
click there and a windows will come which will display the name of your apps just select that name and click on change button then another window will come which looks like
in that window just select the option called allow this program to access skype then a dialog come on the skype window which looks like
where you need to click on allow access button and then your job will be done. hope this will help.

Simple C# application not working correctly, works fine when run from within Visual Studio

I'm new to C# and VS, and I have a bizarre problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ExampleApplication {
class Program {
static void Main(string[] args) {
FileInfo fi = new FileInfo("C:\\TEST.TXT");
fi.Create();
Console.WriteLine("File exists: {0}", fi.Exists);
}
}
}
This is my program, it creates a file TEST.TXT. When I run the program inside VS 2010 with the Debug -> Start Debugging command (F5), the program works fine and the file is created.
However if I build the solution then run the program from the .exe using cmd prompt:
C:\...\Visual Studio 2010\Projects\ExampleApplication\ExampleApplication\bin\Debug\ExampleApplication.exe
It runs outputting: File exists: true. But the file is not created. Anyone have any ideas?
Thanks
Perhaps file stream eventually was not released by OS so file system does not reflect recent changes? Just try explicitly closing a new file stream after creation:
FileInfo fi = new FileInfo("C:\\TEST.TXT");
using (var stream = fi.Create());
Please let us know whether it works or not since it is a bit crazy idea ;) I believe problem could be even simple
I suspect that User Account Control is redirecting the file creation to the shadow copy directory; under VS you're running as an administrator, so the file creation is not redirected.
Have a look for the file in the VirtualStore directory. That will be at
C:\Users\YourUserNameHere\AppData\Local\VirtualStore
The actual folder will be something like
C:\Users\YourUserNameHere\AppData\Local\VirtualStore\C
To solve this problem, you could change the program so that it writes the file to a location intended for user data; an obvious choice would be the Documents folder. You could also run the application with administrator privileges. There are at least three ways to do this:
Right-click the executable and choose "Run as Administrator"
Run the executable with a shortcut, after checking the "Run as Administrator" check box in the shortcut's properties dialogue
Use either method above to open an "Administrator" command prompt session; then run the application normally by entering its name at the command prompt.
Could be a permissions problem. If the user account running vS is different from the one running the command prompt, they may have different permissions for creating files. Look in your Windows event viewer to see if the error is reported there. Then, use try-catch, as Marc suggested.
Just a tip. When you work with files, always do it in a try catch. You never know what could happen like in your case.
Do you run the cmd as administrator? Maybe you don't have the rights to create the file.
Also add a try catch and return the error in the console. It would help you to figure out your problem if running as administrator doesn't work.
try
{
your code..
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
You need to call fi.Refresh(); in order to have the FileSystemInfo re-examine the base object, otherwise, it will always return false.
Try to close the file as follow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ExampleApplication {
class Program {
static void Main(string[] args) {
FileInfo fi = new FileInfo("C:\\TEST.TXT");
fi.Create();
fi.Close();
Console.WriteLine("File exists: {0}", fi.Exists);
}
}
}
I don't know the reason of this, but why won't you try this:
if(!File.Exists("C:\\TEST.TXT"))
{
File.Create("C:\\TEST.TXT");
Console.WriteLine("File exists");
}
You're doing this the wrong way. You have to dispose the file object after you're done with it. To create an empty file in .NET, I'd suggest you do it like this:
using (File.Create("C:\\TEST.TXT"));
or like this:
File.Create("C:\\TEST.TXT").Dispose();
Alternatively you could just modify your fi.Create() to:
fi.Create().Dispose();
It is likely that Visual Studio Debugger is automatically cleaning up your mess for you by properly disposing all objects when you stop the debugging.
use the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ExampleApplication {
class Program {
static void Main(string[] args) {
if(!File.Exists("C:\\TEST.TXT"))
{
GC.Collect();
GC.WaitForPendingFinalizers();
fi.Create();
fi.Close();
Console.WriteLine("File exists: {0}", fi.Exists);
}
}
}
}
This is a simple solution,
I ran you code and got a UnauthorizedAccessException which is a security exception
you do not have the right security to save a file in the root folder (C:\)
if you change it to FileInfo("TEXT.TXT"); instead of C:\TEXT.TXT
it will work
Btw, you cant use a using statement as FileInfo doesn't implement IDisposable interface.

C# reference to a dll file i created doesn't work as it should

i'm using microsoft visual C# 2010 express to write a form program to read and write to an access database.
i created a class that is designed to read/write to the database file, saved it under a namespace and created a dll from it.
it is set as ".net Framework 4"
in my main program i added the reference to the dll file but when i try to add it to the code with
using Database;
it won't work even that the Database is in the reference of the namespace.
am i doing something wrong? or is there another way to use the commands from Database in my main program other then copying it to it?
// update //
solved
added public to all database public and DataBase db = new DataBase();
DATABASE.cs is use it for dll
using System;
using System.Collections.Generic;
using System.Data.OleDb;
namespace Database
{
public class DataBase
{
public DataBase()
{
}
public void ItemInsert(string name,string creator,string publishing,string itemType,string genere, string year)
the main program
using System;
using System.Windows.Forms;
using Database;
namespace library
{
public partial class newItemForm : Form
{
private void btnConfirmNewItemClick(object sender, EventArgs e)
{
DataBase db = new DataBase(); //this solved it
db.ItemInsert(txtItemNameType.Text, txtEditorType.Text, txtCreatorType.Text, comboBoxType.Text, txtGenereType.Text, txtYearType.Text);
}
}
}
You also need to Add a Reference to said assembly in your current project. The using statement brings a referenced assembly into scope...
right click you project in visual studio, select add refrence then choose Browse tab, then find the poject folder and get in bin -> debug and then you will see the dll choose it. visual studio will add it to your refrences, now you need to add a using on top of the pages you want it like this:
using mydllName;
if you didnt find your dll:
Load the librery project agian and right click in visual studio and press Build it will generate the dll.
You must add a reference to the assembly you created. The point of creating an Assembly is not that you don't have to "copy it" to another project, but rather that you don't have to duplicate code.

Categories

Resources