c# windows app writing text to file - c#

Having real problems with writing to a text file using following code
using HCP1.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.UI.Xaml.Media.Imaging;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Storage;
using System.Threading.Tasks;
using System.Text;
using System.Diagnostics;
private void imageview_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
var point = e.GetPosition(imageview);
var p1 = (int)point.X;
var p2 = (int)point.Y;
string stringVal1;
string stringVal2;
stringVal1 = System.Convert.ToString(p1);
stringVal2 = System.Convert.ToString(p2);
Text3.Text = stringVal1;
Text4.Text = stringVal2;
if (p2 > 210 && p2 < 235 && p1 > 339 && p1 < 367) {
string dave = Environment.NewLine + Text3.Text + "," + Text4.Text;
string path = #"C:\myfile.txt";
StreamWriter sw = new StreamWriter(path);
string bufferOne = dave;
sw.Write(bufferOne);
sw.Close();
sw.Dispose();
};
}
When a double tap is done it should save the contents of a textblock to a file but I have three errors
The best overloaded method match for 'System.IO.StreamWriter.StreamWriter(System.IO.Stream)' has some invalid arguments (This is at the Streamwriter Line)
cannot convert from 'string' to 'System.IO.Stream' (At the same line)
'System.IO.StreamWriter' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'System.IO.StreamWriter' could be found (are you missing a using directive or an assembly reference?) (This is at the Close line)
As you can see I have SYstem.io reference
Any ideas where im going wrong?
Any help appreciated
Mark

Use File.WriteAllText. It's a static method and does the same as opening, writing and closing a file. Remember, the less code the less bugs.
if (p2 > 210 && p2 < 235 && p1 > 339 && p1 < 367)
{
string dave = Environment.NewLine + Text3.Text + "," + Text4.Text;
File.WriteAllText(#"C:\myfile.txt", dave);
}

Related

System.IO.FileStream does not contain a definition for 'ReadLine' and no extension method 'ReadLine' of type. Are you missing an assembly reference?

I am trying to read a basic text file that contains 1 byte of data.
Apparently, ReadLine isn't defined and I don't know what's wrong. See code below.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
public class loadGame : MonoBehaviour {
// Use this for initialization
void Start () {
FileStream file = new FileStream("save.txt", FileMode.Open, FileAccess.Read);
int introPlayed = file.ReadLine();
if (introPlayed == 1)
{
SceneManager.LoadScene("game");
file.Close();
}
else
{
SceneManager.LoadScene("intro");
file.Close();
}
}
// Update is called once per frame
void Update () {
}
}
Any help appreciated!

What using statement do I need for WavReader and FlacWriter?

so far I have these using statements:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using System.Threading;
using NAudio.Wave;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Alvas.Audio;
I'm trying to write a wav to flac converter.
Here is the function:
public static void flac_converter()
{
string inputFile = Path.Combine("wav ", input);
string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac"));
if (!File.Exists(inputFile))
throw new ApplicationException("Input file " + inputFile + " cannot be found!");
WavReader wav = new WavReader(inputFile);
using (var flacStream = File.Create(outputFile))
{
FlacWriter flac = new FlacWriter(flacStream, wav.BitDepth, wav.Channels, wav.SampleRate);
// Buffer for 1 second's worth of audio data
byte[] buffer = new byte[wav.Bitrate / 8];
int bytesRead;
do
{
bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length);
flac.Convert(buffer, 0, bytesRead);
} while (bytesRead > 0);
flac.Dispose();
flac = null;
}
}
But I get this error: "The Type of Namespace 'WavReader' cannot be found"
I also get this error: "The Type or Namespace 'FlacWriter' cannot be found"
I know I have to do two things, download the appropriate lib, then all teh DLL files to the reference of the project and also add "using blahBlah;"
However, I don't know what DLL i need, or what using statements I need to get rid of these errors. Anyone know?
change the spelling of wavreader to wavereader. It should work out.

NAudio Object reference not set to an instance of an object

