I save a png file using silverlight. But I want to save it in application IMG folder. My code is as follows:
if (lastSnapshot != null)
{
string ImageName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
+ DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".png";
string filePath=System.IO.Path.Combine("~/Images/", "" + ImageName + "");
using (var pngStream = GetPngStream(lastSnapshot))
using (var file = File.Create(filePath))
{
byte[] binaryData = new Byte[pngStream.Length];
long bytesRead = pngStream.Read(binaryData, 0, (int)pngStream.Length);
file.Write(binaryData, 0, (int)pngStream.Length);
file.Flush();
file.Close();
}
}
I want to do it in silverlight. Can I do it? Can I save file directly in application folder? How to do it? I'll be grateful to anyone who will help me. Thanks in advance.
Adjacent question of mine
You will have to provide a webservice or upload url at the server side and use that from within Silverlight at the client.
Silverlight applications to NOT have direct access the the server's folders because Silverlight executes at the client.
Related
I am trying to create a console app on network version 5.0 using visual studio. The purpose is to read all the PNG files in a directory, and make a JSON file with the code:
{
"format_version": "1.16.100",
"minecraft:texture_set": {
"color": "*Filename*",
"metalness_emissive_roughness": "*Filename_mer*"
}
}
In it. And yes this is for Minecraft. The filename and the filename_mer are automatically filled in by code, but that isn't my issue.
static void Main(string[] args)
{
Console.WriteLine("Goto " + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Packages\\Microsoft.MinecraftUWP_8wekyb3d8bbwe\\LocalState\\games\\com.mojang\\resource_packs" + " And find the resource pack you want to generate files into");
string pathname = Console.ReadLine();
#region Message
Console.WriteLine();
Console.WriteLine("Looking for folder...");
#endregion
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Packages\\Microsoft.MinecraftUWP_8wekyb3d8bbwe\\LocalState\\games\\com.mojang\\resource_packs\\" + pathname + "\\textures";
#region Message
Console.WriteLine();
Console.WriteLine("Folder found in");
Console.WriteLine(path);
#endregion
string[] directories = Directory.GetDirectories(path);
foreach (string paths in directories)
{
string[] files = Directory.GetFiles(paths);
foreach (string file in files)
{
string filenameWithType = file.Substring(file.LastIndexOf("\\") + 1);
string filename = filenameWithType.Substring(0, filenameWithType.LastIndexOf("."));
string thisPath = file.Substring(0, file.LastIndexOf("\\"));
if (filenameWithType.Substring(filenameWithType.LastIndexOf(".") + 1) == "png")
{
string newPath = thisPath + "\\" + filename + ".texture_set.json";
File.Create(newPath);
List<string> codeInFile = new List<string>();
codeInFile.Clear();
codeInFile.Add("{");
codeInFile.Add("\"format_version\": \"1.16.100\",");
codeInFile.Add("\"minecraft:texture_set\": {");
codeInFile.Add("\"color\": \"" + filename + "\",");
codeInFile.Add("\"metalness_emissive_roughness\": \"" + filename + "_mer\"");
codeInFile.Add("}");
codeInFile.Add("}");
TextWriter tw = new StreamWriter(newPath, false);
foreach (string line in codeInFile)
{
tw.WriteLine(line);
Console.WriteLine(line);
}
tw.Close();
string newPathtxt = thisPath + "\\" + filename + "_mer.png";
Bitmap bitmap = new Bitmap(file);
using (Bitmap b = new Bitmap(bitmap.Width, bitmap.Height))
{
using (Graphics g = Graphics.FromImage(b))
{
g.Clear(Color.Green);
}
b.Save(newPathtxt, ImageFormat.Png);
}
}
}
}
#region Message
Console.WriteLine("");
Console.WriteLine("All done, Your good to go!");
#endregion
Console.Read();
}
This is all my code, on the file. It reads a directory which you enter, looks through the textures file, and for each PNG file in there it creates a JSON file with the code specified earlier. Although when run the code gives me this error.
System.IO.IOException: 'The process cannot access the file 'C:\Users\https\AppData\Local\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang\resource_packs\Vanilla_Resource_Pack_1.17.10\textures\blocks\acacia_trapdoor.texture_set.json' because it is being used by another process.'
I cannot find any answers which fit my issue, I would appreciate any help.
This is a Minecraft Bedrock edition resource pack btw, not sure if that helps.
Don't do this File.Create(newPath); or rethink your problem. It looks like a typo.
In short File.Create(newPath) is creating a FileStream and discarding it, leaving the file open and with a share lock. If you are trying to pre-create the file, at least use the using statement:
using (File.Create(newPath));
or Close
File.Create(newPath).Close();
or
File.WriteAllText(newPath, String.Empty);
or
File.WriteAllBytes(newPath, Array.Empty<byte>());
If you are just trying to create or overwrite the file StreamWriter(newPath, false) is good enough.
I am uploading file before that I want to delete existing file.
string newfilename = txtname.Text + "Resume" + fileExtension;
System.IO.File.Delete(newfilename);
AsyncFileUpload1.SaveAs(Server.MapPath("/tmp/jobres/" + uId) + "\\" + newfilename;
If I upload a .doc file, after that upload a .pdf file then both are there. I want only latest uploaded file not old file. How I can delete old file?
Before upload saveas, you can take file path for the directory and then delete all the existing files and then upload the latest file.
You can reference below code:
string newfilename = txtname.Text + "Resume" + fileExtension;
string[] pathOfFiles = System.IO.Directory.GetFiles(Server.MapPath("/tmp/jobres/" + uId));
foreach (string filePath in pathOfFiles)
{
System.IO.File.Delete(filePath);
}
AsyncFileUpload1.SaveAs(Server.MapPath("/tmp/jobres/" + uId) + "\\" + newfilename;
I'm trying to convert .sd9 files to .wav files in C#, these files are basically .wav files with unessecary headers (used in certain games).
I found some NodeJS code online to do so, this code works fine:
var snd_buf = fs.readFileSync(item);
if (snd_buf.slice(0x00, 0x03).toString() != "SD9") {
console.log("[-] '" + path.basename(item) + "' is not SD9 file.");
continue;
}
var wav_buf = snd_buf.slice(0x20, snd_buf.length);
if (sd9_file_list.length > 1) {
if (!fs.existsSync(output_path)) {
fs.mkdirSync(output_path);
}
output_path += "/" + path.basename(item, ".sd9") + ".wav";
}
output_path = path.normalize(output_path);
fs.writeFileSync(output_path, wav_buf);
item: Full path to the file
Now I'm trying to 'translate' this C# without any luck:
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.sd9", SearchOption.AllDirectories);
foreach (string file in files)
{
UTF8Encoding nobom = new UTF8Encoding(false);
string raw_buffer = File.ReadAllText(file, nobom);
string wave_buffer = "";
if (raw_buffer.Substring(0x00, 0x03) == "SD9")
{
wave_buffer = raw_buffer.Substring(0x20);
MessageBox.Show(wave_buffer);
}
if (File.Exists(fbdd.SelectedPath + #"\" + Path.GetFileNameWithoutExtension(file) + ".wav")) { File.Delete(fbdd.SelectedPath + #"\" + Path.GetFileNameWithoutExtension(file) + ".wav"); }
BinaryWriter sw = new BinaryWriter(new FileStream(fbdd.SelectedPath + #"\" + Path.GetFileNameWithoutExtension(file) + ".wav", FileMode.CreateNew));
sw.Write(wave_buffer);
sw.Flush();
sw.Close();
}
Either the way of writing or reading seems to add unnecessary headers / malformed bytes or whatever to make the file unreadable, for example, a correctly converted file with the NodeJS script is 91KB while my script outputs a corrupted file of 140KB.
Your problem is that you are reading in your SD9 file as a string. You're then using string functions to slice the file apart. Instead, you want to read your file as byte[].
Use File.ReadAllBytes() to read your file as byte[].
Use this trick to "splice" your byte array: Getting a sub-array from an existing array
Use System.Text.Encoding.ASCII.GetString() to convert byte[] to string for the "SD9" header comparison.
Use File.WriteAllBytes() to write your new file.
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string path = Server.MapPath("~//Images//" + FileName);
FileUpload1.SaveAs(path);
string imagepathsource = path;
string imagepathdest = #"D:\\" + Session["brandname"].ToString() + "\\" + Seasonfolders.SelectedItem.Text + "\\" + stylefolders.SelectedItem.Text + "\\Images\\" + FileName;
File.Move(imagepathsource, imagepathdest);
uploadedimage.ImageUrl = "D://" + Session["brandname"].ToString() + "//" + Seasonfolders.SelectedItem.Text + "//" + stylefolders.SelectedItem.Text + "//Images//" + FileName;
}
}
uploadedimage is a ImageButton where I need to display the image using imageurl with that link. The image is not displaying and no error.. I could see when the above code gets execute the page is getting refreshed? What code can be added in this ??
ImageUrl needs an URL.
So instead of assigning it with a local file name on disk, use something like this (where ~ stands for 'application root folder':
uploadedimage.ImageUrl = "~/Images/yourImage.png"
You have to supply it with:
An image inside your application root folder;
An URL that is catched by a HttpHandler, that generates / loads the image from a different location (a little harder to do, but if you need to load from another location then inside application root, this is the best option).
I have saved (Write) an image to "LocalFolder" not being picked up by the notification Toast.
StorageFolder systemLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
string path = systemLocalFolder.Path + "\\" + R.GetResourceString("CachedImageFolder") +
"\\" + R.GetResourceString("CachedImagePrefix") + contact + ".png";
path = path.Replace(#"\", #"/");
path = #"file:///" + path;
image.SetAttribute("src", path);
binding.AppendChild(image);
After setting this image , the toast does not show up.
However doing this:
image.SetAttribute("#Assets/Logo.png", path);
Does show up the toast with the image.
But i want to write a file and then use it and not pick from App Package.
Win8+XAML+C#
The file:/// protocol isn't supported for Windows 8 Store apps BUT you don't need it anyway. Something like this should get you there:
path = #"ms-appdata:///local/" + R.GetResourceString("CachedImageFolder") + "/"
+ R.GetResourceString("CachedImagePrefix") + contact + ".png";