I have this simple code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.IO;
using Microsoft.Win32;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
}
}
}
which I compile when clicking a button with CSharpCodeProvider. I then load it into a new AppDomain and run the Main method, but the open file dialog is never shown. I know it is running as I have tested this.
Also trying to unload the domain results in an error.
If anymore information is needed just ask!
Most likely the AppDomain you created doesn't have the FileDialogPermission. This means of course that trying to use an OpenFileDialog will fail. See here for more information.
Assuming the namespace you're using reflects the type of application: you cannot use OpenFileDialog just like that!
Have you tried adding [STAThread] before the main function?
[STAThread]
public static void Main(string[] args)
Several components of the operating system, such as dialogs, use COM components, which to be communicated with, need to have this attribute present in the entry point of the program.
Related
Very basic knowledge on c# and is the first i use anything p/invoke related.
Help me pls, i have made this code but it doesnt seem to work.
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 System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
public static extern void SevenZipArchive(string c);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
{
foreach (ArchiveEntry entry in archive)
{
Console.WriteLine(entry.FileName);
}
}
}
}
}
It tells me that SevenZipArchive is a 'Method' and is being used like a 'type'.
I have include the library to my project already, i just dont know how to use it.
Here is the library:
https://sevenziplib.codeplex.com/
You first need to remove this code:
[DllImport("...")]
public static extern void SevenZipArchive(string c);
You do not need to provide any p/invoke declarations. The library wraps that up for you.
This is a .net assembly. You use it just as you use any other. Take the following steps:
Download the project. You already did that I think.
Build the solution. Make sure that
In your project, add a reference to the assembly that you built in the previous stage. It's SevenZipLib\bin\Debug\SevenZipLib.dll or SevenZipLib\bin\Release\SevenZipLib.dll depending on the target you selected.
Add using SevenZipLib; to your project code to to gain access to the namespace.
Once you've done that your code will work. You can use the tests project that is supplied as part of the download as a rich source of example code.
Once again i need your help.. Anywho.. I've managed to get this code below to "work". the part there doesn't work is "this". I've no idea of what to use in static and how it works. I'm rather new to this aswell, so i might need some explenation for dummies.
Anyway. This code below is suppossed to be my "mainform", where everything is loaded like icons, size, settings, menu and whatever not.
Currently i'm trying to add a global "settings". which can be loaded from all the forms. So each invidual forms would be this.ClientSize = new System.Drawing.Size(1440, 900); and anything else i may add would have same impact on the form. like icons, opacity, whatever not.
Overally it's just a place to store settings there can be accessed from any other forms.
What i've written below here, is what i've managed so far. The SettingsOnProgramStart is recognised in my Settings form, but it does not change the Clientsize or the icon. It's pobably because of the "this" as it shows red lines.
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 lala.events
{
public partial class TrayMenu : Form
{
public TrayMenu()
{
InitializeComponent();
SettingsOnProgramStart();
}
}
public static void SettingsOnProgramStart()
{
//
// Load global settings.
//
this.Icon = new Icon("images/skin/global/icon.ico");
this.ClientSize = new System.Drawing.Size(1440, 900);
}
}
}
Settings file :
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 lala.events
{
public partial class Settings : Form
{
public Settings()
{
InitializeComponent();
TrayMenu.SettingsOnProgramStart();
}
}
}
Thank you for your time and sorry for any problems this may have caused :/
I'd love if anyone could give me a useful link to a "configuration for dummies", where i'd learn about using cfg, ini for my project. so i can get the invidual settings loaded from a file.
You need to pass the form whose settings need to be set in:
public static void SettingsOnProgramStart(Form formToSet)
{
//
// Load global settings.
//
formToSet.Icon = new Icon("images/skin/global/icon.ico");
formToSet.ClientSize = new System.Drawing.Size(1440, 900);
}
Used as:
//When called from another form
TrayForm.SettingsOnProgramStart(this);
As to settings, see MSDN application settings: http://msdn.microsoft.com/en-us/library/k4s6c3a0(v=vs.110).aspx
As an aside; this code doesn't really make sense as part of the TrayForm class, as its not related to that object at all. It should likely be a member of a GlobalSettingsManager class, or something similar.
I am trying to get the SuperSocketServer app up an running. However, I cannot seem to create an instance of the server. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketBase.Protocol;
namespace WinnieServer
{
class Program
{
AppServer W = new AppServer();
static void Main(string[] args)
{
Console.Write("Starting...");
while (true)
{
}
}
}
}
I tried the examples in the source code. However, they all give the same error:
Cannot create an instance of the abstract class or interface 'SuperSocket.SocketBase.AppServer'
Is this the correct way to create an instance of SSS?
take care,
lee
I had this same issue. The download on their home screen gives you the 1.4.4 version. If you look in object explorer it IS abstract. If you go to their downloads and grab the binaries zip, you'll get the 1.5 version that ISN'T abstract.
I just open a new C# project , but when i want to compile it i give the title error . i search for this error but didn't find any result .
what am i going to do ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world!");
}
}
}
Try cleaning the project.
Make sure that the startup item isn't set
to a non-existant ConsoleApplication1.
Whatever the main form of
your project is, right click on it in Solution Explorer and select
"Set as Start Up...".
Make sure that the Program.cs (or Program.vb)
file is not attempting to load ConsoleApplication1.
In a windows application project (C#, Visual Studio 2010) I simply want to change to a new startup form (leaving the older one that has already been tested as it is). In program.cs, I comment out the "old" form and and introduce my new "clone":
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1()); // this working form uses ListBox
Application.Run(new GridForm()); // want this new form which uses DataGridView
}
Both Form1 and GridForm are in the same project and both have the same using directives. (snippet follows):
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Reflection;
using CBMI.Common;
using log4net;
using log4net.Config;
namespace CBMI.WinFormsUI
{
public partial class GridForm : Form
{
private string defaultRootFolder;
public static readonly ILog log = LogManager.GetLogger(typeof(GridForm));
public GridForm()
{
InitializeComponent();
}
Seems like a ridiculously simple question/issue but I am stumped.
EDIT-UPDATE:
Somehow in the cloning/"refactoring" process, I messed up the namespace on the newer form by introducing a case change. This was discovered by trying your suggestion as follows:
private void Form1_Load(object sender, EventArgs e)
{
CBMI.WinFormsUI.GridForm improvedForm = new CBMI.WinFormsUI.GridForm();
improvedForm.Show();
return;
Form1.cs has namespace coded as follows:
namespace CBMI.WinformsUI
GridForm.cs has namespace coded as follows (note the CamelCase):
namespace CBMI.WinFormsUI
EDIT-UPDATE: What is best practice for cloning/refactoring new form?
I forget how I hacked out the new form. I think I started by "add new item" to the project, gave it new name and saved. Then I manually copied over some code from the original and introduced new datagridview, etc. and removed the prior user interface controls from the new form. I've always found this a bit awkward trying to leave "version 1" working and introduce a new extended thing built up from the "old". How do the "pros" do this sort of thing?
That is the proper way to run the form you want. So there has to be an issue with how Main() relates to your new form. Like it being in the wrong namespace, or something to that effect.
Check it by trying to run an instance of you form from the existing startup form using:
Formname frm1 = new Formname();
frm1.show();
I assume you copied all files for Form1 (there may be up to three of them: Form1.cs Form1.Designer.cs and Form1.resx). Then did you change the class name in new *.Designer.cs? To GridForm ?
Other than this issue, there should be no problem with your code in Main. And you can test it just by adding a brand new form to the project and referencing it as you did.