Referring to this question, I have used the code from it: Play audio from a stream using C#
However, I get an error "Object reference not set to an instance of an object" running in debug. I have assigned Url's to a combo box, and when I choose the first choice, opening an ASX stream, the program crashes, giving me the above in debugger.
Any ideas? I have tried doing some fixing with the code below, but I don't think I'm getting the idea, since it still isn't working.
Edit:// It doesn't recognize Mp3FileReader however there are no errors/warnings, it isn't green as it normally would be, am I missing a library?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using NAudio.Wave;
using NAudio;
public static void PlayMp3FromUrl(string url)
{
using (Stream ms = new MemoryStream())
{
using (Stream stream = WebRequest.Create(url).GetResponse().GetResponseStream())
{
byte[] buffer = new byte[32768];
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
ms.Position = 0;
Mp3FileReader fr = new Mp3FileReader(ms);
WaveStream blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(fr));
if(ms != null)
{
using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
waveOut.Init(blockAlignedStream);
waveOut.Play();
while (waveOut.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(100);
}
}
}
else
{
MessageBox.Show("blockAlignedStream variable was null!");
}
}
}
The code sample you are using is not a good example of how to play an MP3 from a stream. There are multiple problems with the code. Either use the technique described in this article, or you may be able to make use of the new MediaFoundationReader which can play from a URL.

Wrong encoding, output file gives � instead of normal char

I want to make a program that searches a file for desired chars in words (letters č ć ž š), replaces them with c z s etc. and saves the file. In my attempt, however, I get some stupid signs, so that means it opens the file wrongly. When I try to add encoding.unicode it gives me errors (shown below). And one more question, how do I make a program which opens files by dragging them in an .exe file.
Error 3 The best overloaded method match for
'System.IO.File.Open(string, System.IO.FileMode,
System.IO.FileAccess)' has some invalid
arguments C:\Users\Vulisha\AppData\Local\Temporary
Projects\ConsoleApplication1\Program.cs 14 59 ConsoleApplication1
Error 4 Argument '3': cannot convert from 'System.Text.Encoding' to
'System.IO.FileAccess' C:\Users\Vulisha\AppData\Local\Temporary
Projects\ConsoleApplication1\Program.cs 14 122 ConsoleApplication1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (StreamReader stream = new StreamReader(File.Open(#"C:\Users\Vulisha\Desktop\titl.txt", FileMode.Open)))
{
string fileText = stream.ReadToEnd();
// Do your replacements
fileText = fileText.Replace(#"č", #"c");
fileText = fileText.Replace(#"ć", #"c");
fileText = fileText.Replace(#"š", #"s");
fileText = fileText.Replace(#"ž", #"z");
fileText = fileText.Replace(#"đ", #"d");
fileText = fileText.Replace(#"Č", #"C");
fileText = fileText.Replace(#"Č", #"C");
fileText = fileText.Replace(#"Š", #"S");
fileText = fileText.Replace(#"Ž", #"Z");
fileText = fileText.Replace(#"Đ", #"D");
using (StreamWriter writer = new StreamWriter(File.Open(#"titl.txt", FileMode.Create)))
{
// You do a create because the new file will have less characters than the old one
writer.Write(fileText);
}
}
}
}
}
You need to be more careful with placement of parentheses. You need
new StreamWriter(File.Open(#"titl.txt", FileMode.Create), Encoding.Unicode)
but you wrote
new StreamWriter(File.Open(#"titl.txt", FileMode.Create, Encoding.Unicode))
See the difference?

IPTC .NET read/write c# library

I am looking for some library to read/write IPTC metadata from Jpg files.
Open source or paid, doesn't matter.
It should work with .NET 3.5 and c#.
Does anybody know such a library? I googled but haven't found anything.
*http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.aspx
using System;
using System.IO;
using System.Linq;
using System.Windows.Media.Imaging;
namespace wictest
{
class Program
{
static void Main(string[] args)
{
var stream = new FileStream("1.jpg", FileMode.Open, FileAccess.Read);
var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
var metadata = decoder.Frames[0].Metadata as BitmapMetadata;
if(metadata != null)
Console.WriteLine(metadata.Keywords.Aggregate((old, val) => old + "; " + val));
Console.ReadLine();
}
}
}
You need to reference PresentationCore.dll and WindowsBase.dll to get access to the System.Windows.Media namespace.

Categories