|
Hi. I am trying to upload and download files to and from my web server. I Keep getting a 405 with WebClient. I understand the problem is that i don't have access. I tried to send a username password with WebClient.Credentials, but it still doesn't work. Is there any way to get permission from a web server to upload a file and then upload it?
-- Steve
|
|
|
|
|
Yes - by using the Credentials property and assigning an ICredentials implementation. If you're using CredentialCache.DefaultCredentials , then it probably won't work unless you're connecting to a site that requires NTLM authentication with your domain account (which is about all that would be in the DefaultCredentials s).
Instead, create a new instance of the CredentialCache to hold multiple credentials, or just a NetworkCredential to hold a single credential.
If you look at the class documentation for the CredentialCache class in the .NET Framework SDK (installed by default with VS.NET), it even gives an example of using both classes mentioned above. For the former, you instantiate a new NetworkCredential and add it to a CredentialCache associated with a given Uri and specifying the authentication type ("Digest", "NTLM", "Kerberos", etc.).
If you wanted to download a file from www.domain.com with username:password, you'd do something like this:
WebClient client = new WebClient();
CredentialCache cache = new CredentialCache();
cache.Add(new Uri("http://www.domain.com/"), "Basic",
new NetworkCredential("username", "password", "DOMAIN")));
client.Credentials = cache;
client.DownloadFile("http://www.domain.com/file.txt", "file.txt"); Read the documentation for NetworkCredential to see what password schemes are supported. You can add additional schemes, if necessary, by implementing the IAuthenticationModule , also - of course - documented in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hey. I'm still having problems with uploading. I am using a linux web server. do you think that is causing the trouble? If so, what can i do to get it working?
-- Steve
|
|
|
|
|
You're code uses the HTTP protocol, which is a specification that is independent of the host OS; so, no, it doesn't matter that you're running linux on the server.
If you're having problems, please be specific. The code I gave you is a sample that you should research further, like reading the method and property documentation in the .NET Framework SDK if you don't understand a particular line of code. You should also read - which I mentioned - which authentication protocols are supported. If your web server (presumably Apache) is uses an unsupported authentication mechanism, then you're have to implement the IAuthenticationModule interface and roll your own authentication module.
With specifics regarding your problem, I really can't help you.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi
I have a VC++ program which has a struct defined. I am trying to declare the same struct in c# and am having some problem
--In VC++ , lets say my struct looked like
#define MAX_UNITS 100
struct _MyStruct{
int X;
int Y;
char Units[MAX_UNITS];
};
I can do a sizeof(_MyStruct) and it return me the size including the 100 char array in the struct
--In C#, i tried to declare the following
// all in an unsafe class
struct _MyStruct{
int X;
int Y;
char [] Units = new char[MAX_UNITS];
};
//gives me compile time error about initialising in the struct
so I tried;
struct _MyStruct{
int X;
int Y;
char [] Units;
};
but now i cant use sizeof, and if i use the marshal.sizeof, it returns 12. I need the size with the 100 char array. Any suggestions
thanks
|
|
|
|
|
You are not allowed do field initialization in structs. If you want this behavior you have to use a class. Or if you are inclined you can wait to initialize that field later.
|
|
|
|
|
If you want, you can define a non-default constructor that sets fields to the parameters you specify, and initializes the other fields to whatever you want. You cannot define a default constructor (i.e., a constructor with no params) for a struct, though.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I am too embarrassed to ask this silly question. But I need help. Can any1 tell me, how can I access between forms in C#? For some weird reason I can’t access. I created an object of the form I am trying to access but it’s referring to null.
Say, i have got two forms name "formA" and "formB". I have got five buttons in "formA". How can i access those buttons from formB.
N.B. All the button got public access.
If anyone could help, it would be greatly appreciated. I could attach sample code but i can't see any options here. So, if u give me e-mail then i could send it to ya.
Ta
B
|
|
|
|
|
Well Forms.Controls is public and should be available to anyone outside the "formA" object. I'm kind of confused how you don't have access to either this or the individual controls if you claim they are all public.
|
|
|
|
|
To add to what Tom said. You should not declare fields in C# as public, you should declare them as private or protected and then provide public properties to access them. Declaring fields as public breaks the concept of encapsulation in Object Orientation.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
|
|
|
|
|
|
Mehbub wrote:
I could attach sample code but i can't see any options here
Try pasting the code section where you create the form and it is refering to null.
<font=arial>Weiye Chen
Life is hard, yet we are made of flesh...
|
|
|
|
|
Ok, i have received your code. Let me paste some of it here so that the rest of the developers can give their advice too.
FormA.cs
private void Form1_Load(object sender, System.EventArgs e)
{
Form frmB = new FormB();
frmB.MdiParent = this;
frmB.Show();
}
FormB.cs
private void btnAccessFormA_Click(object sender, System.EventArgs e)
{
Form frmA = new FormA();
}
Mehbub wrote:
How can i access those buttons from formB.
You can't access the same form A's buttons in form B by creating Form A again. Since FormA is your mdi parent and FormB is the child, you can use the MdiParent property of FormB. e.g.
FormB.cs
private void btnAccessFormA_Click(object sender, System.EventArgs e)
{
this.MdiParent.button1.Visible = true;
} I'm not sure if you need to cast it to FormA first like :
((FormA)this.MdiParent).button1.Visible = true; I don't have VS.NET on my pc here to test it out.
<font=arial>Weiye Chen
Life is hard, yet we are made of flesh...
|
|
|
|
|
Lately I've been getting pretty excited with Interop stuff, but I have one problem, I have no idea what the parameters for a particular .dll method are. I know how to view all the exported functions in a method using DUMPBIN or LINK (if anyone knows a better way, I'm all ears) but it doesn't tell me anything about the parameters. Any ideas?
|
|
|
|
|
If you're talking about native DLLs (which you most likely are since you're viewing the EAT with DUMPBIN), this is not even possible. The exported functions are merely RVAs (relative virtual addresses). The execution stack contains pointers or data that the function call pops from. The parameters are not known except by the function itself, so they are not in the EAT (export address table).
All you can do is consult the documentation. If these are Windows DLLs, most APIs are documented in the MSDN Library[^], which should be your first stop for any questions about Windows platform developer (contains the Platform SDK, the .NET Framework SDK, Web Development, and many other Windows-related documentation).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
If you're talking about native DLLs (which you most likely are since you're viewing the EAT with DUMPBIN), this is not even possible.
That's really good to know. Thanks a ton Heath. Looks like you're answering everybody's questions today.
|
|
|
|
|
Hi all,
I'd like to know how would i be able to code an application to copy the time (copy a time stamp) from a client computers clock.
Example Program:
A simple windows application with a button on it,
when i click the button,
the client computer returns a time stamp or time strap from the computer's clock to the form "04:17pm", to a label or textbox.
The source code would be very much appreciated ..!
Thanx!
|
|
|
|
|
Logically, you should look at the DateTime structure in the .NET Framework SDK, which has a static property called Now . If you further look at the documentation for ToString , the solution's quite obvious:
textBox1.Text = DateTime.Now.ToString("hh:mmtt");
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I would like to share a solution with all of you guys to the Response.Redirect problem.
I have a scenario:
1. An asp page containing two submit type buttons 'Back' and 'Save'.
2. Save may do some action and stay in the same page..
3. But Back would do same as save button..but Goes to Home Page.
4. U may have Save_Click () serverside code in aspx.cs
5. U can simulate the same from back.. and also hidBack to maintain from which button its coming from...
<input type="image" id="Button2" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 136px"
value="Back" OnClick="return ClickBack()">
<script language="javascript">
function ClickBack()
{
document.Form1.hidBack.value = "YES";
document.Form1.Button1.click();
//IMPORTANT:: RETURN FALSE TO NULLIFY POSTING BY BACK BUTTON.
return false;
}
6. Server side code may look as
private void Save_Click(object sender, System.EventArgs e)
{
if(Request.Form["hidBack"] == "YES")
{
Page.Response.Redirect("Home.aspx",false);
}
Response.Write(" U pressed first Button");
}
</script>
7.if WE DONT HAVE //IMPORTANT:: code line, it will postback twice to the server..there by Response.Redirect doesnt work.
8. Any other observations?? thanks
Phani, Virtusa.
|
|
|
|
|
|
I am creating a web page in C# using XML and XSLT. I am able to access the web controls but I am unable to access the 'file' control.
I am using the code in C# like this
XPathDocument ctrlCategoryDoc = new XPathDocument(Server.MapPath("XML/NewCategory.xml"));
XPathNodeIterator itr = ctrlCategoryDoc.CreateNavigator().Select("//Ctrl");
System.Text.StringBuilder sb;
sb = new System.Text.StringBuilder();
// foreach Ctrl
while (itr.MoveNext())
{
// get the control name
string controlName = itr.Current.GetAttribute("name", "");
sb.Append(controlName);
sb.Append(" : ");
// get the control
object ctrl = FindControl(controlName);
}
the object 'ctrl' contains name of the control in case of web control but in case of html control () it is giving undefined value.
can anyone tell me the solution for this
|
|
|
|
|
For one, this belongs in the ASP.NET forum.
Also, drop the spaces in sb.Append(" : "); . You won't find a control within an INamingContainer implementation named like that.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everybody . I got a little question about distributing a .Net C# Application. Many of you already did such things, and maybe you find the time to give me a hint on doing this
I created a small C# Application and I wish to upload it so that anybody can use it. I gave it to a friend for testing but the problem is that the Application crashes when you try to run the executable. The target computer already had the .Net Framework 1.1 installed.
What I sent to my friend is the executable and two DLLs. The program uses the WinSock control and the two DLLs are something like AxInteropWinSock.dll or something like this, the only difference is that one of them does not have the "Ax" in its name. This is not so important... what's important is that I sent the .exe and the two DLLs in the same folder. I took them from the "Release" folder of my .Net Solution, after I ran the solution using "Release" configuration.
I saw on the Internet stuff about using MSI files to make a setup or assemblies. I don't know how to use those and the Microsoft documentation is terrible. I would prefer to give this to somebody in a zip file, without any complicated setups and stuff like that. It's a simple application, I'd rather avoid using MSI files and so on.
Any help is greatly appreciated, thank you for your time and have a nice day.
Axonn.
The Greater Mind Balance
|
|
|
|
|
MSI is not a magic bullet that will solve your problems. Obviously, something is missing. If all you use MSI for is copying those assemblies into a folder, then something as simple as COPY or XCOPY or just zipping it up will do fine.
The difference between Interop.something.dll and AxInterop.something.dll is, by default, that the Interop assembly contains the typelib wrapper (RCW, or Runtime Callable Wrapper) and the AxInterop assembly contains the control(s) that extend AxHost , a Control which wraps ActiveX controls.
Distributing those assemblies isn't enough, however. Since they are only wrappers, you need to distribute the WinSock COM control that you're wrapping. All those assemblies do is make the COM control and typelib useable in .NET (through the RCW).
This is where MSI would be handy. COM controls need to be registered on the system and MSI can do that easily. You can add the file to the File System editor and in the file's properties (in the PropertyGrid), set the registration to SelfReg (something like that).
You don't need to do this, though. If you don't plan on mass-deploying this, just direct users to download the WinSock COM control and install it it, if it has it's own installer.
If you do want to distribute this to anyone, though, I'd recommend using an MSI package. VS.NET's Windows Installer projects are very simple to learn, but are by no means complete (for our installs, I use Wise for Windows Installer, which is a full-blown authoring package for MSIs and MSMs from Wise[^]). The VS.NET is simple to use and you should explore it a little.
For example, in the File System editor you can add folders to whatever pre-defined folder you want, then add your project outputs (handy when you want to maintain version compatibility within a solution). Just right click in the folders and seld Add File, choose Project, browse to the project you want and select Build Ouput. Now when you build the MSI project it will make sure the other projects are up-to-date and automatically grab the build outputs (the assemblies) from the projects' target directories.
Also add the COM control if the VS.NET Windows Installer project doesn't do it already (it often will). Make sure that it's set to register itself as I mentioned above.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you for your answer Heath. I can see why you are a MVP . The problem is that I`m kind of new to the .Net "peripherical utilities". I've been developing programs in .Net at work for a year, but I never had to distribute one. Now I made a little IRC client at home and I want to deliver it and I have problems...
Not to mention that I don't know what's that XCOPY or "File System Editor" and where I can find them. But I have MSDN and I will search, that's the least of my worries.
First of all... the File System editor can help me? I can add files to my project using that, maybe, but will it register the WinSock OCX control?
Can you tell me if the Windows Installer made be Wise is free?
Thank you, have a nice day
The Greater Mind Balance
|
|
|
|
|