我正面临一个问题.
我想在app.config文件中保存设置
我在配置文件中写了单独的类和定义的部分..
但是当我运行应用程序时.它不会将给定的值保存到配置文件中
这是SettingsClass
public class MySetting:ConfigurationSection
{
private static MySetting settings = ConfigurationManager.GetSection("MySetting") as MySetting;
public override bool IsReadOnly()
{
return false;
}
public static MySetting Settings
{
get
{
return settings;
}
}
[ConfigurationProperty("CustomerName")]
public String CustomerName
{
get
{
return settings["CustomerName"].ToString();
}
set
{
settings["CustomerName"] = value;
}
}
[ConfigurationProperty("EmailAddress")]
public String EmailAddress
{
get
{
return settings["EmailAddress"].ToString();
}
set
{
settings["EmailAddress"] = value;
}
}
public static bool Save()
{
try
{
System.Configuration.Configuration configFile = …Run Code Online (Sandbox Code Playgroud) 我想知道MSMQ(Microsoft Messaging Queue)可以在TCP或UDP上运行吗?在什么港口?
我在从WinForm应用程序访问Active Directory时遇到一些问题.我想要的是从Active Directory创建用户和查询用户.
以下是查找用户的代码段:
public bool FindUser(string username)
{
using (PrincipalContext context = new PrincipalContext(
ContextType.Domain,
this.domainName,
this.DomainUserName,
this.DomainPassword))
{
UserPrincipal user = UserPrincipal.FindByIdentity(context, username);
return (user != null) ? true : false;
}
}
Run Code Online (Sandbox Code Playgroud)
我无法PrincipalContext根据给定的参数创建对象.我得到这个例外:
Exception: The server could not be contacted.
Run Code Online (Sandbox Code Playgroud)
和内部异常表明,
Inner Exception: The LDAP server is unavailable.
Run Code Online (Sandbox Code Playgroud)
域正在运行的位置.我可以ping通它,也可以连接到这个域.
我必须在每一秒后捕获Desktop的屏幕截图.在Winform应用程序中它运行正常.但在将代码移动到Windows服务后,它没有捕获屏幕截图.知道为什么不这样做吗?
这是代码
public partial class ScreenCaptureService : ServiceBase
{
System.Timers.Timer timer = new System.Timers.Timer();
public ScreenCaptureService()
{
InitializeComponent();
this.timer.Interval = 1000;
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
CaptureScreen();
}
protected override void OnStart(string[] args)
{
if (!EventLog.SourceExists(this.ServiceName, Environment.MachineName))
{
EventLog.CreateEventSource(
new EventSourceCreationData(
this.ServiceName,
Environment.MachineName
)
);
}
EventLog.WriteEntry(this.ServiceName, "The OnStart event has been called");
this.timer.Enabled = true;
CaptureScreen();
}
protected override void OnStop()
{
EventLog.WriteEntry(this.ServiceName, "The OnStop event has been called");
this.timer.Enabled = false;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Managed Extensibility Framework(MEF)学习.Net中的可插拔体系结构.我在网上看到了示例代码,但是当我尝试实现它时,我陷入了困境.
代码使用的是:
var catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog.CreateResolver());
Run Code Online (Sandbox Code Playgroud)
这var可以在C#3.0上获得,因为我在C#2.0中进行编码.
上述两种陈述的替代方案是什么?如何使用VS 2005使它们在c#2.0中运行?
我现在尝试了这个说法
错误1 找不到类型或命名空间名称'AttributedAssemblyPartCatalog'(您是否缺少using指令或程序集引用?) C:\ Documents and Settings\test\Desktop\MEFDemo\MEFDemo\Program.cs 31 13 MEFDemo
我在哪里为SystemComponentModel.Composition添加了referance
目前我正在设计我的学位项目.几天前我开始学习LINQ.我发现它很有意思并计划在我的项目中使用它,但现在我在某些方面感到困惑.
当我添加LINQ to SQL类时,它会自动为数据库中的每个表生成实体类.
假设我在数据库中有两个表:
用户
项目
UserProjects(联合表)
以及表示哪个用户与哪个项目相关联的联合表.
LINQ to SQL类自动为我生成这三个类.现在我应该创建单独的(用户和项目)类作为业务对象还是使用这些自动生成的实体?
此外,要使用数据库功能,我们需要使用3层架构.我可以直接从我的BLL调用LINQ DAL方法,还是需要创建单独的DAL,它将调用LINQ DAL的方法?
class UserBLL
{
public void saveUser(String username, String password)
{
// here I am calling LINQ DAL from by BLL
UserDataContext db = new UserDataContext();
User u =new User {Username = username, Password = password};
db.user.InsertOnSubmit(u);
db.SubmitChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
以上方法调用序列是否正常?
我正在开发一个小型TCP客户端/服务器库.
我在创建客户端并将其连接到服务器时遇到此问题.它给了我这个例外
通常只允许使用每个套接字地址(协议/网络地址/端口)
我的代码是.
public TCPClient(string remoteIPAddress, int port)
{
this.remoteIPAddress = IPAddress.Parse(remoteIPAddress);
this.port = port;
IPEndPoint remoteEndPoint = new IPEndPoint(this.remoteIPAddress, this.port);
tcpClient = new TcpClient(remoteEndPoint);
}
Run Code Online (Sandbox Code Playgroud)
这是TCPServer
public TCPServer(int listeningPort)
{
this.listeningPort = listeningPort;
tcpListenter = new TcpListener(this.listeningPort);
workers = new List<TCPServerWorker>();
this.keepRunning = false;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,为什么我得到这个例外
我有以下xml
<students>
<student>
<id>12</id>
<name>Mohsan</name>
</student>
<student>
<id>2</id>
</student>
<student>
<id>3</id>
<name>Azhar</name>
</student>
</students>
Run Code Online (Sandbox Code Playgroud)
请注意,在2名称元素中缺失.
我必须使用Linq to XML读取这个xml
我使用以下代码来获取所有学生..
请建议我改进此代码
var stds = from std in doc.Descendants("student")
select new
{
ID = std.Element("id").Value,
Name = (std.Element("name")!=null)?std.Element("name").Value:string.Empty
};
Run Code Online (Sandbox Code Playgroud) 我已经创建了一个TCP服务器.我面临一个问题.我的TCP服务器没有收到大于30000字节的数据.
这是接收数据的代码
MAX_TCP_DATA = 64000
private void Process()
{
if (client.Connected == true)
{
log.InfoFormat("Client connected :: {0}", client.Client.RemoteEndPoint);
Byte[] bytes = new Byte[MAX_TCP_DATA];
String data = null;
NetworkStream stream = client.GetStream();
int i;
try
{
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// bytes contains received data in byte[].
// Translate data bytes to a UTF-8 string.
byte[] receivedBuffer = new byte[i];
Array.Copy(bytes, receivedBuffer, i);
if …Run Code Online (Sandbox Code Playgroud) 我必须从保存在位组合的三个部分中的字节中获取值.
位组合如下
| - - | - - - | - - - |
第一部分包含两位第二部分包含3位第三部分包含3位
样本值是
11010001 = 209十进制
我想要的是创建三个不同的属性,它获得如上定义的给定位的三个部分的十进制值.
如何从这个十进制数中获取位值,然后从各个位获取十进制值..
我在项目中遇到了一些问题.当我尝试更新实体时,它给了我不同类型的错误.
我从网上读.这些错误是因为
1 - 我从本地创建DataContext的方法获取实体类的Object
并且在update方法中,id不会更新,因为这里在本地创建了另一个DataContext.(即使它没有抛出任何异常)
我找到了许多与此问题相关的文章
1 - 在表中添加时间戳列(在我的项目中不起作用.我试过这个)
一个人说为每个人使用SINGLE DataContext.
我通过创建以下类来完成此操作
public class Factory
{
private static LinqDemoDbDataContext db = null;
public static LinqDemoDbDataContext DB
{
get
{
if (db == null)
db = new LinqDemoDbDataContext();
return db;
}
}
}
public static Student GetStudent(long id)
{
LinqDemoDbDataContext db = Factory.DB;
//LinqDemoDbDataContext db = new LinqDemoDbDataContext();
Student std = (from s in db.Students
where s.ID == id
select s).Single();
return std;
}
public static void UpdateStudent(long studentId, string …Run Code Online (Sandbox Code Playgroud) c# ×7
tcp ×3
c#-3.0 ×2
linq ×2
sockets ×2
.net-3.5 ×1
3-tier ×1
app-config ×1
asp.net ×1
concurrency ×1
datacontext ×1
linq-to-xml ×1
mef ×1
msmq ×1
udp ×1
var ×1