i am trying to make a program that looks at all the drives, tries to find a removable one with the name "DOM WORK" and then gets the drive name (like E:) and then prints that. Unfortunately i get this error message:
Unhandled Exception: System.IO.IOException: The device is not ready.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIODriveError(String driveName, Int32 errorCode)
at System.IO.DriveInfo.get_VolumeLabel()
at WorkSync.Program.Main(String[] args) in d:\dominic\documents\visual studio 2015\Projects\WorkSync\WorkSync\Program.cs:line 19
I have tried building the program as an executable and then running it with administrator privileges, which did not work.
Here is my code that i am using:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorkSync
{
class Program
{
static void Main(string[] args)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
string memStickName = "";
foreach(DriveInfo drive in allDrives)
{
Console.WriteLine(drive.VolumeLabel);
if (drive.VolumeLabel == "DOM WORK" && drive.DriveType.ToString() == "removable")
{
memStickName = drive.Name;
break;
}
}
Console.WriteLine(memStickName);
}
}
}
You need to make sure that the drive is actually accessible using the DriveInfo.IsReady property.
foreach(DriveInfo drive in allDrives)
{
if (drive.IsReady == true)
{
// Check volume name here
}
}
Related
in the constructor :
SaveLoadFiles.LoadFile(textBoxRadarPath, "radarpath.txt");
SaveLoadFiles.LoadFile(textBoxSatellitePath, "satellitepath.txt");
if (textBoxRadarPath.Text != "" || textBoxSatellitePath.Text != "")
{
if(!Directory.Exists(textBoxRadarPath.Text))
{
Directory.CreateDirectory(textBoxRadarPath.Text);
}
if (!Directory.Exists(textBoxSatellitePath.Text))
{
Directory.CreateDirectory(textBoxSatellitePath.Text);
}
btnStart.Enabled = true;
}
else
{
btnStart.Enabled = false;
}
the SaveLoadFiles class
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Weather
{
public class SaveLoadFiles
{
public static void SaveFile(string contentToSave, string fileName)
{
string applicationPath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory); // the directory that your program is installed in
string saveFilePath = Path.Combine(applicationPath, fileName);
File.WriteAllText(saveFilePath, contentToSave);
}
public static void LoadFile(TextBox loadTo, string fileName)
{
string applicationPath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory); // the directory that your program is installed in
string saveFilePath = Path.Combine(applicationPath, fileName); // add a file name to this path. This is your full file path.
if (File.Exists(saveFilePath))
{
loadTo.Text = File.ReadAllText(saveFilePath);
}
}
}
}
when i used the application before and backed it up on my usb flash drive the second hard drive letter was D i had two hard disks : C and D and the project and the folders were on drive D.
now i backed up the project including the saved files but now my hard disks letters are C and E there is no D
but in the constructor when it's reading the text files the folders in the text files are D:....etc
but it should be E:
I'm checking if the folder exist or not and then if not creating it but it's trying to create the folder on drive D and D is not existing.
You are reading contents of a data file that has a file path that no longer exists.
The solution is to edit those data files: "radarpath.txt" and "satellitepath.txt" to have the proper path.
An application would normally provide a UI for selecting the folder to use, rather than saving a hardcoded path in a datafile. What you could do is use FileDialog to prompt the user for the directories to use if they don't exist.
How Do I open google chrome using c#?
It shows System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
I'd triedProcess.Start("C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe"); too, but it shows the same exception
using System;
using System.Diagnostics;
namespace tempTest
{
class Program
{
static void Main(string[] args)
{
Process.Start("chrome.exe");
}
}
}
The chrome application path can be read from the registry.
You can try following codes:
var key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", false);
if(key != null)
{
var path = Path.Combine(key.GetValue("Path").ToString(), "chrome.exe");
if(File.Exists(path))
{
Process.Start(path);
}
}
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've very new to the TestStack (White) UI Automation library and I'm having a bit of an issue in terms of "hooking" the process. I'm trying to hook CCleaner, but I keep getting
An unhandled exception of type 'TestStack.White.AutomationException'
occurred in TestStack.White.dll
Additional information: Couldn't find window with title Piriform
CCleaner in process 1156, after waiting for 30 seconds:
My current code is:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using TestStack.White;
using TestStack.White.Factory;
using TestStack.White.UIItems.Finders;
using TestStack.White.InputDevices;
using TestStack.White.UIItems.WindowItems;
namespace NightWipe
{
class Program
{
private const string ExeSourceFile = #"C:\Program Files\CCleaner\CCleaner.exe";
private static TestStack.White.Application _application;
private static TestStack.White.UIItems.WindowItems.Window _mainWindow;
static void Main(string[] args)
{
clean();
}
public static string clean()
{
var psi = new ProcessStartInfo(ExeSourceFile);
_application = TestStack.White.Application.AttachOrLaunch(psi);
_mainWindow = _application.GetWindow("Piriform CCleaner");
_mainWindow.WaitWhileBusy();
return "";
}
}
}
I thought that maybe it was the name of the process since CCleaner starts another process (not CCleaner.exe) but CCleaner64.exe as seen here, which I can assume is for 64 bit operating systems maybe? Anyway I tried names including: "CCleaner", "CCleaner64"; but this threw the same exact exception.
I'm using Inspect by Microsoft and this is what it pulls for me (large image):
Inspect's information. Any idea what I'm doing wrong here?
The problem is that CCleaner is visible as WIN32 app. So GetWindow() doesn't work. You can try this code:
public void CCleanerSample()
{
var application = Application.AttachOrLaunch(new ProcessStartInfo(#"C:\Program Files\CCleaner\CCleaner.exe"));
AutomationElement ccleanerAutomationElement = null;
Console.Write("Waiting till WIN32 app is launching");
while (ccleanerAutomationElement == null)
{
ccleanerAutomationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "Piriform CCleaner"));
Thread.Sleep(1000);
Console.Write(".");
}
Console.WriteLine(" Done");
var mainWindow = new Win32Window(ccleanerAutomationElement, WindowFactory.Desktop, InitializeOption.NoCache,
new WindowSession(application.ApplicationSession, InitializeOption.NoCache));
}
I am trying to retrieve process information and I'm aware that I can use:
Process[] myProcesses = Process.GetProcesses();
but how do I retrieve the process description? Is it via some Win32 API call? I'm running Vista and when I click under the Processes tab in Task Manager, I see the description.
What you see in Task Manager is actually the Description field of the executable image.
You can use the GetFileVersionInfo() and VerQueryValue() WinAPI calls to access various version informations, e.g. CompanyName or FileDescription.
For .Net way, use the FileDescription member of FileVersionInfo, instantiated with the executable name got via Process.MainModule.FileName.
Another way would be through Assembly. Load the Assembly from the executable image, then query the AssemblyDescriptionAttribute custom attribute.
You just have to go a bit further down the properties.
Suppose you have an instance of notepad running.
Process[] proc = Process.GetProcessesByName("notepad");
Console.WriteLine("Process version- " + proc[0].MainModule.FileVersionInfo.FileVersion);
Console.WriteLine("Process description- " + proc[0].MainModule.FileVersionInfo.FileDescription);
There you go !
This is the only way I could see to do it. I tried Process and Win32_Process, but no go.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
namespace Management
{
class Program
{
static void Main(string[] args)
{
var ps = Process.GetProcesses();
foreach (var p in ps)
{
try
{
var desc = FileVersionInfo.GetVersionInfo(p.MainModule.FileName);
Console.WriteLine(desc.FileDescription);
}
catch
{
Console.WriteLine("Access Denied");
}
}
Console.ReadLine();
}
}
}