how to write a file to Local Document library - c#

i am using streamwriter to write the file, however the location of the new written file is located within one of the folder with in the project folder but i would like the location to be on the document library of the computer. im using a mvc application not a console Application
Any help would be appreciate.
thank you
using (var File = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("~/OutputFileTest.csv"), false)) // true for appending the file and false to overwrite the file
{
foreach (var item in outputFile)
{
File.WriteLine(item);
}
}

From MSDN:
// Sample for the Environment.GetFolderPath method
using System;
class Sample
{
public static void Main()
{
Console.WriteLine();
Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
}
}
/*
This example produces the following results:
*/

Related

How to Modify Dicom Tags Using fo-dicom

I am creating a console application that will modify dicom tags. I will load up a single dicom file and update the PatientID tag.
I can not seem to to get anything to modify. I am able to read tags, but updating/adding does not seem to work for me. Previously I have used the DICOM ToolKit on powershell and it is very straight forward and easy, but I want to start developing in c# and so far I am failing.
using System;
using System.IO;
using SpiromicsImporterPrep.FileMethods;
using Dicom;
namespace SpiromicsImporterPrep
{
class Program
{
static void Main(string[] args)
{
string filename = #"Z:\SPIROMICS\Human_Scans\Dispatch_Received\NO_BACKUP_DONE_HERE\MIFAR\FORCE\JH114062-FU4\119755500\Non_Con_FRC__0.75__Qr40__5_7094\IM001139";
var file = DicomFile.Open(filename, readOption: FileReadOption.ReadAll);
var dicomDataset = file.Dataset;
dicomDataset.AddOrUpdate(DicomTag.PatientID, "TEST-PATIENT");
}
}
}
I expect after running the code when I look at the Dicom Header tags for this file with ImageJ or and other dicom reader that the value for the PatientID tag will be "TEST-PATIENT" The code runs with no errors but nothing seems to be updated or changed when I look at the dicom header.
you should invoke DicomFile.Save() Method.
string[] files = System.IO.Directory.GetFiles(#"D:\AcquiredImages\20191107\1.2.826.0.1.3680043.2.461.11107149.3266627937\1.2.276.0.7230010.3.1.3.3632557514.6848.1573106796.739");
foreach (var item in files)
{
DicomFile dicomFile = DicomFile.Open(item,FileReadOption.ReadAll);
dicomFile.Dataset.AddOrUpdate<string>(DicomTag.PatientName, "abc");
dicomFile.Save(item);
}
FileReadOption.ReadAll is required.

how to write a file to desktop using streamwriter

I have a block which is supposed to send the overwritten file to my desktop but the code does not seem to be working, I am using a MVC application not a console apllication.
Can anyone tell me what I am doing wrong or advise me on how to achieve my solution.
using (var File = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "~/ColTexOutputFileTest.csv", false)) // true for appending the file and false to overwrite the file
{
foreach (var item in outputFile)
{
File.WriteLine(item);
}
}
Remove the '~' char.
"\ColTexOutputFileTest.csv"
This character ' ~ ' used to find Server Side folder or file
For Example if you access App_Data folder in abc.xml file
HttpContext.Current.Server.MapPath("~/App_Data/abc.xml");
if you streamed file on local access to file as windows path
using (var File = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\ColTexOutputFileTest.csv", false)) // true for appending the file and false to overwrite the file
{
foreach (var item in outputFile)
{
File.WriteLine(item);
}
}
"~/ColTexOutputFileTest.csv" change it "\ColTexOutputFileTest.csv"
As stated in the answers above, the ~ is the problem. .Net provides the Path class which has a combine method for joining path & file names & not needing to know whether separators are needed :
using (var File = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ColTexOutputFileTest.csv"), false))
See : https://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx

Restrict users to upload zip file with folder inside

I have a file upload control.
I restrict users to upload only zip files.
the namespace i use is Ionic.Zip;
I also want check if that zip file has a folder inside.
I have to restrict the users not upload a zipfile with a folder inside.
I could check how many files inside zip file like
using (ZipFile zip = ZipFile.Read(file_path))
{
if (zip.Count < 5)
{
}
I do not know how to check for a folder inside
Anyone can help me please.
thanks in advance
void Main()
{
var isGood=false;
using (ZipFile zip = new ZipFile(#"c:\\1.zip"))
{
for (var i=0;i<zip.Count;i++)
if (zip[i].Attributes==FileAttributes.Directory)
{
isGood=false;
break;
}
}
if (isGood) Console.WriteLine ("ok");
else
Console.WriteLine ("error");
}
// Define other methods and classes here
edit :
there's seems to be a problem with the way you created this zip file.
I extracted the files from the file you sent me and created new zip : (named 3.zip):
and as you can see - the code works :
so I guess the dll is not powerful enough to recognize edge format
You can iterate on your zip object's ZipEntries - ZipEntry object contains IsDirectory property.
foreach(var entry in zip)
{
if(entry.IsDirectory)
{
//your stuff
}
}

How to fix System.UnauthorizedAccessException when decompressing bz2 file?

Im trying to decompress a bz2 file via code using the ICSharpCode.SharpZipLib.
It seems no matter where I make my file, even though I have FULL ACCESS control over it, I keep getting this Exception. Any help greatly appreciated.
using System;
using System.IO;
using ICSharpCode.SharpZipLib.BZip2;
namespace decompressor
{
class MainClass
{
public static void Main(string[] args)
{
string filePath = "C:\\FreeBase\\opinions.tsv.bz2";
string decompressPath = "C:\\Users\\mike\\Desktop\\Decompressed";
Console.WriteLine("Decompressing {0} to {1}", file, path);
BZip2.Decompress(File.OpenRead(filePath),File.OpenWrite(decompressPath), true);
}
}
}
Your code can have no access to create new paths at your desktop.
Check the permissions for the "C:\\Users\\mike\\Desktop\\Decompressed".
Maybe, you should write so:
string decompressPath = "C:\\Users\\mike\\Desktop\\Decompressed\\opinions.tsv";

DotNetZip - How to extract to working directory

I am trying to get a method that uses the DotNetZip library to extract a file to the current working directory, although I can't seem to get it to do it, it wants a file path:
private void unzipfiles()
{
using (var zip = Ionic.Zip.ZipFile.Read("ccsetup307.zip"))
{
zip.ExtractAll("directory-name",ExtractExistingFileAction.OverwriteSilently);
}
}
If you want to extract to the current directory, why don't you use the GetCurrentDirectory method and pass that in as the expected parameter like so:
using (var zip = Ionic.Zip.ZipFile.Read("ccsetup307.zip"))
{
zip.ExtractAll(Directory.GetCurrentDirectory()
,ExtractExistingFileAction.OverwriteSilently);
}
http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory.aspx
I know it isn't implicit but it should work fine for you.
You can use the below code as well.
string x = "your file name";
using (ZipFile zip = ZipFile.Read(x))
{
zip.ExtractAll(Path.GetDirectoryName(x), ExtractExistingFileAction.OverwriteSilently);
}

Categories