I am trying to create script for my terminal server, which will move all folders with their contents, created in the wrong place. I don't know which names these folders will have. As I noticed, moving folders in C# is a problem for some reason. Can someone help me with my code? It just deleting my test folders and nothing moves.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.VisualBasic.FileIO;
namespace ConsoleApp1
{
public class Programm
{
public static void Main()
{
string root = #"C:\Users\user1\Desktop";
string[] subdirectoryEntries = Directory.GetDirectories(root);
string destDirname = #"D:\confiscated";
foreach (string path in subdirectoryEntries)
{
FileSystem.MoveDirectory(path, destDirname, true);
}
}
}
}
This is how to do it:
string startPath = #"YOURSTARTPATH";
string endPath = #"YOURENDPATH";
foreach (string directory in Directory.GetDirectories(startPath))
{
Directory.Move(directory, Path.Combine(endPath, Path.GetFileName(directory)));
}
This way, you will use the already provided by the framework classes to work with directories.
No need to include Microsoft.VisualBasic.FileIO
Related
c# File handler error.I can't find it.
use SafeFileHandle filehandle -> error
I tried to copy it, but it didn't work out well.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Win32.SafeHandles;
namespace test
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("검색할 경로를 입력하세요.");
string result1 = Console.ReadLine();
Console.WriteLine("파일 확장자를 입력하세요.");
string result2 = Console.ReadLine();
Filehandle file1 = new Filehandle();
file1.FileCheck(result1,result2);
}
}
You need to install the package in your project before you can use it.
dotnet add package Microsoft.Win32.SafeHandles
This question already has answers here:
How to get a path to the desktop for current user in C#?
(2 answers)
Closed 3 years ago.
I have a File on my Desktop and I want to get the full Path of the File in my code, should it be on my Desktop or anywhere
My code is looking like this
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GetFullPath
{
class Program
{
static void Main(string[] args)
{
string filename = "eMemoExpenseApproval.docx";
string fullFilePath = Path.Combine(Directory.GetCurrentDirectory(), filename);
Console.Write("Path : " + fullFilePath);
Console.Read();
}
}
}
Rather than get the full path from Desktop it shows the Path from Visual Studio, which is not suppose to be so, but i get this instead
Path : C:\Users\Administrator\Documents\Visual Studio 2017\Projects\GetFullPath\GetFullPath\bin\Debug\eMemoExpenseApproval.docx
Edit:
this works to get the Path of the file on Desktop
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GetFullPath
{
class Program
{
static void Main(string[] args)
{
string filename = "eMemoExpenseApproval.docx";
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string fullFilePath = path +"/"+ filename;
Console.Write("Path : " + fullFilePath);
Console.Read();
}
}
}
Fine but How about other directories?
Directory.GetCurrentDirectory() actually returns the directory in which the application is executed.
If you know that the file is located in your Desktop, you can instead do something like this :
string fullFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop, filename));
As I understand you want to search a limited set of folders for a named file. To do that declare a function like this:
IEnumerable<string> FindInMultipleFolders(string[] folders, string filename)
{
var result = new List<string>();
foreach (var folder in folders)
{
var dirs = Directory.GetFiles(folder, filename);
foreach (String dir in dirs)
{
result.Add(dir);
}
}
return result;
}
And call it with the file name and the folders to search like this:
FindInMultipleFolders(
new string[]
{
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
#"C:\Some\Other\Folder\I\Would\Like\Searched"
},
"eMemoExpenseApproval.docx");
}
The file might be in multiple folders, so the function returns an IEnumerable<string>. FindInMultipleFolders only searches the passed folders, not subfolders. If you want subfolders to be searched you should add SearchOption.AllDirectories as a third parameter to GetFiles. Then you could search the whole hard drive with:
FindInMultipleFolders(
new string[]
{
#"C:\"
},
"eMemoExpenseApproval.docx");
}
I have couple questions about referencing methods / variables between two or more *.cs files. I know that there are similar topics, but I still don't quite understand what is going on.
I'm using Visual Studio Community 2015.
So here is the problem. I have 2 files, those files are First.cs and Second.cs. They are saved in completely different, known locations on hard disc.
Inside First.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forum
{
class First
{
static void Main(string[] args)
{
}
public int GiveMeNumber()
{
return 5;
}
}
}
Inside Second.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forum
{
class Second
{
int number = // method from file First.cs
}
}
How do I access method GiveMeNumber() from First.cs in Second.cs as assignment for int number? How do I tell my compiler where are those files?
Thanks for any help :)
Like Alex said in his comment.
You can create a solution/project, add existing item, and browse to your first.cs and second.cs.
Mark both files with the public-keyword
for example:
namespace Forum
{
public class Second
{
int number = // method from file First.cs
}
}
Then both class can be used within each other.
So you could do
var first = new First();
var number = first.GiveMeNumber();
you probably want to do it the other way around, because I think you have a console app where your First class has a main-method.
does that help>?
I am trying to figure out if any of the below 3 locations are accessible and append to build location,if multiple locations are accessible pick one of them and bail out if none exit,can anyone provide info on how to do this?
1.BIN-LOC-WiFi-FW\loc_proc\bin
2.loc_proc\pkg\cnss_proc\bin
3.loc_proc_ps\package
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace aputloader
{
class Program
{
static void Main(string[] args)
{
string buildlocation = #"\\location\builds784\INTEGRATION\LOC.1.2-00028-Z-1";
//check if atleast one of the following folders exist and append to buildlocation
//1.BIN-LOC-WiFi-FW\loc_proc\bin
//2.loc_proc\pkg\cnss_proc\bin
//3.loc_proc_ps\package
//multiple folders exist ,pick one
//none exist ,bail out
}
}
}
Maybe something like this?
if(Directory.Exists("BIN-LOC-WiFi-FW\loc_proc\bin"))
{
// This path is a directory
}
else if(Directory.Exists("loc_proc\pkg\cnss_proc\bin"))
{
// This path is a directory
}
else if(Directory.Exists("loc_proc_ps\package")
{
// This path is a directory
}
else
{
Console.WriteLine("No valid folder exists.");
// Do nothing.
}
I created this class "XML_Toolbox" that could be used by any of my forms to perform any of the key XML actions that i am going to be using repeatedly. So with that being said, here is that class' code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace Personal_Finance_Manager
{
class XML_toolbox
{
public static void createFile (string filename, string filePath)
{
string createPath = filePath + #"\" + filename + ".txt";
if (file.exists(createPath))
{
StreamWriter outfile = new StreamWriter(createPath, true);
}
else
{
MessageBox.Show("This file already exists!!! Please choose another name!");
}
}
}
}
all the individual parts were working when called from another form up until i added the:
if (file.exists(createPath)) {}
IF statement.
Now i am getting the
The name "file" does not exist in the current context
error. I have the
using System.IO;
what else am i missing?
Thanks!
Class name is File not file, method name is Exists. C# is case-sensitive.
It's called File, not file.
File.Exists()