I have to search the filepath from filename into my harddisk.
I was wondering if there is a way to use main Windows 7 search manager (start->edit text with "search programs and file".
Or simply if there is a quick way to find filepath within computer
Can you help me?
You'll need the Windows Search SDK.
Edit: Check out the related MSDN docs.
Here's some sample code, i've used in my univercity project:
DirectoryInfo di = new DirectoryInfo("/some/path");
foreach (FileInfo i in di.GetFiles("filter.text"))
{
// do something
}
Here, /some/path is path where you want to search files and filter.text is filename filter (for example, *.* for all files or *.cpp for .cpp files match. These would match all files or 1.cpp, main.cpp and Main.Project.cpp, respectively).
Related
I am a newbie to programming so i am hoping support for my problems from all my friends
This is the problem
I have used the loation as 2DB to read a csv file.It works well when I give the file name also But I want to give only location and program needs to select the file
string path = "G:\kaash\2DB\";
string[] row_text = System.IO.File.ReadAllLines(""+path+", *.csv");
string[] data_col = null;
This code will help you to find all .csv files as a list of strings from the specified directory.
string path = #"G:\kaash\2DB\";
List<string> csvIn2DB = System.IO.Directory.GetFiles(path, "*.csv", SearchOption.TopDirectoryOnly).ToList();
By modifying the search pattern you can locate your files more specifically. You can change the SearchOption to AllDirectories if you want to extend the search to inner folders of the specified directory.
Let you need to get all your .csv file names follows the pattern "FILECSV_xxxxxx.csv", then the search pattern will be like this: FILECSV_*.csv
I have a quiz that has a login feature but, when you change pc you must also change the drive the file is located e.g D drive, E drive etc...
Currently its set to F. Is there something i can add that will make it automatically search each drive for the file?
Here is my code
if (File.Exists(#"F:\C# Completed quiz\adam new\Mario quiz\bin/PlayerDetails.txt"))
{
string[] lines = File.ReadAllLines(#"F:\C# Completed quiz\adam new\Mario quiz\bin/PlayerDetails.txt");
I'd recommend you just put it in the AppData or MyDocuments folder:
string filename = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "C# Completed quiz","adam new","Mario quiz","bin","PlayerDetails.txt");
//or Environment.SpecialFolder.MyDocuments
if (File.Exists(filename))
{
string[] lines = File.ReadAllLines(filename);
}
you need to enumerate the hard drives on the system and inspect them one at a time
https://msdn.microsoft.com/en-us/library/system.io.directory.getlogicaldrives(v=vs.110).aspx
shows how to enumerate the hard drives on the system
This answer uses what looks like a better way
How can I determine if a given drive letter is a local/mapped/usb drive?
Once you know a drive is a mounted drive the you should look at <drive>:/<your path>
Use Resource file to store your txt inside the application or just distribute it with your executable. To add resource file:
Right click on your project and select Properties
Go to Resources tab and create new file
Click Add resource -> Add Existing File...
Choose your text file and click Open
The file can now be accessed like string:
var lines = Properties.Resources.PlayerDetails;
If the file is in the same folder as your exe then you can access it in this way:
var lines = File.ReadAllLines("PlayerDetails.txt");
Edit: Note the comment below if you prefer to use this method.
I have a folder "res/resx/" which contatins .resx files. What I want is to get all those .resx files.
Here is my code for that.
var Path = Server.MapPath("~/");
var SampleUrl = System.IO.Path.GetFullPath(System.IO.Path.Combine(Path, "Res/resx/"));
string[] files= System.IO.Directory.GetFiles(SampleUrl);
var AllFiles = new System.Collections.ObjectModel.ReadOnlyCollection<string>(files);
foreach (string sFileName in AllFiles)
{
Response.write(sFileName + " <br>");
}
This code is working on my local and i was able to see a list of my resx files. But when i deploy this to my website and access it, an error occurs on the 2nd line of code which says:
Could not find a part of the path
'D:\Websites\mywebsite.com\Res\resx'
I tried allowing directory browsing to see if my files exist. In my local, i can browse the files but on the website, I cannot. And it seems the system cannot find the folder "Res/Resx" too. it says:
404 - File or directory not found. The resource you are looking for
might have been removed, had its name changed, or is temporarily
unavailable.
But the folder exist and it is running on my local. Any advice as to what i should do or is their something i have missed? Thanks
Try this
string[] filePaths = Directory.GetFiles(Server.MapPath("Your folder"));
Look at the System.IO.Directory class and the static method GetFiles. It has an overload that accepts a path and a search pattern. Example:
string[] files = System.IO.Directory.GetFiles(path, "*.resx");
answered by
Anthony Pegram here.
I am trying to write out a text file to: C:\Test folder\output\, but without putting C:\ in.
i.e.
This is what I have at the moment, which currently works, but has the C:\ in the beginning.
StreamWriter sw = new StreamWriter(#"C:\Test folder\output\test.txt");
I really want to write the file to the output folder, but with out having to have C:\ in the front.
I have tried the following, but my program just hangs (doesn't write the file out):
(#"\\Test folder\output\test.txt");
(#".\Test folder\output\test.txt");
("//Test folder//output//test.txt");
("./Test folder//output//test.txt");
Is there anyway I could do this?
Thanks.
Thanks for helping guys.
A colleague of mine chipped in and helped as well, but #Kami helped a lot too.
It is now working when I have:
string path = string.Concat(Environment.CurrentDirectory, #"\Output\test.txt");
As he said: "The CurrentDirectory is where the program is run from.
I understand that you would want to write data to a specified folder. The first method is to specify the folder in code or through configuration.
If you need to write to specific drive or current drive you can do the following
string driveLetter = Path.GetPathRoot(Environment.CurrentDirectory);
string path = diveLetter + #"Test folder\output\test.txt";
StreamWriter sw = new StreamWriter(path);
If the directory needs to be relative to the current application directory, then user AppDomain.CurrentDomain.BaseDirectory to get the current directory and use ../ combination to navigate to the required folder.
You can use System.IO.Path.GetDirectoryName to get the directory of your running application and then you can add to this the rest of the path..
I don't get clearly what you want from this question , hope this get it..
A common technique is to make the directory relative to your exe's runtime directory, e.g., a sub-directory, like this:
string exeRuntimeDirectory =
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
string subDirectory =
System.IO.Path.Combine(exeRuntimeDirectory, "Output");
if (!System.IO.Directory.Exists(subDirectory))
{
// Output directory does not exist, so create it.
System.IO.Directory.CreateDirectory(subDirectory);
}
This means wherever the exe is installed to, it will create an "Output" sub-directory, which it can then write files to.
It also has the advantage of keeping the exe and its output files together in one location, and not scattered all over the place.
Basically, I have this code:
DirectoryInfo dir = new DirectoryInfo(#"\\MYNETWORK11\ABCDEFG\ABCDEFGHIJKL\00806\");
FileInfo[] files = dir.GetFiles("200810*");
I expect it to match any files starting with 200810. However, it's matching files named
20070618_00806.bak and 20070817_00806.bak (the stars aren't in the filename, that was the only way I could include the underscore)
I tried it with dir from a command prompt, and it matches those files also. Why?
Edit:
Maybe using C: as the example was not a good thing. The directory I'm actually querying is a network share
\\MYNETWORK11\ABCDEFG\ABCDEFGHIJKL\00806\
If checking against the short name has anything to do with it, won't 20070817_00806.bak be 200708~1.bak? That doesn't match either
msdn states that
"Because this method checks against
file names with both the 8.3 file name
format and the long file name format,
a search pattern similar to "*1*.txt"
may return unexpected file names. For
example, using a search pattern of
"*1*.txt" will return
"longfilename.txt" because the
equivalent 8.3 file name format would
be "longf~1.txt"."
Could this be the cause?
Try this from the command line:
dir /x 200810*
The "/x" will make it show the short filenames, as well as the long filenames. This would let you see whether the short filename actually does start with "200810".
I can't reproduce this, either from the command line or in a test app:
c:\Users\Jon\Test>echo > 20070618_00806.bak
c:\Users\Jon\Test>echo > 2007081700806.bak
c:\Users\Jon\Test>dir 200810*
Volume in drive C is OS
Volume Serial Number is B860-7E20
Directory of c:\Users\Jon\Test
File Not Found
And the C# app:
using System;
using System.IO;
class Test
{
static void Main()
{
foreach (var file in new DirectoryInfo(".").GetFiles("200810*"))
{
Console.WriteLine(file);
}
}
}
(This doesn't print any results.)
Perhaps there's some OS setting somewhere which is making a difference... which OS are you using? (I'm on 32-bit Vista.)
GetFiles will search the long file name and the short filename...it's not somehow matching short file names is it?