这有点棘手.
我正在异步上传文件到FTP.上传每个文件后,我正在检查该文件的上传操作的状态.这可以使用该请求的FtpWebResponse对象的StatusCode属性来完成.代码段如下所示.
FileStream fs = File.Open(fileName, FileMode.Open);
while ((iWork = fs.Read(buf, 0, buf.Length)) > 0)
requestStream.Write(buf, 0, iWork);
requestStream.Close();
FtpWebResponse wrRet = ((FtpWebResponse)state.Request.GetResponse());
Run Code Online (Sandbox Code Playgroud)
根据msdn,大约有37个StatusCode值.我不知道这些状态代码值中的哪一个将确保文件成功上传.我在代码中用来检查成功的其中一些是:
wrRet.StatusCode == FtpStatusCode.CommandOK
wrRet.StatusCode == FtpStatusCode.ClosingData
wrRet.StatusCode == FtpStatusCode.ClosingControl
wrRet.StatusCode == FtpStatusCode.ConnectionClosed
wrRet.StatusCode == FtpStatusCode.FileActionOK
wrRet.StatusCode == FtpStatusCode.FileStatus
Run Code Online (Sandbox Code Playgroud)
但我不知道其余的.我需要确定这些代码,因为基于上传操作的失败或成功,我还要执行其他相关操作.错误的条件会影响剩余的代码.我想到的另一个想法是简单地将上面的代码放入try..catch而不依赖于这些状态代码.有了这个,我不会依赖于状态代码,并假设任何失败将始终指向catch块.如果这是正确的方法,请告诉我.
关于扩展方法的一些问题:
为什么扩展方法是静态的?
为什么需要在静态类中声明它们?
这个关键字在扩展方法的参数列表中表示什么?由于它是一个静态类,"this"关键字如何在此上下文中工作?
对于这些类型的方法,内存分配是如何发生的?
在加载excel文件之前,C#中是否有办法在excel中关闭宏.我正在使用Excel Interop加载excel文件.我只能看到VB.NET的解决方案.
谢谢Nishant
我在我的机器上安装了SQL Server 2008(Windows XP SP3).
我还在另一台有.net应用程序的机器上安装了Visual Studio 2008.现在,当我尝试从我的.NET应用程序连接到SQL Server时.
我无法这样做.我收到一条错误消息:
由于登录前连接初始化过程中的错误,客户端无法建立连接.可能的原因包括:客户端尝试连接到不受支持的SQL Server版本; 服务器太忙,无法接受新连接; 或者服务器上存在资源限制(内存不足或允许的最大连接数).(提供者:命名管道提供者,错误:0 - 管道的另一端没有进程.)
是因为客户端版本是2005(默认使用Visual Studio 2008安装)而SQL Server是2008?
我理解虚函数是什么.但我没有得到的是他们如何在内部工作?
class Animal
{
virtual string Eat()
{
return @"Eat undefined";
}
}
class Human : Animal
{
override string Eat()
{
return @"Eat like a Human";
}
}
class Dog : Animal
{
new string Eat()
{
return @"Eat like a Dog";
}
}
static void Main()
{
Animal _animal = new Human();
Console.WriteLine(_animal.Eat());
_animal = new Dog();
Console.WriteLine(_animal.Eat());
}
Run Code Online (Sandbox Code Playgroud)
上面的输出给出:
Eat like a Human
Eat undefined
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,_animal是Animal类型,它引用了Human对象或Dog对象.这是什么意思?我理解在内存中_animal包含一个指向Human或Dog对象的地址.它如何决定调用哪个函数.在第一种情况下,我覆盖,因此调用了子的实现,但在第二种情况下,我使用new,因此调用了父实现.能告诉我发动机罩下发生了什么吗?
在此先感谢尼克
我想写一个常用的方法来检查表单是否已经打开.如果它是打开的,那么只需激活它.否则显示它.现在我的挑战是我将哪种类型的参数传递给Test方法?
private void Test(?? ??)
{
bool isFormOpen = false;
foreach (Form form in Application.OpenForms)
{
if (form is ??)
{
isFormOpen = true;
form.Activate();
}
}
if (!isFormOpen)
{
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢Nishant
这是我的xml
<root>
<children>
<child id = "1">
<description>This is child 1</description>
</child>
<child id = "2">
<description>This is child 2</description>
</child>
<child id = "3">
<description>This is child 3</description>
</child>
</children>
</root>
Run Code Online (Sandbox Code Playgroud)
我正在尝试更新名为Child的表,其中包含"ID"和"Description"列.该表中已包含ID列值,但描述为空.我需要根据上面给出的xml文件中的ID更新此描述.
我尝试使用标志值为2(以元素为中心)执行OPENXML,并且能够检索所有描述.但我不知道如何使用OPENXML中的where子句基于ID值检索描述.
我使用的数据库是SQL Server 2008.
(OPENXML也适用于SQL Server 2005吗?)
这是我试图做的wat:
DECLARE @idoc int DECLARE @doc xml
select @doc= c from openrowset(bulk 'C:\Test.xml',single_blob) as temp(c)
exec sp_xml_preparedocument @idoc output, @doc
SELECT * FROM OPENXML (@idoc, '/root/children/child', 2)
WITH (summary varchar(1000)) descr
EXEC sp_xml_removedocument @idoc
Run Code Online (Sandbox Code Playgroud)
在此先感谢尼克
ps:无法更改xml的结构.我需要解决这个问题.
我仍然不清楚 .NET 中 Streams 的概念
文件流例如:
using (FileStream fs = File.Open(C:\temp\Test.txt, FileMode.Open, FileAccess.Write, FileShare.None))
Run Code Online (Sandbox Code Playgroud)
上面的代码给我一个 FileStrem 对象。如果我的理解是正确的,我得到的 FileStream 对象是文件 C:\temp\Test.txt 的字节表示形式
我的问题:是否有对文件 C:\temp\Test.txt 的物理引用。
FileStream 只是底层文件字节表示的抽象吗?如果是,那么我可以传递这个 FileStream 对象来表示驻留在其他 Windows 计算机上的 Web 服务吗?
还有什么时候适合使用流。考虑一个场景,我需要从某个远程目录读取文件并将其通过 SFTP 传输到某个位置。在这里创建 FIleStream 有意义吗?
为什么图片框控件的可见性属性在此处不起作用.我最初将它们设置为false,以便在屏幕加载时它们不可见.但后来我想切换这个.我做了以下但似乎没有工作.这是一个Windows窗体应用程序.
private void Action()
{
while (true)
{
Random r1 = new Random();
int num = r1.Next(1,3);
switch (num)
{
case 1:
pictureBoxLeft.Visible = true;
pictureBoxRight.Visible = true;
break;
case 2:
pictureBoxLeft.Visible = true;
pictureBoxRight.Visible = false;
break;
case 3:
pictureBoxLeft.Visible = false;
pictureBoxRight.Visible = true;
break;
}
Thread.Sleep(200);
pictureBoxLeft.Visible = false;
pictureBoxRight.Visible = false;
Thread.Sleep(500);
}
}
Run Code Online (Sandbox Code Playgroud)
还要添加,这与文本框正常工作!有任何想法吗???
提前谢谢了
c# ×5
.net ×4
com-interop ×1
excel ×1
filter ×1
flags ×1
interop ×1
methods ×1
picturebox ×1
reflection ×1
sql ×1
sql-server ×1
stream ×1
t-sql ×1
virtual ×1