String in a XML - c#

I am wondering why when I write this in the URL:
http://ServerNameRedacted/IISHostedCalcService/FilesService.svc/GetFoldersAndFiles?selectedFolder=FOLDER//SUBFOLDER
I can't get the information in XML.
But when I use this, the parameter in my function which is exactly the same thing FOLDER//SUBFOLDER it doesn't work...
If I write it directly in a string it will work. So I don't know why, because the parameter is exactly the same...
Should I use string.format or something?
void listBoxFolders_Tap(object sender, GestureEventArgs e)
{
Folder directory = new Folder();
directory = (Folder)listBoxFolders.SelectedItem;
// Connexion to the webservice to get the subfolders and also the files
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadSubFoldersAndFiles);
wc.DownloadStringAsync(new Uri("http://myserver.net/IISHostedCalcService/FilesService.svc/GetFoldersAndFiles?selectedFolder=" + directory.FullPath.Substring(25) + '/'));
}

Considering that you're adding a forward slash at the end of the URI in your code, I would submit that they are not, in fact, the same.
Create your URI string outside of the DownloadStringAsync method, and use a a System.Diagnostics.Debug.WriteLine to see what the actual contents are of your URI string.

Related

Using Process.Start in C#

I want to start python aplication from C# so I use method Process. After I ran it and click the button I get
System.ComponentModel.Win32Exception: The system cannot find the file
specified
In either case. What's wrong?
int counter = 0;
private void button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
counter++;
TextBox1.Text = counter.ToString();
//Process.Start("Python.exe", "C:\\Users\\kamil\\source\\PythonApplication2\\PythonApplication2.py");
Process.Start("IExplore.exe", "www.northwindtraders.com");
}
The issue can be the full path of python.exe and I explore.exe. You can specify full path for them. Even a path can be added to PATH env variable. If a path is specified in PATH environment vatriable you don't need to specify the full path. You can change it like this:
var old = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine)
var new = old + #"Full path, here you don't need to escape double quotes."
Environment.SetEnvironmentVariable("PATH", new, EnvironmentVariableTarget.Machine)
It is a good practice to include path inside #"" here special characters don't need escaping.

How to fix C# google search

