Im trying to take path to IIS virtual catalog but my solution returns me
file:///E:/Programy%20C#!Katalog2\Katalog2\MvcApplication1\Views\Home\jpg\1001\1\0
My code:
public ActionResult Searcher(string symbol, string dekoracja)
{
if (symbol != "" && dekoracja != "")
{
MyModel.Symbol = symbol;
MyModel.Dekorajca = dekoracja;
string path = Server.MapPath(Url.Content(#"~/Images/jpg/" + PathBuilder.Instance.pathBuilder(symbol, dekoracja)));
DirectoryInfo Dir = new DirectoryInfo(path);
try
{
FileInfo[] FileList = Dir.GetFiles("*jpg");
foreach (FileInfo fileInfo in FileList)
{
//tylko jpg ma wyswietlac do refaktoryzacji
MyModel.ImageList.Add(fileInfo.FullName);
}
}...
Im new in asp.net and I dont know how to take correct path (~/Vievs/Home/Jpg/...),i need it to put into image src. Symbol and Dekoracja are folder names given in parameter of ActionResult.
public class PathBuilder
{
public static readonly PathBuilder Instance = new PathBuilder();
public string pathBuilder(string Symbol, string Dekoracja)
{
return Symbol + #"\" + Dekoracja + #"\";
}
}
Your images should not be inside your /Views folder. Create a new folder in your app (say Images), then the code to generate the paths to be used in the src attribute of an <img> tag should be
string folder = String.Format("Images/{0}/{1}", symbol, dekoracja); // or whatever you call your folder
var path = Server.MapPath(folder);
FileInfo[] files = new DirectoryInfo(path).GetFiles(); // or GetFiles("*jpg") if it contains other image types
List<string> imageList = files.Select(x => String.Format("/{0}/{1}", folder, x.Name)).ToList();
Related
goal: we are not talking about files, but about folders. if the desired folder, which is specified in the array of strings, is available on the desktop, then we need to get subdirectories and paths to this folder, if the desired folder is not on the desktop, then the search for this folder has already been performed in appdata, and then the same thing, if the folder is present, then we get subdirectories and paths to this folder.
string[] directory = new string [] {#"folder1/", #"folder3/", }
foreach (string sPath in directory)
{
string Path;
if (sPath.Contains("folder1"))
{
Path = Desktop + sPath;
}
else
{
Path = Appdata + sPath;
}
there is an if (sPath.Contains("folder1"))
I intended the string Path to first take the Path = "Desktop + sPath" logic to return the names of the subdirectories of the folder1 folder
if (Directory.Exists(Path)) foreach (string folder in Directory.GetDirectories(Path))
{
Console.WriteLine(folder);
this code does not work for me = (if you delete a folder from the desktop, which, for example, was present, then the search from another place that is specified in the code is not carried out( how to fix the situation?
string Path;
if (sPath.Contains("folder1"))
{
Path = Desktop + sPath;
}
else
{
Path = Appdata + sPath;
}
full code
string [] directory = new string [] {#"folder1/", #"folder3/", }
foreach (string sPath in directory)
{
string sFullPath;
if (sPath.Contains("folder1"))
{
sFullPath = Desktop + sPath;
}
else
{
sFullPath = Appdata + sPath;
}
if (Directory.Exists(sFullPath)) foreach (string folder in Directory.GetDirectories(sFullPath))
{
Console.WriteLine(folder);
}
}
First of all we should come to terms. To be "available on the desktop" is to be a subfolder of
// Have a look at
// Environment.SpecialFolder.CommonDesktopDirectory
// Environment.SpecialFolder.DesktopDirectory
// as well
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
then we can manipulate with directory array:
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string[] directory = new string [] {#"folder1/", #"folder3/", };
foreach (string sPath in directory) {
if (Directory.Exists(Path.Combine(desktop, sPath))) {
// available on the desktop
}
else if (Directory.Exists(Path.Combine(appData, sPath))) {
// available on the AppData
}
else {
// not exists
}
}
In my Web.Config file I have add my IP address in file path. when i try to access all files of that folder then it gives an error uri format not supported. but if i give local file path then it works fine.
string pathdata = Utility.GetConfigValue("DevSubmittedStateTaxForms");
string uploadPath = Utility.GetConfigValue("DevUploadFiles");
DirectoryInfo d = new DirectoryInfo(pathdata);
FileInfo[] Files = d.GetFiles("*.pdf");
var status = new List<Object>();
int i = 1;
foreach (FileInfo filename in Files)
{
status.Add(new {ID = i, Name = filename.Name, URL = pathdata + filename.Name + ".pdf" });
i++;
}
using System;
using System.IO;
namespace Test
{
public class Program
{
static void Main(string[] args)
{
string pathdata = #"\\192.168.1.27\Temp\";
var d = new DirectoryInfo(pathdata);
var files = d.GetFiles("*.pdf");
foreach (var filename in files)
{
Console.WriteLine(pathdata + filename.Name + ".pdf");
}
Console.ReadLine();
}
}
}
The above code does work (on my machine - that is my current IP address and a folder I have shared on my machine). Your code likely doesn't work for one of two reasons:
Your path is not valid (e.g. using / instead of \)
CBR folder is not shared
I want get all files from an external storage folder(wall_e_imgs)..Here are codes-
public void getImages()
{
var path1 = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath.ToString();
string path = System.IO.Path.Combine(path1, "wall_e_imgs");
//var files= System.IO.Directory.GetFiles(Android.OS.Environment.ExternalStorageDirectory.ToString() + "wall_e_imgs");
//var files = System.IO.Directory.GetFiles(path);
//string path = Android.OS.Environment.ExternalStorageDirectory.ToString() + "/wall_e_imgs";
//File directory=new File(path);
Java.IO.File directory = new Java.IO.File(path);
Java.IO.File[] files = directory.ListFiles();//always count is 0 even though there are lot files there
foreach (var i in files)
{
FileInfo info = new FileInfo(i.Name);
if (info.Name.Contains("Wall_e"))
{
di.Add(new DownloadedImages { Path1 = info.DirectoryName, Name1 = info.FullName });
}
}
}
But it always give 0 files even though there are lot of files.
Try this
var folder = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "yourfoldername";
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
var filesList = Directory.GetFiles(folder);
foreach (var file in filesList)
{
var filename = Path.GetFileName(file);
}
Try something like this:
// Use whatever folder path you want here, the special folder is just an example
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "wall_e_imgs");
if (Directory.Exists(folderPath))
{
var files = Directory.EnumerateFiles(folderPath);
foreach (var file in files)
{
// Do your stuff
}
}
Please note that this uses the Directory class from System.IO, not Java.IO
ffilelist will contain a list of mp3 files in "/storage/emulated/0/Music/"
string phyle;
string ffilelist = "";
public void listfiles()
{
try
{
var path1 = "/storage/emulated/0/Music/";
var mp3Files = Directory.EnumerateFiles(path1, "*.mp3", SearchOption.AllDirectories);
foreach (string currentFile in mp3Files)
{
phyle = currentFile;
ffilelist = ffilelist + "\n" + phyle;
}
//playpath(phyle); // play the last file found
}
catch (Exception e9)
{
Toast.MakeText(ApplicationContext, "ut oh\n"+e9.Message , ToastLength.Long).Show();
}
}
I have created something that grabs all file names that have the extension .lua with them. This will then list them in a CheckListBox. Everything goes well there but I want to know which one of the CheckListBox's are ticked/checked and then open them in notepad.exe.
To dynamically add the files Code (works perfectly, and adds the files i want)
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string path = appData + "\\Lua";
string[] fileArray = Directory.GetFiles(path, "*.lua");
for (int i = 0; i < fileArray.Length; i++)
{
string Name = Path.GetFileName(fileArray[i]);
string PathToLua = fileArray[i];
ScriptsBoxBox.Items.AddRange(Name.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
Console.WriteLine(fileArray[i]);
}
Then when i check the items i want to open in notepad i use `
System.Diagnostics.Process.Start("notepad.exe", ScriptsBoxBox.CheckedItems.ToString());
Or
System.Diagnostics.Process.Start("notepad.exe", ScriptsBoxBox.CheckedItems);
Neither works and im pretty sure it's on my end. So my problem is that i cannot open the file that is ticked/checked in checklistbox and want to resolve this problem. However when I do
System.Diagnostics.Process.Start("notepad.exe", PathToLua);
It opens the files with .lua extension ticked or not which makes sense.
I don't think there are any arguments that you can pass to notepad to open a list of specific files. However, you can use a loop to open each file.
foreach (var file in ScriptsBoxBox.CheckedItems)
{
System.Diagnostics.Process.Start("notepad.exe", file);
}
I don't know WinForms as well as WPF but here goes
You need an object that contains your values
public class LuaFile
{
public string FileName { get; set; }
public string FilePath { get; set; }
public LuaFile(string name, string path)
{
FileName = name;
FilePath = path;
}
public override string ToString()
{
return FileName;
}
}
Replace your for loop with
foreach (var file in files)
{
ScriptsBoxBox.Items.Add(new LuaFile(Path.GetFileName(file), file));
}
And to run the checked files
foreach (var file in ScriptsBoxBox.CheckedItems)
{
System.Diagnostics.Process.Start("notepad.exe", ((LuaFile)file).FilePath);
}
Thanks everyone that helped but I solved it on my own (pretty easy when you read :P)
For anyone in the future that wants to do this here is how i accomplished it.
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string path = appData + "\\Lua";
string[] fileArray = Directory.GetFiles(path, "*.lua");
for (int i = 0; i < fileArray.Length; i++)
{
string Name = Path.GetFileName(fileArray[i]);
string PathToLua = fileArray[i];
//ScriptsBoxBox.Items.AddRange(Name.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
// Console.WriteLine();
Console.WriteLine(ScriptsBoxBox.CheckedItems.Contains(Name));
var pathname = ScriptsBoxBox.CheckedItems.Contains(Name);
if (ScriptsBoxBox.CheckedItems.Contains(Name))
{
System.Diagnostics.Process.Start("notepad.exe", fileArray[ScriptsBoxBox.CheckedItems.IndexOf(Name)]); // I supposed this would get the correct name index, and it did! fileArray by default seems to get the path of the file.
}
Ok, so I have made some code here
which will list all the directories and what I want to do is to display the folder names but when selected and push save it saves the file path not just the name
codes
the file look folder
FolderBrowserDialog elfenliedtopfan5wins = new FolderBrowserDialog();
// elfenliedtopfan5wins.RootFolder = Environment.SpecialFolder.ProgramFiles;
if (elfenliedtopfan5wins.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// test 1
// Example #1: Write an array of strings to a file.
// Create a string array that consists of three lines. //raw = 0 \\raw\\images = 1 \\raw\\weapons = 2 \\raw\\weapons = 3 \\raw\\xmodel = 4 \\raw\\xmodelparts = 5 \\raw\\xmodelsurfs = 6
string[] lines = { elfenliedtopfan5wins.SelectedPath + "\\raw" };
// and then closes the file.
System.IO.File.WriteAllLines(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\elfenlied_weapons\\path.txt", lines);
string rawsave = elfenliedtopfan5wins.SelectedPath;
Properties.Settings.Default.rawpath = rawsave;
Properties.Settings.Default.Save();
listbox();
copyfilealais();
public void listbox()
{
LOADMOD.Items.Clear();
string[] getfiles = Directory.GetFiles(Properties.Settings.Default.rawpath + "\\mods");
string[] dirs = Directory.GetDirectories(Properties.Settings.Default.rawpath + "\\mods");
foreach(string file in getfiles)
{
LOADMOD.Items.Add(file);
}
foreach (string dir in dirs)
{
LOADMOD.Items.Add(dir);
}
}
this way it works file but I don't want to show the path in the listbox. I just want the name of the folders which I added.
foreach(string file in getfiles)
{
LOADMOD.Items.Add(path.getfilename(file));
}
foreach (string dir in dirs)
{
LOADMOD.Items.Add(path.getfilename(dir));
}
Which shows like this which is what I want but when I push save it just saves name of the folder aka this
so I was wording is their a way to have it like this picture with all folder names shown but when I push save it saves the file path not just the name?
image of what i want it to be like but to save the path instead of the folder name
the save button
private void buttonX7_Click(object sender, EventArgs e)
{
Properties.Settings.Default.modpath = LOADMOD.SelectedItem.ToString();
// testing to see if it saves file path
MessageBox.Show(Properties.Settings.Default.modpath);
richTextBoxEx1.LoadFile(Properties.Settings.Default.modpath + "//mod.csv", RichTextBoxStreamType.PlainText);
}
and I still want it to save the path but only way it does that if i don't include path.getfilename
does anyone have any-idea how i would be able to do this ?
Instead of keep File name or File path in each Listbox item, you can keep an object in it, then play with DisplayMember and ValueMember properties of the ListBox.
In this scenario you need define just one simple class for your all Listbox items and create an instance of it for each item. In the following sample this class is MyType.
Inside MyType.cs file:
public class MyType
{
public string Name {get; set;}
public string Path {get; set;}
}
Inside your main class:
public void listbox()
{
LOADMOD.Items.Clear();
string[] getfiles = Directory.GetFiles(Properties.Settings.Default.rawpath + "\\mods");
string[] dirs = Directory.GetDirectories(Properties.Settings.Default.rawpath + "\\mods");
LOADMOD.DisplayMember = "Name";
LOADMOD.ValueMember = "Path";
foreach(string file in getfiles)
{
// Create an item for the list
var thisItem = new MyType {
Name = path.getfilename(file),
Path = file
};
LOADMOD.Items.Add(thisItem);
}
foreach (string dir in dirs)
{
// Create an item for the list
var thisItem = new MyType {
Name = path.getfilename(dir),
Path = dir
};
LOADMOD.Items.Add(thisItem);
}
}
Now you can use it with Text and SelectedValue properties of the ListBox. When you select an item you can write some codes like these:
string MyName = LOADMOD.Text;
string MyPath = LOADMOD.SelectedValue;
// Now You have both Name and Path of your file or directory here
MessageBox.Show(MyName);
MessageBox.Show(MyPath);