I have a site for example "http://example.com" and here is settings.ini file "http://example.com/settings.ini" , in file wrriten this text:
[Client]
Enabled=1
I wan't to give that value from C#, is it possible, how to?
For example I'm using this code:
var MyIni = new IniFile(#"C:\settings.ini");
var DefaultVolume = MyIni.Read("Enabled");
MessageBox.Show(DefaultVolume);
It works good, I'm trying to make something like this:
var MyIni = new IniFile(#"http://example.com/settings.ini");
but its not work, thanks.
EDIT:
I'm getting this error:
An unhandled exception of type "System.ArgumentException" in mscorlib.dll
For more information: URI formats are not supported.
Updated:
This code gets all value from .ini, now i need to integrate to my old code
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://example.com/settings.ini");
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();
Read ini file content with HttpWebRequest (http://www.csharp-station.com/HowTo/HttpWebFetch.aspx)
You can use my library in order to retrieve your settings from an INI file:
https://github.com/MarioZ/MadMilkman.Ini
For example like the following:
WebClient client = new WebClient();
IniFile myIni = new IniFile();
myIni.Load(client.OpenRead("http://example.com/settings.ini"));
string defaultVolume = myIni.Sections["Client"].Keys["Enabled"].Value;
MessageBox.Show(defaultVolume);
Also just as an FYI, you can retrieve that value as an integer like the following:
int volume;
myIni.Sections["Client"].Keys["Enabled"].TryParseValue(out volume);
I hope this helps.
Related
I want to write my current state of a game into a JSON file so that once the user comes back they can either resume or start new. I'm creating a hello world to take user input, store it in JSON and load it back in.
I currently can load JsonObject very quickly using this method
public JObject GetJsonData(string jsonFileName, string dirNameJsonLivesIn)
{
if (string.IsNullOrEmpty(jsonFileName))
throw new ArgumentNullException(jsonFileName);
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
var defaultPath = $"{assembly.GetName().Name}.{jsonFileName}";
var extendedPath = $"{assembly.GetName().Name}.{dirNameJsonLivesIn}.{jsonFileName}";
if (string.IsNullOrEmpty(dirNameJsonLivesIn))
extendedPath = defaultPath;
Stream stream = assembly.GetManifestResourceStream(extendedPath);
using (var reader = new StreamReader(stream))
{
var jsonString = reader.ReadToEnd();
return JObject.Parse(jsonString);
}
}
With this method, I can access objects with ["strings"] just like python does very easily and painlessly.
Problem accures when I try to write to the file. I get an error Access denied... I have given permission on the manifest for Write_External_Files or something along the line. Still get the same error. I've also done some research and there has been a few line of code which people recommended to add to the MainActivity.cs but that didn't work either.
Using this method to write file
private void StoreData_Clicked(object sender, EventArgs e)
{
var jsonFileName = "Statefile.json";
var text = entryBox.Text;
var state = new Dictionary<string, string>();
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
var defaultPath = $"{assembly.GetName().Name}.{jsonFileName}";
state.Add("CurrentState", text);
var json = JsonConvert.SerializeObject(state, Formatting.Indented);
File.WriteAllText(defaultPath, json);
}
Could someone explain why this is happening? Why do I have the ability to read the external_resources but not write to them? Oh yeah, I have set my properties to Embedded Resource and also Always Copy.
Update - Error
System.UnauthorizedAccessException
Message=Access to the path "/HelloWorldXamarin.Statefile.json" is denied.
Can any one give me a proper explain why i am unable to get updated XML content from URL. I have a XML file which will frequently update. But in my application i am getting old data. Until i restart my application.
Here i am placing my code that i have tried
XmlDocument doc = new XmlDocument();;
string str;
using (var wc = new WebClient())
{
str = wc.DownloadString(location.AbsoluteUri);
}
doc.LoadXml(str);
And also tried with below code
WebRequest req = HttpWebRequest.Create("url");
using (Stream stream = req.GetResponse().GetResponseStream())
{
xmldoc.Load(stream);
}
I got to know that raw git hub take time to update in all servers so it is taking time to update. So you can use other web services to get result you want.
Background: relatively new to C# and NetSuite.
Given a little netsuite method (via SuiteTalk) such as:
private void getInvoice()
{
RecordRef invoiceRef = new RecordRef
{
internalId = "111111",
type = RecordType.invoice,
typeSpecified = true
};
ReadResponse readResponse = _service.get(invoiceRef);
}//eof
How would I get the entirety of the readResponse as a file? It is an XML file on the front-end...can I download / read that to a file at the end of this script? I don't know if its being treated as a stream here or not either, which would make it a little easier to just turn it into a file.
In your example, a request would be made to NetSuite and the response will be loaded into your "readResponse" variable as a ReadResponse class. You would then need to convert the record returned into an Invoice:
Invoice invoiceRecord = (Invoice)readResponse.record;
if you want to write the response to a file, you could do something like this:
ReadResponse readResponse = _service.get(invoiceRef);
FileStream fs = System.IO.File.Create("response.xml");
XmlSerializer writer = new XmlSerializer(typeof(ReadResponse));
writer.Serialize(fs, readResponse);
fs.Close();
I am using Out of Browser Application in silverlight.
I have problem while loading xml file usine below mentioned code.
string contentUri = Application.Current.Host.Source
.AbsoluteUri;
var contentUri1 = contentUri.Substring(0, contentUri.LastIndexOf("/")) + "/Hello1.xml";
WebClient wc = new WebClient();
wc.OpenReadCompleted+=(open,read)=>
{
string content;
using (StreamReader reader = new StreamReader(read.Result,Encoding.Unicode))
{
byte[] m_Bytes = ReadToEnd(read.Result);
string s = Encoding.UTF8.GetString(m_Bytes, 0, m_Bytes.Length);
}
};
wc.DownloadProgressChanged
+= (chang,dh)=>
{
};
wc.OpenReadAsync(new Uri(contentUri));
where my xml file contained
<Root>
<element>FirstElement</element>
</Root>
I got the garbage value as output can anyone please help me how can i download that original xml content?
When you invoke webclient calls on Out-Of-Browser mode it suppose that you have already RootVisual Created, because webclient will run on its Dispatcher apparently.
If not, you end up with no response from server, and what is strange, even exception is not thrown !!
Anyway, this Post from Jeremy explains details:
http://csharperimage.jeremylikness.com/2010/05/webclient-and-deploymentcatalog-gotchas.html
i want to open a css file using C# 4.5 and change only one file at a time.
Doing it like this gives me the exception - URI formats are not supported.
What is the most effective way to do it ?
Can I find the line and replace it without reading the whole file ?
Can the line that I am looking and than start to insert text until
cursor is pointing on some char ?
public void ChangeColor()
{
string text = File.ReadAllText("http://localhost:8080/game/Css/style.css");
text = text.Replace("class='replace'", "new value");
File.WriteAllText("D://p.htm", text);
}
I believe File.ReadAllText is expecting a file path, not a URL.
No, you cannot search/replace sections of a text file without reading and re-writing the whole file. It's just a text file, not a database.
most effective way to do it is to declare any control you want to alter the css of as "runat=server" and then modify the CssClass property of it. There is no known alternative way to modify the css file directly. Any other hacks is just that.. a hack and very innefficient way to do it.
As mentioned before File.ReadAllText does not support url. Following is a working example with WebRequest:
{
Uri uri = new Uri("http://localhost:8080/game/Css/style.css");
WebRequest req = WebRequest.Create(uri);
WebResponse web = req.GetResponse();
Stream stream = web.GetResponseStream();
string content = string.Empty;
using (StreamReader sr = new StreamReader(stream))
{
content = sr.ReadToEnd();
}
content.Replace("class='replace'", "new value");
using (StreamWriter sw = new StreamWriter("D://p.htm"))
{
sw.Write(content);
sw.Flush();
}
}