We have to write a file in one drive (L) which is the shadow copy of the C drive. We tried with normal like below.
string datFile = "L:\\DATA\\ABC.DAT";
string message = "test";
try
{
using (StreamWriter writerAppend = new StreamWriter(datFile, true))
{
writerAppend.WriteLine(message);
}
}
But it is giving error
System.IO.DirectoryNotFoundException: Could not find a part of the path 'L:\DATA\ABC.DAT
Please help if there is any specific way to access it.
Related
I wrote a program with asp.net core I would extract tar archive files and I have used this method.
tarArchive.ExtractContents(destPathFolder)
but I got error Exception creating directory '*' because a file or directory with the same name already exists. I don't know how to resolve this issue I have done a lot of searches but I couldn't solve my issue.
for example, the blow address is a place I would extract my files.
C:\data\634192CC-FDCB-4711-BD41-B0F7A7E77148\BAF59489-C7F9-4747-A0A1-B3C7A94FFFD4-2019-12-08
14-47-19\task-2019-12-08 21-13-20
this method extract tar archives files that have text files. It means if we have image files we will get that error.
static void ExtractTar(Stream inStream, String destFolder)
{
string sourcePath = Path.Combine(destFolder , "/data" );
Stream tarStream = new TarInputStream(inStream);
TarArchive tarArchive = TarArchive.CreateInputTarArchive(tarStream);
tarArchive.ExtractContents(sourcePath );
tarArchive.Close();
tarStream.Close();
inStream.Close();
}
I have changed to this code but I have got Access path to "" is denied.
static void ExtractTar(string addressFile, string destFolder)
{
Stream inStream = System.IO.File.OpenRead(addressFile);
TarArchive tarArchive = TarArchive.CreateInputTarArchive(inStream);
tarArchive.ExtractContents(destFolder);
tarArchive.Close();
inStream.Close();
}
I have a simple WinForms application, but it has some Embedded Resources (in a subfolder under "Resources") that I would like to copy out to a folder on the computer. Currently, I have the latter working (with a explicit method naming the Embedded Resource and where it should go):
string path = #"C:\Users\derek.antrican\";
using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("WINFORMSAPP.Resources.SUBFOLDER.FILE.txt"))
using (Stream output = File.Create(path + "FILE.txt"))
{
input.CopyTo(output);
}
But I'm still trying to figure out how to get the former working: looping through all the resources in the "WINFORMSAPP.Resources.SUBFOLDER" folder and moving them. I've done quite a bit of Googling, but I'm still not sure how to get a list of each Embedded Resource in this subfolder.
Any help would be GREATLY appreciated!
Start by getting all resources embedded in your assembly:
Assembly.GetExecutingAssembly().GetManifestResourceNames()
You can check these names against the name of your desired subfolder to see if they are inside or outside it with a simple call to StartsWith.
Now loop through the names, and get the corresponding resource stream:
const string subfolder = "WINFORMSAPP.Resources.SUBFOLDER.";
var assembly = Assembly.GetExecutingAssembly();
foreach (var name in assembly.GetManifestResourceNames()) {
// Skip names outside of your desired subfolder
if (!name.StartsWith(subfolder)) {
continue;
}
using (Stream input = assembly.GetManifestResourceStream(name))
using (Stream output = File.Create(path + name.Substring(subfolder.Length))) {
input.CopyTo(output);
}
}
I created a android app to create a stockage list by capturing code bars, the idea is to write a csv file in to a network folder, because I want the app to run as much offline as it's possible.
Currently my code looks like:
string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
string filename = Path.Combine(path, "stock.csv");
using (var streamWriter = new StreamWriter(filename, true))
using (var writer = new CsvWriter(streamWriter))
{
foreach (var item in articulos)
{
writer.WriteField(item.codbar);
writer.WriteField(item.reference);
writer.WriteField(item.quantity);
writer.NextRecord();
}
}
string path2 = #"\\Desktop-jce8pl5\csv\stock.csv";
File.Copy(filename, path2,true);
But I keep geting a System.UnauthorizedAccessException
I tried to enter directly to the file from another computer and there
is no problem.
I give full permission to "all" and "network"
I tried directly with IP I tried not to copy, just to create
string path = #"\\Desktop-jce8pl5\csv\stock.csv";
FileStream fs = null;
if (File.Exists(path))
{
fs = File.Open(path, FileMode.Append);
}
else
{
fs = File.Create(path);
}
But there is no way.
Any help?
Thanks.
As #RobertN sugested, I tried to connect with EX File Ex and detected that I was unable to, so I checked the windows 10 general configuration to shared folders and it was only enabled to auth users.
I changed that, then I start with the cifsmanager but on that moment we decided that, if the user has access to local network he will most sure have acces to internet, so I will send the file by email.
I'm using Xamarin, and according to previous answers, this shall work:
string path = Path.Combine(Android.OS.Environment.DirectoryDownloads, "families.txt");
File.WriteAllText(path, "Write this text into a file!");
But it doesn't, I get and unhandled exception. I have set the permissions to read and write to external storage (even though this is internal).
I also tried it with this:
string content;
using (var streamReader = new StreamReader(#"file://" + path )) // with and without file://
{
content = streamReader.ReadToEnd();
}
But I got the same unhandled exception.
UPDATE: The path is the problem, since I get the else part here:
Java.IO.File file = new Java.IO.File(path);
if (file.CanRead())
testText.Text = "The file is there";
else
testText.Text = "The file is NOT there\n" + path;
Which is weird, because the path seems to be correct. The exceptions: Could not find a part of the path: /Download/families.txt
UPDATE2: On external storage, it works, with the same code... Might it be my device's problem? That would be great, cause I tested the external storage version on my friend's phone, but mine doesn't have external storage (OnePlus One), so I'm still looking for a solution (if there's any).
Finally found a solution.
var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filename = Path.Combine(path.ToString(), "myfile.txt");
The path was the problem, now with a simple streamwriter it works like magic.
try
{
using (var streamWriter = new StreamWriter(filename, true))
{
streamWriter.WriteLine("I am working!");
}
}
catch { ... }
I am attempting to add a file import function to an admin webpage where a CSV file will be imported from a local C: drive
I've just realized that string filePath = Path.GetFileName(FileUpload1.FileName); doesn't actually allow the client path to be read and only gives the filename.
protected void btnImportData_Click(object sender, EventArgs e)
{
List<CSVFile> entries = new List<CSVFile>();
string filePath = Path.GetFileName(FileUpload1.FileName);
using (TextFieldParser parser = new TextFieldParser(filePath))
{
//Other code
}
}
Currently the line TextFieldParser parser = new TextFieldParser(filePath) gives an error as I can't get the full path to the CSV.
Is it possible to load the contents of FileUpload1 into variable parser instead? I can see the control has content but not sure how or if it's possible?
The FileName property is simply the filename the browser sent, and is not yet physically stored on the server. To do so, you'll need to save it first:
string filePath = String.Format({0}{1}", System.IO.Path.GetTempPath(), FileUpload1.FileName); // Save to temp directory
FileUpload1.SaveAs(filePath);
using (TextFieldParser parser = new TextFieldParser(filePath))
{
//...
}
File.Delete(filePath); // Delete the file if you're done with it
Also, if TextFieldParser can take in a System.IO.Stream, you won't even need to save the file to disk first:
using (TextFieldParser parser = new TextFieldParser(FileUpload1.FileContent)) // Read stream
{
//...
}
Take into account that the file is not stored yet on the server so there is no path. FileName is just that: a file name and not a full path.
If you require the file to exist on the file system in order to parse it, you'll need to first save it.