Open a file in c# [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to read all the files in a folder. If the name is index.html, nothing happens. It's not even opening the file in the browser.
This is the code I use .
foreach (System.IO.FileInfo thefile in fiArr)
{
if (thefile.Name == "index.html")
{
FileStream fileStream = new FileStream(path + thefile.Name, FileMode.Open, FileAccess.Read);
}
}

All that your code this is create a FileStream pointing to this file. So you could read the file and fetch its contents in memory. But you cannot expect it to open in any browser. You could use the Process.Start method to open the file using the default program that is associated with this file type:
foreach (System.IO.FileInfo thefile in fiArr)
{
if (thefile.Name == "index.html")
{
Process.Start(thefile.Name);
}
}

Your code is putting the contents of the file into a FileStream so you can use it in your code. You'll need to do something with that FileStream next.
If you wanted the file opened using the default application (i.e. appearing in a browser) use this:
System.Diagnostics.Process.Start(thefile);

Related

How to get No extension file type [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want get file type but my file noting extension in C#,Asp.net.
I want get file type from downloaded file with webClient.DownloadFile.
Can trace file?
Can understand downloaded file is pdf or no?
There is no absolute way to get the file type of a file with no extension.
Some file types, such as EXE, have a header that you could check. Other files, such as text files, could probably be detecting using some sort of heuristic that looks for only text characters. But still other files would not be identifiable.
As long as you know the extension beforehand it's possible, otherwise you'd have to be familiar with the file's structure. If all you're doing is downloading a pdf without the .pdf extension, rename your file [something].pdf when you download it and you'll be able to access it as a regular pdf.
There are some links where same question has been asked . I hope it helps
https://superuser.com/questions/435224/how-do-i-find-out-the-file-type-without-an-extension
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/16c34850-8799-4a8b-8702-2f59d96e79c1
http://www.troublefixers.com/open-files-without-any-valid-file-extension/
http://bytes.com/topic/c-sharp/answers/812692-open-file-without-knowing-extension
FileStream fs = new FileStream(#"c:\a.pdf", FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
string pdfText = r.ReadToEnd();
Regex rx1 = new Regex(#"/Type\s*/Page[^s]");
MatchCollection matches = rx1.Matches(pdfText);
MessageBox.Show("The PDF file has " + matches.Count.ToString() + " page(s).";

File.Copy Access Denied [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a problem with copying from local disk to flash. The code worked before, but after I create anoteher foreach loop and I had to create new objects, the File.Copy functionality isn't working anymore.
In value File | System.IO.File there are values like: Error_Access_Denied | 5, Error_Invalid Parameter| 87, GetFileExInfoStandard | 0.
edit: locationUSB present file path on flash. (locationUSB == "D:\something.hex") and x._location == "C:\something_1.hex" .
foreach (object item in this.dataGridView2.Rows)
{
versionOnDisk = this.VersionInt(x._version);
versionOnFlash = this.VersionInt(((DataGridViewRow)item).Cells[2].Value.ToString());
if (versionOnFlash > versionOnDisk)
forbidCopying = true;
else
locationUSB = _logicalDrive + ((DataGridViewRow)item).Cells["Filename"].Value.ToString(); // <-- location value (because of the foreach)
if (!forbidCopying)
File.Copy(x._location, locationUSB, true); // <--
else if (AllowDelete.Checked)
File.Delete(locationUSB);
}
edit:
If I change the location into logical drive path, which value is "D:\" I get the DirectoryNotFoundException was unhandled: Could not find a part of the path 'D:\'.
Most probably after creating new files, you are not closing the FileStreams. Close the FileStreams of the newly created files using either myFile.Close(); or create new files inside using (var myFile = File.Create(myPath)) blocks.

The C# equivalent [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am trying to close the program :HHTCntrl.exe as if the user clicked exit on that program. I have the following Borland C++ code. What is the C# equivalent?
FILE *sf;
AnsiString ClosePollControl = AnsiString("HHT")+cbHHTNo->Text+AnsiString(".cls");
sf = fopen(ClosePollControl.c_str(),"w");
fclose(sf);
You're opening the file for writing. Assuming that cbHHTNo is the string containing file name, in C# it will go like that:
var path = "HHT" + cbHHTNo + ".cls";
using (var file = File.OpenWrite(path))
{
// do sth with the open file stream here
}
Well, it is actually pretty simple.
There isn't a concept of an AnsiString in C# so you have to use a string.
string ClosePollControl = "HHT" + cbHHTNo->Text + ".cls";
// Open the stream and write to it.
using (FileStream fs = File.OpenWrite(ClosePollControl))
{
}
This assumes you have some user level control, cbHHTNo, that you are getting the Text (e.g. string).
If you are trying to shutdown the process then use System.Diagnostics.Process class. For example,
Process process = Process.GetProcessesByName("HHTCntrl.exe").FirstOrDefault();
if (process != null)
{
process.Kill(); // to immediatelly kill the process or use process.CloseMainWindow() to gracefully "kill" the process
}
If you want to close an active form use Close() method.

What is the most robust way to open file and read content [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I usually do this when I have to read the whole file always:
using (var fileStream = File.OpenRead(...))
using (var reader = new StreamReader(fileStream))
var content = reader.ReadToEnd();
Any better/faster way?
string content = System.IO.File.ReadAllText(#"your file path");
If you want to get lines:
string[] lines = System.IO.File.ReadAllLines(#"your file path");
if you want to only read lines until get to some line then use the IEnumerable ReadLines:
foreach(string line in System.IO.File.ReadLines(#"your file path")
{
if (line == ...)
{
break;
}
}

How to move file/folder to an already existing folder [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm working on something like recycle bin, that I need to move folder/files to already existing folder, I tried to use
Directory.Move
but it creates new directory and that's wrong for me, I have a specific directory to move to.
Can you help me?
It seems do don't actually want to move the folder, you want to move the contents of the folder. If you want to do that, you have to tell the computer to do that:
void MoveContentsOfDirectory(string source, string target)
{
foreach (var file in Directory.EnumerateFiles(source))
{
var dest = Path.Combine(target, Path.GetFileName(file));
File.Move(file, dest);
}
foreach (var dir in Directory.EnumerateDirectories(source))
{
var dest = Path.Combine(target, Path.GetFileName(dir));
Directory.Move(dir, dest);
}
// optional
Directory.Delete(source);
}

Categories