So I'm creating a program that can go into google and search for the word that has
been saved into a variable. So my question is how I can do this, without having it go to a URL instead just going to Google and search "whatever" Here's the code.
private void button2_Click(object sender, EventArgs e)
{
if (Script.Text == Script.Text)
{
Console.AppendText("\n[1] Loading Websites of " + Script.Text + "...");
}
WebClient wc = new WebClient();
Process.Start(#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "Scripts " + Script.Text);
}
This should solve your problem
public static void GoToSite(string url)
{
System.Diagnostics.Process.Start("chrome.exe", url);
}
you can also change browser dynamically by adding a second params
You can also do this:
String stringToSearch = “stackoverflow”;
System.Diagnostics.Process.Start(#"C:\Program Files\Internet Explorer\iexplore.exe", "http://www.google.com.au/search?q=" + stringToSearch);
This assumes IE is the default browser on your machine. If Chrome or Firefox is the default browser, then code will fail.
This is a useful link:
http://www.seirer.net/blog/2014/6/10/solved-how-to-open-a-url-in-the-default-browser-in-csharp
Maybe you can put it in a try catch block and see if there is an exception. If there is an exception with starting IE, then open chrome.

Issues with URI and C#: URI starts with "./", cannot be read or used as path

I have a simple function, that is supposed to take in one filepath, as a string, then create a relative path based on the main application working directory:
private string ConvertToLocal ( string path )
{
Uri finalURI;
Uri applicationURI = new Uri ( System.AppDomain.CurrentDomain.BaseDirectory );
string rebuiltPath = applicationURI.ToString () + path;
Uri targetURI = new Uri ( rebuiltPath );
finalURI = applicationURI.MakeRelativeUri ( targetURI );
return finalURI.ToString ();
}
It should return a nice simple string, which is afterwards used to save the relative URI. The idea is to allow to load assets independently of the current directory. I thought it would generate a path like "\Resources\Images\img.png", but it doesn't work that way. Not only that, but I keep getting an exception due to how the string is formatted, as it always comes with a dot slash at the beginning, such as: "./C:/Users/Foo/Bar/Resources/Images/img.png" and if I try to use that it produces an exception. I couldn't find any help, the documentation doesn't mention anything, so I am asking here hoping for some help. Thank you.
I think you set the path C:/Users/Foo/Bar/Resources/Images/img.png to variable path。so the rebuildPath isnot a valid path and return an unexpected result .If you want to return like "/Resources/Images/img.png"
you should set /Resources/Images/img.png to path variable.About MakeRelativeUri you can refer the below:http://msdn.microsoft.com/en-us/library/system.uri.makerelativeuri(v=vs.100).aspx

Getting a what i believe to be a webclient error in C#, Need help Please

Okay so i am fairly new to programming and was told to just start acouple projects and google/ask if i need hep so here i am, im making a program that will download and run a file from the internet and when i run the release app i get this error http://gyazo.com/7b017f0ed550af3b86b24ad480db8fe8 so i press the details and it give me this long error report thing http://pastebin.com/Ag7u9gBZ i dont seem to have enough knowledge to be able to read and debug it, and i am very confussed, from what i see it has to do with system.io and webclient and path or something i dont know.
private void startBotToolStripMenuItem_Click(object sender, EventArgs e)
{
string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe";
string direct_exe_from_url = "http://rs542p2.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=1764003915&filename=Minecraft.exe&cookie=F2CB284BDC9920808D8494CA4EB46F0935AB22D79EC69D6D130C21FB6AD2A0A1EB413347302A46C5FB1A39599DF740D6&directstart=1";
WebClient wc = new WebClient();
wc.DownloadFile(direct_exe_from_url, filepath);
Process.Start(filepath);
}
You're getting a System.IO.DirectoryNotFoundException so I would guess that this: C:\Users\Owner\AppData\Roaming\Downloaded Files\ directory does not exist.
You can use the Directory.CreateDirectory method to create the directory before putting the downloaded file in there, e.g:
string directoryPath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\";
string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
string direct_exe_from_url = "http://rs542p2.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=1764003915&filename=Minecraft.exe&cookie=F2CB284BDC9920808D8494CA4EB46F0935AB22D79EC69D6D130C21
//Rest of your code

Downloaded file using webclient.DownloadFileAsync has 0KB

I'm trying to download zend-framework (from http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip) simply using WebClient
string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip";
WebClient downloader= new WebClient();
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
The file is created, but it is empty. I checked response using fiddler and I get HTTP 200, correct content-length but "connection: closed" and fiddler shows "-1" in "body" column.
I have tried adding user agent (copied from google chrome request) and "connection: keep-alive" to headers, but none of these helped. I'm also pretty sure, that my program downloaded this file using the same URL once or twice before. There are no errors in events fired by WebClient.
Any ideas?
Ok, I finnaly found the answer! Before downloading the file, I was checking its size by sending HttpWebRequest. The problem was, that i didn't Close() the response.
Thanks for the answers, they were nice clues.
Just my guess: maybe you can try to keep the WebClient instance in some place would not be garbage collected. When the DownloadFileCompleted event fired, you just clean the reference to the WebClient instance and let GC to reclaim the memory later (and don't forget to call Dispose method).
Try to handle the DownloadProgressChanged and DownloadFileCompleted event.
private void button1_Click(object sender, EventArgs e)
{
string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip";
WebClient downloader = new WebClient();
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(downloader_DownloadFileCompleted);
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
}
void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
label1.Text = e.BytesReceived + " " + e.ProgressPercentage;
}
void downloader_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
MessageBox.Show(e.Error.Message);
else
MessageBox.Show("Completed!!!");
}
If you have UAC enabled in Windows "C:\temp.zip" in the following line will fail to save the file because you aren't allowed to write outside of user directories without elevated permissions:
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");

Categories