I'm new in c#,and write this code:
const string file ="http://ipaddress/"+"1.jpg";
var fileName = file.Split('\\').Last();
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var fts = new FileToSend(fileName, fileStream);
await Bot.SendPhotoAsync(update.Message.Chat.Id, fts, "Nice Picture");
}
but in this line:
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
get this error:
URI formats are not supported
How can i solve that problem ?thanks.
I'd suggest you use WebClient instead of FileStream for this. Here's a link that might help you: URL Formats are not supported
Hope it helps!
Related
I want to read a binary file line by line (I'm writing of course continously, but I know that after 457 bytes new data start and I know exactly the byte structure and where which information is written to) and change a special entry of the line. I get an System.IO.IOException when I try to access the same file with both BinaryReader and BinaryWriter. I use locking to prevent that the file is accessed from somewhere else.
My code is:
using (FileStream fs2 = new FileStream(testfile, FileMode.Open, FileAccess.Read))
{
using (BinaryReader r = new BinaryReader(fs2))
{
using (BinaryWriter bw = new BinaryWriter(new FileStream(testfile, FileMode.Open, FileAccess.Write), utf8))
{
for (int i = 0; i < 11000; i+=457)
{
int myint = r.ReadInt64();
bw.Seek(i, SeekOrigin.Current);
bw.Write(myint*2);
}
}
}
}
How can I do this?
Do not create the second FileStream because the file is locked for the read operation by the first FileStream object.
If you are sure about file structure, the exception only can come out from 2nd FileStream instantiation. See link below for more information:
Read and Write to File at the same time
It is working for me using the following code:
if (File.Exists(testfile))
{
FileInfo fi = new FileInfo(testfile);
using (FileStream fs2 = new FileStream(testfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (BinaryReader r = new BinaryReader(fs2))
{
r.BaseStream.Seek(0, SeekOrigin.Begin);
using (BinaryWriter bw = new BinaryWriter(new FileStream(testfile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)))
{
for (int i = 0; i <= (fi.Length-177); i += 177)//181
{
}
}
}
}
}
I encrypted a pdf and would like to save it to the database, below are the codes as to how I did it. I would like to know if there are any ways to set the Created "PDFEncrypted.pdf" onto the Stream fs without using a FileUploader?
string WorkingFolder = Server.MapPath("~/Files/");
string InputFile = Path.Combine(WorkingFolder, "PDF.pdf");
string OutputFile = Path.Combine(WorkingFolder, "PDFEncrypted.pdf");
using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(InputFile);
PdfEncryptor.Encrypt(reader, output, true, "pass123", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
So I have these codes to upload it
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
/*Initialize FileStreams for both writing and reading*/
writeStream = new FileStream(logfilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
readStream = new FileStream(logfilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
/*Initialize the Writer and Reader*/
fileWrite = new StreamWriter(writeStream);
fileRead = new StreamReader(readStream);
So this is what I use to be able to constantly write and read to the same file. How can I clear the file without the need to close both streams temporarily, clear file, and reopen them again?
I have the following problem:
I use this code to open a file and write to it:
using (FileStream fileStream = new FileStream(saveDir + #"\" + saveFile, FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite))
{
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.Write("Test");
streamWriter.Close();
}
but in the "using" line it tells me that it can not open the file because it is used by another process but the file is not open and isn't used. What's wrong?
I searched around this forum and the internet but I can't find a solution.
You could try using FileMode.OpenOrCreate instead of creating the file "manually":
using (FileStream fileStream = new FileStream(saveDir + #"\" + saveFile,
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.Write("Test");
streamWriter.Close();
}
through my application, i open a text file, read it and than close it.
then, manually accessing to that file is restricted.
how do i solve it?
here is my code:
fs = new FileStream(#"C:\Weather\somePlace.json", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (StreamReader streamReader = new StreamReader(fs))
{
String responseData = streamReader.ReadToEnd();
//Deserialize the json output
var outObject = JsonConvert.DeserializeObject<RootObject>(responseData);
// do something with the information
fs.Close();
fs = null;
}
thanks !
You don't need the FileStream actually. You can just use this:
using (StreamReader streamReader = new StreamReader(#"C:\Weather\somePlace.json"))
{
String responseData = streamReader.ReadToEnd();
//Deserialize the json output
var outObject = JsonConvert.DeserializeObject<RootObject>(responseData);
// do something with the information
}
As FileStream is IDisposable, it should be disposed after usage.
So wrap it into using to ensure file will be released:
using(fs = new FileStream(#"C:\Weather\somePlace.json", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// the rest of your code
}